436 |
//} |
//} |
437 |
|
|
438 |
|
|
439 |
#ifdef SPECULATIVE |
#ifdef _OPENMP |
440 |
|
//#ifdef SPECULATIVE |
441 |
void OptInfoSimann::OptimiseLikelihoodOMP() { |
void OptInfoSimann::OptimiseLikelihoodOMP() { |
442 |
|
|
443 |
//set initial values |
//set initial values |
739 |
x[i] = bestx[i]; |
x[i] = bestx[i]; |
740 |
} |
} |
741 |
} |
} |
742 |
|
//#endif |
743 |
#endif |
#endif |
744 |
|
|
745 |
// calcule a new point |
// calcule a new point |
805 |
} |
} |
806 |
|
|
807 |
/// Represents the function that computes how good the parameters are |
/// Represents the function that computes how good the parameters are |
|
double evaluate_f(const DoubleVector& params) { |
|
|
double trialf; |
|
808 |
#ifdef _OPENMP |
#ifdef _OPENMP |
809 |
|
double evaluate_par_f(const DoubleVector& params) { |
810 |
|
double trialf; |
811 |
int id = omp_get_thread_num (); |
int id = omp_get_thread_num (); |
812 |
trialf = EcoSystems[id]->SimulateAndUpdate(params); |
trialf = EcoSystems[id]->SimulateAndUpdate(params); |
813 |
#else |
return -trialf; |
814 |
trialf = EcoSystem->SimulateAndUpdate(params); |
} |
815 |
#endif |
#endif |
816 |
|
double evaluate_f(const DoubleVector& params) { |
817 |
|
double trialf; |
818 |
|
trialf = EcoSystem->SimulateAndUpdate(params); |
819 |
return -trialf; |
return -trialf; |
820 |
} |
} |
821 |
|
|
1014 |
} |
} |
1015 |
|
|
1016 |
|
|
1017 |
void OptInfoSimann::OptimiseLikelihood() { |
#ifdef _OPENMP |
1018 |
|
void OptInfoSimann::OptimiseLikelihoodREP() { |
1019 |
|
|
1020 |
//set initial values |
//set initial values |
1021 |
|
|
1041 |
EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled |
EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled |
1042 |
if (scale) { |
if (scale) { |
1043 |
EcoSystem->scaleVariables(); |
EcoSystem->scaleVariables(); |
1044 |
#ifdef _OPENMP |
|
1045 |
int numThr = omp_get_max_threads ( ); |
int numThr = omp_get_max_threads ( ); |
1046 |
for(i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread |
for(i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread |
1047 |
EcoSystems[i]->scaleVariables(); |
EcoSystems[i]->scaleVariables(); |
|
#endif |
|
1048 |
} |
} |
1049 |
EcoSystem->getOptScaledValues(x); |
EcoSystem->getOptScaledValues(x); |
1050 |
EcoSystem->getOptLowerBounds(lowerb); |
EcoSystem->getOptLowerBounds(lowerb); |
1089 |
Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns), |
Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns), |
1090 |
tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score); |
tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score); |
1091 |
|
|
1092 |
ReproducibleSearch<Siman, DoubleVector, ControlClass, evaluate_f, buildNewParams_f> |
ReproducibleSearch<Siman, DoubleVector, ControlClass, evaluate_par_f, buildNewParams_f> |
1093 |
pa(s, x, simanniter); |
pa(s, x, simanniter); |
1094 |
|
|
|
#ifdef _OPENMP |
|
1095 |
// OpenMP parallelization |
// OpenMP parallelization |
1096 |
int numThr = omp_get_max_threads ( ); |
int numThr = omp_get_max_threads ( ); |
1097 |
pa.paral_opt_omp(funcval,numThr,numThr); |
pa.paral_opt_omp(funcval,numThr,numThr); |
1098 |
#else |
iters = pa.iterations(); |
1099 |
|
|
1100 |
|
} |
1101 |
|
#endif |
1102 |
|
void OptInfoSimann::OptimiseLikelihood() { |
1103 |
|
|
1104 |
|
//set initial values |
1105 |
|
|
1106 |
|
double tmp, p, pp; |
1107 |
|
double funcval, trialf; |
1108 |
|
int a, i, j, k, l, quit; |
1109 |
|
int rchange, rcheck, rnumber; //Used to randomise the order of the parameters |
1110 |
|
|
1111 |
|
handle.logMessage(LOGINFO, |
1112 |
|
"\nStarting Simulated Annealing optimisation algorithm\n"); |
1113 |
|
int nvars = EcoSystem->numOptVariables(); |
1114 |
|
DoubleVector x(nvars); |
1115 |
|
DoubleVector init(nvars); |
1116 |
|
DoubleVector trialx(nvars, 0.0); |
1117 |
|
DoubleVector bestx(nvars); |
1118 |
|
DoubleVector scalex(nvars); |
1119 |
|
DoubleVector lowerb(nvars); |
1120 |
|
DoubleVector upperb(nvars); |
1121 |
|
DoubleVector fstar(tempcheck); |
1122 |
|
DoubleVector vm(nvars, vminit); |
1123 |
|
IntVector param(nvars, 0); |
1124 |
|
|
1125 |
|
EcoSystem->resetVariables(); //JMB need to reset variables in case they have been scaled |
1126 |
|
if (scale) { |
1127 |
|
EcoSystem->scaleVariables(); |
1128 |
|
} |
1129 |
|
EcoSystem->getOptScaledValues(x); |
1130 |
|
EcoSystem->getOptLowerBounds(lowerb); |
1131 |
|
EcoSystem->getOptUpperBounds(upperb); |
1132 |
|
EcoSystem->getOptInitialValues(init); |
1133 |
|
|
1134 |
|
for (i = 0; i < nvars; i++) { |
1135 |
|
bestx[i] = x[i]; |
1136 |
|
param[i] = i; |
1137 |
|
} |
1138 |
|
|
1139 |
|
if (scale) { |
1140 |
|
for (i = 0; i < nvars; i++) { |
1141 |
|
scalex[i] = x[i]; |
1142 |
|
// Scaling the bounds, because the parameters are scaled |
1143 |
|
lowerb[i] = lowerb[i] / init[i]; |
1144 |
|
upperb[i] = upperb[i] / init[i]; |
1145 |
|
if (lowerb[i] > upperb[i]) { |
1146 |
|
tmp = lowerb[i]; |
1147 |
|
lowerb[i] = upperb[i]; |
1148 |
|
upperb[i] = tmp; |
1149 |
|
} |
1150 |
|
} |
1151 |
|
} |
1152 |
|
|
1153 |
|
//funcval is the function value at x |
1154 |
|
funcval = EcoSystem->SimulateAndUpdate(x); |
1155 |
|
if (funcval != funcval) { //check for NaN |
1156 |
|
handle.logMessage(LOGINFO, |
1157 |
|
"Error starting Simulated Annealing optimisation with f(x) = infinity"); |
1158 |
|
converge = -1; |
1159 |
|
iters = 1; |
1160 |
|
return; |
1161 |
|
} |
1162 |
|
|
1163 |
|
//the function is to be minimised so switch the sign of funcval (and trialf) |
1164 |
|
funcval = -funcval; |
1165 |
|
cs /= lratio; //JMB save processing time |
1166 |
|
for (i = 0; i < tempcheck; i++) |
1167 |
|
fstar[i] = funcval; |
1168 |
|
|
1169 |
|
Siman s(seed, seedM, seedP, nvars, nt, ns, param, &x, &lowerb, &upperb, vm, t, rt, (1.0 / ns), |
1170 |
|
tempcheck, simanneps, fstar, lratio, uratio, cs, &bestx, scale, &converge, &score); |
1171 |
|
|
1172 |
|
ReproducibleSearch<Siman, DoubleVector, ControlClass, evaluate_f, buildNewParams_f> |
1173 |
|
pa(s, x, simanniter); |
1174 |
|
|
1175 |
// sequential code |
// sequential code |
1176 |
pa.seq_opt(funcval); |
pa.seq_opt(funcval); |
|
#endif |
|
1177 |
iters = pa.iterations(); |
iters = pa.iterations(); |
1178 |
|
|
1179 |
} |
} |
1181 |
|
|
1182 |
|
|
1183 |
|
|
1184 |
|
|
1185 |
|
|
1186 |
|
|
1187 |
|
|