28#include <adolc/drivers/drivers.h>
29#include <adolc/adolc_sparse.h>
40#define CONSTRAIN_START_LOOP(pVector,cn) \
41 for (uint r1=0;r1<l2r.size();r1++){ \
42 for (uint r2=0;r2<l2r[r1].size();r2++){ \
43 for (uint p=0;p<(pVector).size();p++){ \
44 int psec = p+nPriPr; \
45 cix = gix((cn), r1, r2, p);
46#define CONSTRAIN_END_LOOP \
52typedef std::map<string, endvar>
VarMap;
53typedef std::pair<std::string, endvar >
VarPair;
89 mkeq2.
comment=
"[h1] Conservation of matters of transformed products";
96 mkeq3.
comment=
"[h2] Conservation of matters of raw products";
103 mkeq4.
comment=
"[eq 13] Leontief transformation function";
109 mkeq5.
comment=
"[eq 21] Raw product supply function";
115 mkeq6.
comment=
"[eq 20] Trasformed products demand function";
121 mkeq7.
comment=
"[h7 and h3] Transformed products import function";
127 mkeq8.
comment=
"[h8 and h4] Raw products export function";
132 mkeq13.
name=
"mkeq13";
133 mkeq13.
comment=
"[h9] Calculation of the composite price of transformed products (PPC_Dp)";
138 mkeq14.
name=
"mkeq14";
139 mkeq14.
comment=
"[h10] Calculation of the composite price of raw products (PPC_Sw)";
144 mkeq17.
name=
"mkeq17";
145 mkeq17.
comment=
"[h16] Constrain of the transformaton supply (lower than the regional maximal production capacity)";
151 mkeq23.
name=
"mkeq23";
152 mkeq23.
comment=
"[h3] Composit demand eq. (Dp)";
157 mkeq24.
name=
"mkeq24";
158 mkeq24.
comment=
"[h4] Composite supply eq. (Sw)";
163 mkeq26.
name=
"mkeq26";
164 mkeq26.
comment=
"[eq ] Verification of the null transport agents supply";
169 mkeq25.
name=
"mkeq25";
170 mkeq25.
comment=
"Verification of the null trasformers supply (price of raw product + trasf product > trasf product)";
175 mkeq18.
name=
"mkeq18";
176 mkeq18.
comment=
"Constrain on raw material supply (lower than inventory)";
181 resbounds.
name=
"resbounds";
182 resbounds.
comment=
"Constrain on raw material supply (lower than inventory, for each possible combination of primary products)";
194 cons.push_back(mkeq2);
195 cons.push_back(mkeq6);
196 cons.push_back(mkeq7);
197 cons.push_back(mkeq13);
198 cons.push_back(mkeq23);
199 cons.push_back(mkeq3);
200 cons.push_back(mkeq4);
201 cons.push_back(mkeq5);
202 cons.push_back(mkeq8);
203 cons.push_back(mkeq14);
204 cons.push_back(mkeq24);
205 cons.push_back(mkeq17);
206 cons.push_back(mkeq26);
207 cons.push_back(mkeq25);
209 cons.push_back(resbounds);
219template<
class T>
bool
222 double aa, bb, dc0, sigma, a_pr, ct, m, zeromax,supCorr2;
226 for (uint r1=0;r1<
l2r.size();r1++){
227 for (uint r2=0;r2<
l2r[r1].size();r2++){
239 for (uint p=0;p<
secPr.size();p++){
244 obj_value += aa*pow(
mymax(zeromax,x[
gix(
"dc",r1,r2,p)]),(sigma+1)/sigma)-x[
gix(
"pc",r1,r2,p+
nPriPr)]*x[
gix(
"dc",r1,r2,p)];
252 for (uint p=0;p<
priPr.size();p++){
256 obj_value += x[
gix(
"pc",r1,r2,p)]*x[
gix(
"sc",r1,r2,p)] - bb*pow(
mymax(zeromax,x[
gix(
"sc",r1,r2,p)]),((sigma+1)/sigma));
264 for (uint p1=0;p1<
priPr.size();p1++){
265 for (uint p2=0;p2<
priPr.size();p2++){
269 obj_value += x[
gix(
"pc",r1,r2,p2)]*a_pr*x[
gix(
"sc",r1,r2,p1)]-bb*pow(
mymax(zeromax,a_pr*x[
gix(
"sc",r1,r2,p1)]),(sigma+1)/sigma);
274 for (uint p=0;p<
allPr.size();p++){
275 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
277 obj_value += (x[
gix(
"pl",r1,r2To,p)]-x[
gix(
"pl",r1,r2,p)]-ct)*x[
gix(
"rt",r1,r2,p,r2To)];
283 for (uint p=0;p<
secPr.size();p++){
288 for (uint p=0;p<
priPr.size();p++){
289 obj_value -= x[
gix(
"pl",r1,r2,p)]*x[
gix(
"dl",r1,r2,p)];
310template<
class T>
bool
313 double a_pr, a, sigma, ff, pol_mktDirInt_s, pol_mktDirInt_d, pol_mktDirInt_d_pSubstituted, pol_mktDirInt_d_1, pol_mktDirInt_d_1_pSubstituted, gg, q1, p1v, t1, r1v, psi, eta, pworld, ct, k, dispor, mv, in, in_1, supCorr, es_d, pc_1, pc_pSubstituted, pc_1_pSubstituted, additionalDemand;
320 g[cix] = x[
gix(
"dl",r1,r2,psec)]-x[
gix(
"sl",r1,r2,psec)];
321 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
322 g[cix] += x[
gix(
"rt",r1,r2,psec,r2To)]-x[
gix(
"rt",r1,r2To,psec,r2)];
333 pol_mktDirInt_d =
gpd(
"pol_mktDirInt_d",
l2r[r1][r2],
secPr[p]);
335 additionalDemand =
gpd(
"additionalDemand",
l2r[r1][r2],
secPr[p]);
336 g[cix] = - gg*pow(x[
gix(
"pc",r1,r2,psec)]*pol_mktDirInt_d,sigma);
337 vector<string> debug =
secPr;
339 for (uint p2=0;p2<
secPr.size();p2++){
343 pol_mktDirInt_d_pSubstituted =
gpd(
"pol_mktDirInt_d",
l2r[r1][r2],
secPr[p2]);
348 ((x[
gix(
"pc",r1,r2,psec)]*pol_mktDirInt_d) / (x[
gix(
"pc",r1,r2,
priPr.size()+p2)]*pol_mktDirInt_d_pSubstituted))
350 ((pc_1*pol_mktDirInt_d_1) / (pc_1_pSubstituted*pol_mktDirInt_d_1_pSubstituted))
356 for (uint p3=0;p3<
othPr.size();p3++){
364 (x[
gix(
"pc",r1,r2,psec)]*pol_mktDirInt_d / pc_pSubstituted)
366 (pc_1*pol_mktDirInt_d_1 / pc_1_pSubstituted)
372 g[cix] += x[
gix(
"dc",r1,r2,p)]-additionalDemand;
382 g[cix] = x[
gix(
"da",r1,r2,p)]/x[
gix(
"dl",r1,r2,psec)] - pow((q1*x[
gix(
"pl",r1,r2,psec)])/(p1v*pworld),psi);
388 g[cix] = x[
gix(
"pc",r1,r2,psec)]*x[
gix(
"dc",r1,r2,p)]-x[
gix(
"dl",r1,r2,psec)]*x[
gix(
"pl",r1,r2,psec)]-x[
gix(
"da",r1,r2,p)]*pworld;
396 g[cix] = x[
gix(
"dc",r1,r2,p)] -
398 q1 * pow(x[
gix(
"da",r1,r2,p)],(psi-1)/psi)
399 + p1v * pow(x[
gix(
"dl",r1,r2,psec)],(psi-1)/psi),
407 g[cix] = x[
gix(
"dl",r1,r2,p)]-x[
gix(
"sl",r1,r2,p)];
408 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
409 g[cix] += x[
gix(
"rt",r1,r2,p,r2To)]-x[
gix(
"rt",r1,r2To,p,r2)];
411 for (uint p2=0;p2<
priPr.size();p2++){
413 g[cix] -= a_pr*x[
gix(
"sl",r1,r2,p2)];
419 g[cix] = x[
gix(
"dl",r1,r2,p)];
420 for (uint p2=0;p2<
secPr.size();p2++){
422 g[cix] -= a*x[
gix(
"sl",r1,r2,p2+
nPriPr)];
433 pol_mktDirInt_s =
gpd(
"pol_mktDirInt_s",
l2r[r1][r2],
priPr[p]);
439 double Pct = carbonPrice*intRate;
440 double omega =
gpd(
"co2content_products",
l2r[r1][r2],
priPr[p])/1000;
443 g[cix] = x[
gix(
"sc",r1,r2,p)]-ff*pow((x[
gix(
"pc",r1,r2,p)]-omega*Pct)*pol_mktDirInt_s,sigma);
456 g[cix] = x[
gix(
"sa",r1,r2,p)]/x[
gix(
"sl",r1,r2,p)] - pow((t1*x[
gix(
"pl",r1,r2,p)])/(r1v*pworld),eta);
462 g[cix] = x[
gix(
"pc",r1,r2,p)]*x[
gix(
"sc",r1,r2,p)]-x[
gix(
"sl",r1,r2,p)]*x[
gix(
"pl",r1,r2,p)]-x[
gix(
"sa",r1,r2,p)]*pworld;
470 g[cix] = x[
gix(
"sc",r1,r2,p)] -
472 t1 * pow(x[
gix(
"sa",r1,r2,p)],(eta-1)/eta)
473 + r1v * pow(x[
gix(
"sl",r1,r2,p)],(eta-1)/eta),
486 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
487 cix =
gix(12, r1, r2, p,r2To);
489 g[cix] = (x[
gix(
"pl",r1,r2To,p)]-x[
gix(
"pl",r1,r2,p)]-ct);
496 g[cix] = mv - x[
gix(
"pl",r1,r2,p+
nPriPr)];
497 for (uint p2=0;p2<
priPr.size();p2++){
499 g[cix] += a * x[
gix(
"pl",r1,r2,p2)];
552 Index& nnz_h_lag, IndexStyleEnum& index_style){
572 for(uint i=0;i<l1regIds.size();i++){
573 std::vector<int> l2ChildrenIds;
575 std::vector<ModelRegion*> l2Childrens = l1Region->
getChildren(
true);
576 for(uint j=0;j<l2Childrens.size();j++){
577 l2ChildrenIds.push_back(l2Childrens[j]->getRegId());
579 if(l2ChildrenIds.size()){
580 l2r.push_back(l2ChildrenIds);
627 index_style = C_STYLE;
637 for (Index i=0; i<n; i++) {
643 for (Index i=0; i<m; i++) {
665 Index m,
bool init_lambda, Number* lambda){
676 assert(init_x ==
true);
677 assert(init_z ==
false);
678 assert(init_lambda ==
false);
680 VarMap::iterator viter;
683 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
685 int vdomtype = viter->second.domain;
687 for(uint r1=0;r1<
l2r.size();r1++){
688 for(uint r2=0;r2<
l2r[r1].size();r2++){
689 for(uint p=0;p<
priPr.size();p++){
695 for(uint r1=0;r1<
l2r.size();r1++){
696 for(uint r2=0;r2<
l2r[r1].size();r2++){
697 for(uint p=0;p<
secPr.size();p++){
703 for(uint r1=0;r1<
l2r.size();r1++){
704 for(uint r2=0;r2<
l2r[r1].size();r2++){
705 for(uint p=0;p<
allPr.size();p++){
711 for(uint r1=0;r1<
l2r.size();r1++){
712 for(uint r2=0;r2<
l2r[r1].size();r2++){
713 for(uint p=0;p<
allPr.size();p++){
714 for(uint r2To=0;r2To<
l2r[r1].size();r2To++){
733 Index n,
const Number* x,
const Number* z_L,
const Number* z_U,
734 Index m,
const Number* g,
const Number* lambda,
735 Number obj_value,
const IpoptData* ip_data, IpoptCalculatedQuantities* ip_cq){
737 printf(
"\n\nObjective value\n");
738 printf(
"f(x*) = %e\n", obj_value);
742 VarMap::iterator viter;
745 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
747 int vdomtype = viter->second.domain;
749 for(uint r1=0;r1<
l2r.size();r1++){
750 for(uint r2=0;r2<
l2r[r1].size();r2++){
751 for(uint p=0;p<
priPr.size();p++){
752 spd(x[
gix(viter->first,r1,r2,p)],viter->first,
l2r[r1][r2],
priPr[p]);
757 for(uint r1=0;r1<
l2r.size();r1++){
758 for(uint r2=0;r2<
l2r[r1].size();r2++){
759 for(uint p=0;p<
secPr.size();p++){
760 spd(x[
gix(viter->first,r1,r2,p)],viter->first,
l2r[r1][r2],
secPr[p]);
766 for(uint r1=0;r1<
l2r.size();r1++){
767 for(uint r2=0;r2<
l2r[r1].size();r2++){
768 for(uint p=0;p<
allPr.size();p++){
769 spd(x[
gix(viter->first,r1,r2,p)],viter->first,
l2r[r1][r2],
allPr[p]);
774 for(uint r1=0;r1<
l2r.size();r1++){
775 for(uint r2=0;r2<
l2r[r1].size();r2++){
776 for(uint p=0;p<
allPr.size();p++){
777 for(uint r2To=0;r2To<
l2r[r1].size();r2To++){
781 spd(x[
gix(viter->first,r1,r2,p,r2To)],viter->first,
l2r[r1][r2],
allPr[p],
DATA_NOW,
false,
i2s(
l2r[r1][r2To]));
805 for (
int i=0;i<n+m+1;i++) {
822Opt::eval_f(Index n,
const Number* x,
bool new_x, Number& obj_value){
831 gradient(
tag_f,n,x,grad_f);
837Opt::eval_g(Index n,
const Number* x,
bool new_x, Index m, Number* g){
846 Index* iRow, Index *jCol, Number* values){
847 if (values == NULL) {
850 for(Index idx=0; idx<
nnz_jac; idx++)
861 for(Index idx=0; idx<
nnz_jac; idx++)
863 values[idx] =
jacval[idx];
871Opt::eval_h(Index n,
const Number* x,
bool new_x, Number obj_factor, Index m,
const Number* lambda,
872 bool new_lambda, Index nele_hess, Index* iRow, Index* jCol, Number* values){
876 if (values == NULL) {
880 for(Index idx=0; idx<
nnz_L; idx++)
890 for(Index idx = 0; idx<n ; idx++)
892 for(Index idx = 0; idx<m ; idx++)
893 x_lam[n+idx] = lambda[idx];
894 x_lam[n+m] = obj_factor;
899 for(Index idx_total = 0; idx_total <
nnz_L_total ; idx_total++)
903 values[idx] =
hessval[idx_total];
920 Number *xp =
new double[n];
921 Number *lamp =
new double[m];
922 Number *zl =
new double[m];
923 Number *zu =
new double[m];
925 adouble *xa =
new adouble[n];
926 adouble *g =
new adouble[m];
927 adouble *lam =
new adouble[m];
936 x_lam =
new double[n+m+1];
945 for(Index idx=0;idx<n;idx++)
956 for(Index idx=0;idx<n;idx++)
962 for(Index idx=0;idx<m;idx++)
969 for(Index idx=0;idx<n;idx++)
971 for(Index idx=0;idx<m;idx++)
980 for(Index idx=0;idx<m;idx++)
981 obj_value += g[idx]*lam[idx];
1005 unsigned int **JP_f=NULL;
1006 unsigned int **JP_g=NULL;
1007 unsigned int **HP_f=NULL;
1008 unsigned int **HP_g=NULL;
1009 unsigned int *HP_length=NULL;
1010 unsigned int *temp=NULL;
1014 JP_f = (
unsigned int **) malloc(
sizeof(
unsigned int*));
1015 JP_g = (
unsigned int **) malloc(m*
sizeof(
unsigned int*));
1016 HP_f = (
unsigned int **) malloc(n*
sizeof(
unsigned int*));
1017 HP_g = (
unsigned int **) malloc(n*
sizeof(
unsigned int*));
1018 HP_t = (
unsigned int **) malloc((n+m+1)*
sizeof(
unsigned int*));
1019 HP_length = (
unsigned int *) malloc((n)*
sizeof(
unsigned int));
1022 hess_pat(
tag_f, n, xp, HP_f, ctrl_H);
1024 indopro_forward_safe(
tag_f, 1, n, xp, JP_f);
1025 indopro_forward_safe(
tag_g, m, n, xp, JP_g);
1026 nonl_ind_forward_safe(
tag_g, m, n, xp, HP_g);
1030 if (HP_f[i][0]+HP_g[i][0]!=0)
1034 HP_t[i] = (
unsigned int *) malloc((HP_g[i][0]+
HPOFF)*
sizeof(
unsigned int));
1035 for(j=0;j<=(int) HP_g[i][0];j++)
1037 HP_t[i][j] = HP_g[i][j];
1039 HP_length[i] = HP_g[i][0]+
HPOFF;
1045 HP_t[i] = (
unsigned int *) malloc((HP_f[i][0]+
HPOFF)*
sizeof(
unsigned int));
1046 for(j=0;j<=(int) HP_f[i][0];j++)
1048 HP_t[i][j] = HP_f[i][j];
1050 HP_length[i] = HP_f[i][0]+
HPOFF;
1054 HP_t[i] = (
unsigned int *) malloc((HP_f[i][0]+HP_g[i][0]+
HPOFF)*
sizeof(
unsigned int));
1056 while ((k<=(
int) HP_f[i][0]) && (l <= (
int) HP_g[i][0]))
1058 if (HP_f[i][k] < HP_g[i][l])
1060 HP_t[i][j]=HP_f[i][k];
1065 if (HP_f[i][k] == HP_g[i][l])
1067 HP_t[i][j]=HP_f[i][k];
1072 HP_t[i][j]=HP_g[i][l];
1079 for(ii=k;ii<=(int) HP_f[i][0];ii++)
1081 HP_t[i][j] = HP_f[i][ii];
1086 for(ii=l;ii<=(int) HP_g[i][0];ii++)
1088 HP_t[i][j] = HP_g[i][ii];
1095 HP_length[i] = HP_f[i][0]+HP_g[i][0]+
HPOFF;
1099 HP_t[i] = (
unsigned int *) malloc((
HPOFF+1)*
sizeof(
unsigned int));
1118 HP_t[n+i] = (
unsigned int *) malloc((JP_g[i][0]+1)*
sizeof(
unsigned int));
1119 HP_t[n+i][0]=JP_g[i][0];
1123 for(j=1;j<= (int) JP_g[i][0];j++)
1125 HP_t[n+i][j]=JP_g[i][j];
1130 if (HP_length[JP_g[i][j]] <=
HP_t[JP_g[i][j]][0]+1)
1137 temp = (
unsigned int *) malloc((
HP_t[JP_g[i][j]][0]+1)*
sizeof(
unsigned int));
1138 for(l=0;l<=(int)
HP_t[JP_g[i][j]][0];l++)
1140 temp[l] =
HP_t[JP_g[i][j]][l];
1154 unsigned int machin = JP_g[i][j];
1158 HP_t[JP_g[i][j]] = (
unsigned int *) malloc(2*HP_length[JP_g[i][j]]*
sizeof(
unsigned int));
1159 HP_length[JP_g[i][j]] = 2*HP_length[JP_g[i][j]];
1162 for(l=0;l<=(int)temp[0];l++)
1163 HP_t[JP_g[i][j]][l] =temp[l];
1169 HP_t[JP_g[i][j]][0] =
HP_t[JP_g[i][j]][0]+1;
1170 HP_t[JP_g[i][j]][
HP_t[JP_g[i][j]][0]] = i+n;
1175 for(j=1;j<= (int) JP_f[0][0];j++)
1177 if (HP_length[JP_f[0][j]] <=
HP_t[JP_f[0][j]][0]+1)
1179 temp = (
unsigned int *) malloc((
HP_t[JP_f[0][j]][0])*
sizeof(
unsigned int));
1180 for(l=0;l<=(int)
HP_t[JP_f[0][j]][0];l++)
1181 temp[l] =
HP_t[JP_f[0][j]][l];
1182 free(
HP_t[JP_f[0][j]]);
1183 HP_t[JP_f[0][j]] = (
unsigned int *) malloc(2*HP_length[JP_f[0][j]]*
sizeof(
unsigned int));
1184 HP_length[JP_f[0][j]] = 2*HP_length[JP_f[0][j]];
1185 for(l=0;l<=(int)temp[0];l++)
1186 HP_t[JP_f[0][j]][l] =temp[l];
1189 HP_t[JP_f[0][j]][0] =
HP_t[JP_f[0][j]][0]+1;
1190 HP_t[JP_f[0][j]][
HP_t[JP_f[0][j]][0]] = n+m;
1193 HP_t[n+m] = (
unsigned int *) malloc((JP_f[0][0]+2)*
sizeof(
unsigned int));
1194 HP_t[n+m][0]=JP_f[0][0]+1;
1195 for(j=1;j<= (int) JP_f[0][0];j++)
1196 HP_t[n+m][j]=JP_f[0][j];
1197 HP_t[n+m][JP_f[0][0]+1]=n+m;
1204 for (j=1;j<=(int)
HP_t[i][0];j++)
1205 if ((
int)
HP_t[i][j] <= i)
1226 unsigned int ind = 0;
1228 for (
int i=0;i<n;i++)
1229 for (
unsigned int j=1;j<=
HP_t[i][0];j++)
1231 if (((
int)
HP_t[i][j]>=i) &&((
int)
HP_t[i][j]<n))
1239 for (
int i=0;i<n+m+1;i++)
1240 for (
unsigned int j=1;j<=
HP_t[i][0];j++)
1242 if ((
int)
HP_t[i][j]>=i)
1275 map<string, int>::const_iterator p;
1281 msgOut(
MSG_CRITICAL_ERROR,
"Asking the initial position in the concatenated array of a variable ("+varName+
") that doesn't exist!");
1293 VarMap::const_iterator p;
1294 p=
vars.find(varName);
1295 if(p !=
vars.end()) {
1296 return p->second.domain;
1306 return cons.at(cn).domain;
1309template<
class T>
const int
1314 int dType =
gdt(v_or_c);
1315 int othCountriesRegions = 0;
1316 int othCountriesRegions_r2case = 0;
1317 for (uint i=0;i<r1Ix;i++){
1318 othCountriesRegions +=
l2r[i].size();
1320 for (uint i=0;i<r1Ix;i++){
1321 othCountriesRegions_r2case +=
l2r[i].size()*
l2r[i].size();
1326 return gip(v_or_c)+(othCountriesRegions+r2Ix)*
nPriPr+prIx;
1328 return gip(v_or_c)+(othCountriesRegions+r2Ix)*
nSecPr+prIx;;
1330 return gip(v_or_c)+(othCountriesRegions+r2Ix)*
nAllPr+prIx;
1332 return gip(v_or_c)+(othCountriesRegions_r2case)*
nAllPr+(r2Ix*
nPriPr+prIx)*
l2r[r1Ix].size()+r2IxTo;
1334 return gip(v_or_c)+(othCountriesRegions_r2case)*
nAllPr+(r2Ix*
nSecPr+prIx)*
l2r[r1Ix].size()+r2IxTo;
1336 return gip(v_or_c)+(othCountriesRegions_r2case)*
nAllPr+(r2Ix*
nAllPr+prIx)*
l2r[r1Ix].size()+r2IxTo;
1351Opt::gix(
const string &varName,
const int& r1Ix,
const int& r2Ix,
const int& prIx,
const int& r2IxTo)
const{
1353 map <string, vector < vector < vector < vector <int> > > > >::const_iterator p;
1356 return p->second[r1Ix][r2Ix][prIx][r2IxTo];
1365Opt::gix(
const int &cn,
const int& r1Ix,
const int& r2Ix,
const int& prIx,
const int& r2IxTo)
const{
1366 return cpositions[cn][r1Ix][r2Ix][prIx][r2IxTo];
1371 int vInitialPosition = 0;
1372 int cInitialPosition = 0;
1373 VarMap::iterator viter;
1374 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
1375 initPos.insert(pair<string, int>(viter->first, vInitialPosition));
1376 initPos_rev.insert(pair<int, string>(vInitialPosition,viter->first));
1379 for (uint i=0;i<
cons.size();i++){
1380 cInitPos.push_back(cInitialPosition);
1389 VarMap::iterator viter;
1390 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
1391 vpositions.insert(pair<
string, vector < vector < vector < vector <int> > > > >(viter->first,
buildPositionVector(viter->first, viter->second.domain)));
1394 for (uint i=0; i<
cons.size();i++){
1400template<
class T> vector < vector < vector < vector <int> > > >
1406 pVectorSize=
priPr.size();
1409 pVectorSize=
secPr.size();
1412 pVectorSize=
allPr.size();
1415 pVectorSize=
priPr.size();
1418 pVectorSize=
secPr.size();
1421 pVectorSize=
allPr.size();
1424 pVectorSize=
allPr.size();
1434 vector < vector < vector < vector <int> > > > positionsToAdd;
1435 for(uint r1=0;r1<
l2r.size();r1++){
1436 vector < vector < vector <int> > > dim1;
1437 for(uint r2=0;r2<
l2r[r1].size();r2++){
1438 vector < vector <int> > dim2;
1439 for(uint p=0;p<pVectorSize;p++){
1441 for(uint r2To=0;r2To<
l2r[r1].size();r2To++){
1444 dim2.push_back(dim3);
1446 dim1.push_back(dim2);
1448 positionsToAdd.push_back(dim1);
1450 return positionsToAdd;
1458 VarMap::iterator viter;
1459 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
1468 for(uint i=0;i<
cons.size();i++){
1498 for(uint r1=0;r1<
l2r.size();r1++){
1503 for(uint r1=0;r1<
l2r.size();r1++){
1508 for(uint r1=0;r1<
l2r.size();r1++){
1523 for(uint i=0;i<
cons.size();i++){
1524 if(i!=
cons.size()-1){
1525 if (idx >=
gip(i) && idx <
gip(i+1)){
1526 return cons[i].direction;
1530 return cons[i].direction;
1540 map <int, string>::const_iterator p;
1543 VarMap::const_iterator p2;
1544 p2=
vars.find(p->second);
1545 if(p2 !=
vars.end()) {
1547 if (p2->second.l_bound_var ==
""){
1548 return p2->second.l_bound;
1552 }
else if (bound_type==
UBOUND){
1553 if (p2->second.u_bound_var ==
""){
1554 return p2->second.u_bound;
1593 for(uint i=0;i<
cons.size();i++){
1594 if(i!=
cons.size()-1){
1595 if (idx >=
gip(i) && idx <
gip(i+1)){
1609Opt::unpack(
int ix_h,
int domain,
int initial,
int &r1_h,
int &r2_h,
int&p_h,
int&r2to_h,
bool fullp){
1610 ix_h = ix_h-initial;
1612 bool r2flag =
false;
1613 int pIndexToAdd = 0;
1622 r1_h=0;r2_h=0;p_h=0;r2to_h=0;
1635 for (uint r1=0;r1<
l2r.size();r1++){
1636 for (uint r2=0;r2<
l2r[r1].size();r2++){
1637 for (uint p=0;p<np;p++){
1648 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
1667 for(uint i=0;i<
cons.size();i++){
1682 unsigned int **jacpat=NULL;
1690 jacpat =
new unsigned int* [
nCons];
1691 x =
new double[
nVar];
1697 for (
int i=0;i<
nCons;i++) {
1698 for (
int j=1;j<=jacpat[i][0];j++){
1699 vector <int> nzjelement;
1700 nzjelement.push_back(i);
1701 nzjelement.push_back(jacpat[i][j]);
1710 unsigned int **hesspat=NULL;
1715 hesspat =
new unsigned int* [(
nVar+
nCons+1)];
1720 for (
int i=0;i<(
nVar);i++) {
1721 for (
int j=1;j<=hesspat[i][0];j++){
1722 if(hesspat[i][j]<=i){
1723 vector <int> nzhelement;
1724 nzhelement.push_back(i);
1725 nzhelement.push_back(hesspat[i][j]);
1735 cout <<
"Num of variables: " <<
nVar <<
" - Num of constrains:" <<
nCons << endl;
1736 cout <<
"IDX;ROW;COL" << endl;
1741 cout <<
"Dense jacobian: " <<
nCons *
nVar <<
" elements" << endl;
1742 cout <<
"Dense hessian: " <<
nVar*(
nVar-1)/2+
nVar <<
" elements" << endl;
1759Opt::intermediate_callback(AlgorithmMode mode, Index iter, Number obj_value, Number inf_pr, Number inf_du, Number mu, Number d_norm, Number regularization_size, Number alpha_du, Number alpha_pr, Index ls_trials,
const IpoptData *ip_data, IpoptCalculatedQuantities *ip_cq){
1760 int itnumber = iter;
1791Opt::declareVariable(
const string &name,
const int & domain,
const string &desc,
const double & l_bound,
const double & u_bound,
const string & l_bound_var,
const string & u_bound_var){
1793 end_var.
name = name;
1800 vars.insert(std::pair<std::string, endvar >(name, end_var));
1849 vector < vector < vector <double> > > in_temp;
1850 for (uint r1=0;r1<
l2r.size();r1++){
1851 vector < vector <double> > dim1;
1852 for (uint r2=0;r2<
l2r[r1].size();r2++){
1853 vector <double> dim2;
1857 dim2.push_back(this_in);
1859 dim1.push_back(dim2);
1861 in_temp.push_back(dim1);
@ DATA_NOW
The required data is for the current year.
@ MSG_CRITICAL_ERROR
Print an error message and stop the model.
@ MSG_DEBUG
Print a debug message, normally filtered out.
@ MSG_INFO
Print an INFO message.
@ DOM_PRI_PR
Primary products // domain of variables and constrains: primary, secondary, all products or all produ...
@ DOM_R2_SEC_PR
Secondary products over r2 couple regions (in-country commercial flows)
@ DOM_SEC_PR
Secondary products.
@ DOM_SCALAR
Scalar variable (not used)
@ DOM_R2_PRI_PR
Primary products over r2 couple regions (in-country commercial flows)
@ DOM_R2_ALL_PR
All products over r2 couple regions (in-country commercial flows)
@ DOM_PRI_PR_ALLCOMBS
All possible combinations of primary products (2^ number of primary products)
@ DOM_ALL_PR
All products (primary+secondary)
std::map< string, endvar > VarMap
#define CONSTRAIN_START_LOOP(pVector, cn)
std::pair< std::string, endvar > VarPair
#define CONSTRAIN_END_LOOP
ThreadManager * MTHREAD
Pointer to the Thread manager.
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
string i2s(const int &int_h) const
integer to string conversion
double getDoubleSetting(const string &name_h, int position=0, int reg=WORLD) const
vector< vector< int > > createCombinationsVector(const int &nItems)
Return a vector containing any possible combination of nItems items (including any possible subset)....
vector< string > getStringVectorSetting(const string &name_h, int reg=WORLD) const
vector< int > getRegionIds(int level_h, bool excludeResidual=true)
ModelRegion * getRegion(int regId_h)
int getIntSetting(const string &name_h, int position=0, int reg=WORLD) const
vector< ModelRegion * > getChildren(bool excludeResidual=true)
Returns a pointer to the parent regions.
vector< double > inResByAnyCombination
Vector of inventory resource for each possible combination of primary products. This store both alive...
int nLowerEqualZeroConstrains
const double gpd(const string &type_h, const int ®Id_h, const string &prodId_h, const int &year=DATA_NOW, const string &freeDim_h="") const
map< string, vector< vector< vector< vector< int > > > > > vpositions
cached position in the concatenated vector for each variables. Dimensions are l1reg,...
const int gix(const string &varName, const int &r1Ix, const int &r2Ix, const int &prIx, const int &r2IxTo=0) const
Get the index in the concatenated array gived a certain var name, the reg lev1 index,...
vector< vector< int > > priPrCombs
A vector with all the possible combinations of primary products.
Opt(ThreadManager *MTHREAD_h)
Constructor.
vector< vector< vector< double > > > ins
A copy of the inventoried resourses by region and primary product combination. It works also with dyn...
int nGreaterEqualZeroConstrains
constrain * getConstrainByIndex(int idx)
void declareVariables()
declare the variables, their domains and their bounds
unsigned int * cind_L_total
int getDomainElements(int domain)
return the number of elements of a domain
vector< vector< int > > l2r
void calculateSparsityPatternH()
virtual bool intermediate_callback(AlgorithmMode mode, Index iter, Number obj_value, Number inf_pr, Number inf_du, Number mu, Number d_norm, Number regularization_size, Number alpha_du, Number alpha_pr, Index ls_trials, const IpoptData *ip_data, IpoptCalculatedQuantities *ip_cq)
bool eval_obj(Index n, const T *x, T &obj_value)
virtual bool get_bounds_info(Index n, Number *x_l, Number *x_u, Index m, Number *g_l, Number *g_u)
map< int, string > initPos_rev
A map with the name of the variable keyed by its initial position in the index.
void cacheInitialPosition()
cache the initial positions of the variables and the constrains
double getBoundByIndex(const int &bound_type, const int &idx)
Return the bound of a given variable (by index)
int getConstrainDirectionByIndex(int idx)
Return the direction of a given constrain.
const int gip(const string &varName) const
Get the initial index position of a given variable in the concatenated array.
virtual bool eval_f(Index n, const Number *x, bool new_x, Number &obj_value)
double overharvestingAllowance
Allows to harvest more than the resources available. Useful when resources got completelly exausted a...
map< string, int > initPos
A map that returns the initial index position in the concatenated array for each variable.
unsigned int * rind_L_total
virtual bool get_starting_point(Index n, bool init_x, Number *x, bool init_z, Number *z_L, Number *z_U, Index m, bool init_lambda, Number *lambda)
void cachePositions()
cache the exact position index (initial+f(r1,r2,p,r2To) for each variable and constrain
virtual bool get_nlp_info(Index &n, Index &m, Index &nnz_jac_g, Index &nnz_h_lag, IndexStyleEnum &index_style)
virtual bool eval_jac_g(Index n, const Number *x, bool new_x, Index m, Index nele_jac, Index *iRow, Index *jCol, Number *values)
void declareVariable(const string &name, const int &domain, const string &desc="", const double &l_bound=0.0, const double &u_bound=UBOUND_MAX, const string &l_bound_var="", const string &u_bound_var="")
Declare a single variable, its domain and its bounds.
vector< vector< vector< vector< int > > > > buildPositionVector(const T &v_or_c, int dType)
build the matrix of the positions for a given variable or contrain
virtual bool eval_g(Index n, const Number *x, bool new_x, Index m, Number *g)
void calculateNumberVariablesConstrains()
calculate the number of variables and constrains
void copyInventoryResourses()
Copy the inventoried resources in the in vector for better performances.
void calculateSparsityPatternJ()
vector< vector< Index > > nzhelements
nzero elements for the hessian matrix
map< string, endvar > vars
List of variables in the model and their domain: pr product, sec prod, all products or all products o...
int getConNumber(constrain *con)
Return the position in the cons vector.
bool eval_constraints(Index n, const T *x, Index m, T *g)
int getVarInstances(const string &varName)
build the matrix of the positions for a given variable or contrain
void unpack(int ix_h, int domain, int initial, int &r1_h, int &r2_h, int &p_h, int &r2to_h, bool fullp=false)
Return the dimensions given a certain index, domain type and initial position.
virtual void finalize_solution(SolverReturn status, Index n, const Number *x, const Number *z_L, const Number *z_U, Index m, const Number *g, const Number *lambda, Number obj_value, const IpoptData *ip_data, IpoptCalculatedQuantities *ip_cq)
virtual bool eval_h(Index n, const Number *x, bool new_x, Number obj_factor, Index m, const Number *lambda, bool new_lambda, Index nele_hess, Index *iRow, Index *jCol, Number *values)
const int gdt(const string &varName)
Get the domain type of a given variable.
void spd(const double &value_h, const string &type_h, const int ®Id_h, const string &prodId_h, const int &year=DATA_NOW, const bool &allowCreate=false, const string &freeDim_h="") const
virtual void generate_tapes(Index n, Index m, Index &nnz_jac_g, Index &nnz_h_lag)
vector< int > cInitPos
A vector that returns the initial index position in the concatenated array for each constrain.
void declareConstrains()
declare the constrains, their domain, their direction and their associated evaluation function
double getDetailedBoundByVarAndIndex(const endvar &var, const int &idx, const int &bType)
Return the bound of a given variable given the variable and the required index. Called by getBoundByI...
const int gix_uncached(const T &v_or_c, int r1Ix, int r2Ix, int prIx, int r2IxTo=0)
Get the index in the concatenated array gived a certain var name (string) or constrain index (int),...
vector< vector< vector< vector< vector< int > > > > > cpositions
cached position in the concatenated vector for each variables. Dimensions are contrain number,...
const Number & mymax(const Number &a, const Number &b)
virtual bool eval_grad_f(Index n, const Number *x, bool new_x, Number *grad_f)
vector< vector< Index > > nzjelements
nzero elements for the jacobian matrix. nzelements[i][0] -> row (constrain), nzelements[i][1] -> colu...
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Scheduler * SCD
the scheduler object (simulation-loops scheduler)
ModelData * MD
the model data object
string l_bound_var
A variable giving the lower bound. If present, the value defined in the variable overrides l_bound.
double u_bound
A fixed numerical upper bound for all the domain.
string u_bound_var
A variable giving the upper bound. If present, the value defined in the variable overrides u_bound.
string desc
Description of the variable.
double l_bound
A fixed numerical lower bound for all the domain.