| | 152 | === Definición del problema y sus características === |
| | 153 | |
| | 154 | {{{ |
| | 155 | #!cpp |
| | 156 | //Don't forget require the package if it has still no been loaded |
| | 157 | #Require NonLinGloOpt; |
| | 158 | |
| | 159 | //Defining the problem |
| | 160 | NonLinGloOpt::@Problem problem = [[ |
| | 161 | //Optimization method. If not specified the system will choose one |
| | 162 | //by calling method select_automatic_algorithm |
| | 163 | //In this case we want to use MMA (Method of Moving Asymptotes) |
| | 164 | // http://ab-initio.mit.edu/wiki/index.php/NLopt_Algorithms#MMA_.28Method_of_Moving_Asymptotes.29 |
| | 165 | Real id_algorithm = NonLinGloOpt::Algorithm::LD_MMA; |
| | 166 | //We want to minimize the target function |
| | 167 | Real sign = -1; |
| | 168 | //We want just a local minimum |
| | 169 | Real neededGlobal = False; |
| | 170 | //The problem is almost twice differentiable |
| | 171 | Real id_analyticalClass = |
| | 172 | NonLinGloOpt::AnalyticalClass::TWICE_DIFFERENTIABLE; |
| | 173 | //The gradient of all functions is known |
| | 174 | Real id_useGradient = True; |
| | 175 | //Target given as simple Code |
| | 176 | Anything target = my_function; |
| | 177 | //Lower bounds |
| | 178 | Matrix lower_bounds = Col(-10, 0); //x1>=-10, x2 >= 0 |
| | 179 | //Upper bounds |
| | 180 | Matrix upper_bounds = Col( 10, 10); //x1<= 10, x2 <=10 |
| | 181 | //There are two non linear constraining inequations |
| | 182 | Set inequations = [[ |
| | 183 | [[ineq1, ineq1::constraint]], //Inequation given by class method |
| | 184 | [[ineq2, ineq2::constraint]] //Inequation given by class method |
| | 185 | ]]; |
| | 186 | //Initial values |
| | 187 | Matrix x0 = Col(1.234, 5.678); |
| | 188 | //Stopping criteria of relative tolerance for both x and y |
| | 189 | Real relativeTolerance = 1E-4; // |
| | 190 | //Stopping criteria of maximum running time |
| | 191 | Real maxTime = 60; |
| | 192 | //Funcitons will be traced each this number of evaluations |
| | 193 | Real verboseEach = 1 |
| | 194 | ]]; |
| | 195 | |
| | 196 | }}} |
| | 197 | |
| | 198 | === Creación y lanzamiento de la optimización === |
| | 199 | |
| | 200 | {{{ |
| | 201 | #!cpp |
| | 202 | #Require NonLinGloOpt; |
| | 203 | |
| | 204 | //Creating the optimizer instance |
| | 205 | NonLinGloOpt::@Opt opt = problem::create_optimizer(?); |
| | 206 | |
| | 207 | //Running the optimization |
| | 208 | Real opt::optimize_problem(problem); |
| | 209 | |
| | 210 | }}} |
| | 211 | |