Fix newline in Windows.
[PaGMO.git] / GOclasses / algorithms / ASA.cpp
blobb81cce5fde13960195603b0cd659306984fd71fb
1 /*
2 * ASA.cpp
3 * SeGMO, a Sequential Global Multiobjective Optimiser
5 * Created by Dario Izzo on 5/16/08.
6 * Copyright 2008 ¿dvanced Concepts Team (European Space Agency). All rights reserved.
8 */
10 #include <iostream>
12 #include "ASA.h"
13 #include "rng.h"
15 Population ASAalgorithm::evolve(Individual x0, GOProblem& problem) {
17 const std::vector<double>& LB = problem.getLB();
18 const std::vector<double>& UB = problem.getUB();
20 std::vector<double> xNEW = x0.getDecisionVector();
21 std::vector<double> xOLD = xNEW;
22 double fNEW;
23 fNEW = x0.getFitness();
24 double fOLD;
25 fOLD = fNEW;
26 SolDim = xNEW.size();
27 std::vector<double> Step(SolDim,StartStep);
28 std::vector<int> acp(SolDim,0) ;
29 double ratio=0;
30 double currentT = T0;
31 double prob=0;
32 double r=0;
34 //Main SA loops
35 for ( int jter=0; jter < niterOuter; jter++){
36 for ( int mter = 0; mter < niterTemp; mter++){
37 for ( int kter = 0 ; kter < niterRange; kter++){
38 int nter = (int)(drng()*SolDim);
39 for ( int numb = 0; numb < SolDim ;numb++){
40 nter=(nter+1) % SolDim;
41 //We modify the current point actsol by mutating its nter component within
42 //a Step that we will later adapt
43 r = 2.0*drng()-1.0; //random number in [-1,1]
44 xNEW[nter] = xOLD[nter] + r * Step[nter] * ( UB[nter] - LB[nter] );
46 // If new solution produced is infeasible ignore it
47 if ((xNEW[nter] > UB[nter]) || (xNEW[nter] < LB[nter])){
48 xNEW[nter]=xOLD[nter];
49 continue;
51 //And we valuate the objective function for the new point
52 fNEW = problem.objfun(xNEW);
54 // We decide wether to accept or discard the point
55 if (fNEW < fOLD) //accept
57 xOLD[nter] = xNEW[nter];
58 fOLD = fNEW;
59 acp[nter]++; //Increase the number of accepted values
61 else //test it with Boltzmann to decide the acceptance
64 prob = exp ( (fOLD - fNEW )/ currentT );
66 // we compare prob with a random probability.
67 if (prob > drng())
69 xOLD[nter] = xNEW[nter];
70 fOLD = fNEW;
71 acp[nter]++; //Increase the number of accepted values
73 else{
74 xNEW[nter] = xOLD[nter];
76 } // end if
77 } // end for(nter = 0; ...
78 } // end for(kter = 0; ...
81 // adjust the step (adaptively)
82 for (int iter=0; iter < SolDim; iter++)
84 ratio = (double)acp[iter]/(double)niterRange;
85 acp[iter]=0; //reset the counter
86 if (ratio > .6) //too many acceptances, increase the step by a factor 3 maximum
88 Step[iter] = Step [iter] * (1 + 2 *(ratio - .6)/.4);
90 else
92 if (ratio < .4) //too few acceptance, decrease the step by a factor 3 maximum
94 Step [iter]= Step [iter] / (1 + 2 * ((.4 - ratio)/.4));
97 //And if it becomes too large, reset it to its initial value
98 if ( Step[iter] > StartStep ){
99 Step [iter] = StartStep;
103 // Cooling schedule
104 currentT *= Tcoeff;
107 Population newpop;
108 if (fOLD < x0.getFitness()){
109 x0.setDecisionVector( xOLD );
110 x0.setFitness( fOLD );
112 newpop.addIndividual( x0 );
113 return newpop;
116 void ASAalgorithm::initASA(int niterTotInit, int niterTempInit, int niterRangeInit, int SolDimInit, double T0Init, double TcoeffInit, double StartStepInit, uint32_t randomSeed){
117 niterTot=niterTotInit;
118 niterTemp=niterTempInit;
119 niterRange=niterRangeInit;
120 SolDim=SolDimInit;
121 T0=T0Init;
122 Tcoeff=TcoeffInit;
123 StartStep=StartStepInit;
124 niterOuter = niterTot / (niterTemp * niterRange * SolDim);
125 drng.seed(randomSeed);
128 void ASAalgorithm::initASA(int niterTotInit,int SolDimInit, double Ts, double Tf, uint32_t randomSeed){
129 niterTot=niterTotInit;
130 niterTemp=1;
131 niterRange=20;
132 SolDim=SolDimInit;
133 niterOuter = niterTot / (niterTemp * niterRange * SolDim);
134 T0=Ts;
135 Tcoeff=pow(Tf/Ts,1.0/(double)(niterOuter));
136 StartStep=1;
137 drng.seed(randomSeed);