weight_generation.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00034 #include <acado/optimization_algorithm/weight_generation.hpp>
00035 
00036 
00037 
00038 BEGIN_NAMESPACE_ACADO
00039 
00040 
00041 
00042 //
00043 // PUBLIC MEMBER FUNCTIONS:
00044 //
00045 
00046 
00047 WeightGeneration::WeightGeneration(){
00048 
00049 }
00050 
00051 WeightGeneration::WeightGeneration( const WeightGeneration& arg ){
00052 
00053 }
00054 
00055 WeightGeneration::~WeightGeneration( ){
00056 
00057 }
00058 
00059 WeightGeneration& WeightGeneration::operator=( const WeightGeneration& arg ){
00060 
00061     if( this != &arg ){
00062 
00063     }
00064     return *this;
00065 }
00066 
00067 
00068 returnValue WeightGeneration::getWeights( const int    &m        ,
00069                                           const int    &pnts     ,
00070                                           const DVector &weightsLB,
00071                                           const DVector &weightsUB,
00072                                           DMatrix       &Weights  ,
00073                                           DVector       &formers    ) const{
00074 
00075     DVector weight(m);
00076     weight.setZero();
00077 
00078     const int layer = m-1;
00079 
00080     int lastone     = -1            ;
00081     int currentone  = -1            ;
00082     double    step        = 1.0/(pnts-1.0);
00083 
00084     const int n = 0;
00085 
00086     WeightGeneration child;
00087     return child.generateWeights( n, weight, Weights, weightsLB, weightsUB, formers, layer, lastone, currentone, step );
00088 }
00089 
00090 
00091 
00092 
00093 //
00094 // PROTECTED MEMBER FUNCTIONS:
00095 //
00096 
00097 
00098 returnValue WeightGeneration::generateWeights( const  int    &n         ,
00099                                                DVector        &weight    ,
00100                                                DMatrix        &Weights   ,
00101                                                const  DVector &weightsLB ,
00102                                                const  DVector &weightsUB ,
00103                                                DVector        &formers   ,
00104                                                const int     &layer     ,
00105                                                int           &lastOne   ,
00106                                                int           &currentOne,
00107                                                double        &step
00108                                              ) const{
00109 
00110 
00111    int run1, run2;
00112 
00113     if( n == layer ){
00114 
00115         //printf("case1:  n = %d  layer = %d  \n", n, layer );
00116 
00117         double weight_test = 1.0;
00118         for( run1 = 0; run1 < (int) weight.getDim()-1; run1++ )
00119             weight_test -= weight(run1);
00120 
00121         if( ( weight_test >= weightsLB(layer) - 100.0*EPS ) &&
00122             ( weight_test <= weightsUB(layer) + 100.0*EPS )    ){
00123 
00124             if( currentOne >= 0 ){
00125 
00126                 DVector tmp( formers.getDim()+1 );
00127 
00128                 for( run1 = 0; run1 < (int) formers.getDim(); run1++ )
00129                     tmp(run1) = formers(run1);
00130 
00131                 tmp(formers.getDim()) = lastOne;
00132                 formers = tmp;
00133                 lastOne = currentOne;
00134                 currentOne = -1;
00135             }
00136             else{
00137 
00138                 DVector tmp( formers.getDim()+1 );
00139 
00140                 for( run1 = 0; run1 < (int) formers.getDim(); run1++ )
00141                     tmp(run1) = formers(run1);
00142 
00143                 tmp(formers.getDim()) = Weights.getNumCols();
00144                 formers = tmp;
00145             }
00146 
00147              weight(n) = weight_test;
00148 
00149              DMatrix tmp( weight.getDim(), Weights.getNumCols()+1 );
00150 
00151              for( run1 = 0; run1 < (int) Weights.getNumRows(); run1++ ){
00152                  for( run2 = 0; run2 < (int) Weights.getNumCols(); run2++ )
00153                      tmp(run1,run2) = Weights(run1,run2);
00154              }
00155 
00156              for( run2 = 0; run2 < (int) weight.getDim(); run2++ )
00157                      tmp(run2,Weights.getNumCols()) = weight(run2);
00158 
00159              Weights = tmp;
00160         }
00161     }
00162     else{
00163 
00164         //printf("case2:  n = %d  layer = %d  \n", n, layer );
00165 
00166         double weight_test = weightsLB(n);
00167 
00168         while( weight_test <= weightsUB(n) + 100.0*EPS ){
00169 
00170             if( n == layer-2 ) currentOne = Weights.getNumCols()+1;
00171 
00172             weight(n) = weight_test;
00173 
00174             WeightGeneration child;
00175             child.generateWeights( n+1, weight, Weights, weightsLB, weightsUB, formers, layer, lastOne, currentOne, step );
00176 
00177             weight_test += (weightsUB(n)-weightsLB(n))*step;
00178         }
00179     }
00180 
00181     return SUCCESSFUL_RETURN;
00182 }
00183 
00184 
00185 
00186 CLOSE_NAMESPACE_ACADO
00187 
00188 // end of file.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:40:19