58 {
59
60
61
62
63
64
65
66
67
68 declareVariable(
"da",
DOM_SEC_PR,
"Demand from abroad (imports)");
69 declareVariable(
"dc",
DOM_SEC_PR,
"Demand, composite");
70 declareVariable(
"dl",
DOM_ALL_PR,
"Demand from local");
71 declareVariable(
"pc",
DOM_ALL_PR,
"Price, composite");
72 declareVariable(
"pl",
DOM_ALL_PR,
"Price, local") ;
74 declareVariable(
"sa",
DOM_PRI_PR,
"Supply to abroad (exports)");
75 declareVariable(
"sc",
DOM_PRI_PR,
"Supply, composite");
76 declareVariable(
"sl",
DOM_ALL_PR,
"Supply to locals");
77
78}
79
80
81
82void
84
85
88 mkeq2.
comment=
"[h1] Conservation of matters of transformed products";
91
92
95 mkeq3.
comment=
"[h2] Conservation of matters of raw products";
98
99
102 mkeq4.
comment=
"[eq 13] Leontief transformation function";
105
108 mkeq5.
comment=
"[eq 21] Raw product supply function";
111
114 mkeq6.
comment=
"[eq 20] Trasformed products demand function";
117
120 mkeq7.
comment=
"[h7 and h3] Transformed products import function";
123
126 mkeq8.
comment=
"[h8 and h4] Raw products export function";
129
131 mkeq13.
name=
"mkeq13";
132 mkeq13.
comment=
"[h9] Calculation of the composite price of transformed products (PPC_Dp)";
135
137 mkeq14.
name=
"mkeq14";
138 mkeq14.
comment=
"[h10] Calculation of the composite price of raw products (PPC_Sw)";
141
143 mkeq17.
name=
"mkeq17";
144 mkeq17.
comment=
"[h16] Constrain of the transformaton supply (lower than the regional maximal production capacity)";
147
148
150 mkeq23.
name=
"mkeq23";
151 mkeq23.
comment=
"[h3] Composit demand eq. (Dp)";
154
156 mkeq24.
name=
"mkeq24";
157 mkeq24.
comment=
"[h4] Composite supply eq. (Sw)";
160
162 mkeq26.
name=
"mkeq26";
163 mkeq26.
comment=
"[eq ] Verification of the null transport agents supply";
166
168 mkeq25.
name=
"mkeq25";
169 mkeq25.
comment=
"Verification of the null trasformers supply (price of raw product + trasf product > trasf product)";
172
174 mkeq18.
name=
"mkeq18";
175 mkeq18.
comment=
"Constrain on raw material supply (lower than inventory)";
178
180 resbounds.
name=
"resbounds";
181 resbounds.
comment=
"Constrain on raw material supply (lower than inventory, for each possible combination of primary products)";
184
185
186
187
188
189
190
191
192
193 cons.push_back(mkeq2);
194 cons.push_back(mkeq6);
195 cons.push_back(mkeq7);
196 cons.push_back(mkeq13);
197 cons.push_back(mkeq23);
198 cons.push_back(mkeq3);
199 cons.push_back(mkeq4);
200 cons.push_back(mkeq5);
201 cons.push_back(mkeq8);
202 cons.push_back(mkeq14);
203 cons.push_back(mkeq24);
204 cons.push_back(mkeq17);
205 cons.push_back(mkeq26);
206 cons.push_back(mkeq25);
207
208 cons.push_back(resbounds);
209
210;
211
212
213
214}
215
216
217
218template<class T> bool
220
221 double aa, bb, dc0, sigma, a_pr, ct, m, zeromax,supCorr2;
222 obj_value = 0.;
223 zeromax = 0.;
224
225 for (uint r1=0;r1<
l2r.size();r1++){
226 for (uint r2=0;r2<
l2r[r1].size();r2++){
227
228
229
230
231
232
233
234
235
236
237
238 for (uint p=0;p<
secPr.size();p++){
242
243 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)];
244
245 }
246
247
248
249
250
251 for (uint p=0;p<
priPr.size();p++){
254
255 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));
256 }
257
258
259
260
261
262
263 for (uint p1=0;p1<
priPr.size();p1++){
264 for (uint p2=0;p2<
priPr.size();p2++){
268 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);
269 }
270 }
271
272
273 for (uint p=0;p<
allPr.size();p++){
274 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
276 obj_value += (x[
gix(
"pl",r1,r2To,p)]-x[
gix(
"pl",r1,r2,p)]-ct)*x[
gix(
"rt",r1,r2,p,r2To)];
277 }
278 }
279
280
281
282 for (uint p=0;p<
secPr.size();p++){
285 }
286
287 for (uint p=0;p<
priPr.size();p++){
288 obj_value -= x[
gix(
"pl",r1,r2,p)]*x[
gix(
"dl",r1,r2,p)];
289 }
290 }
291
292 }
293
294
295
296
297 return true;
298
299
300}
301
302
303
304
305
306
307
308
309template<class T> bool
311
312 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;
313 Index cix = 0;
314 Index debug = 0;
315
316
318
319 g[cix] = x[
gix("dl",r1,r2,psec)]-x[
gix("sl",r1,r2,psec)];
320 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
321 g[cix] += x[
gix(
"rt",r1,r2,psec,r2To)]-x[
gix(
"rt",r1,r2To,psec,r2)];
322 }
324
325
326
327
332 pol_mktDirInt_d =
gpd("pol_mktDirInt_d",
l2r[r1][r2],
secPr[p]);
334 additionalDemand =
gpd("additionalDemand",
l2r[r1][r2],
secPr[p]);
335 g[cix] = - gg*pow(x[
gix("pc",r1,r2,psec)]*pol_mktDirInt_d,sigma);
336 vector<
string> debug =
secPr;
337
338 for (uint p2=0;p2<
secPr.size();p2++){
340 if(es_d){
342 pol_mktDirInt_d_pSubstituted =
gpd(
"pol_mktDirInt_d",
l2r[r1][r2],
secPr[p2]);
344
345 g[cix] *= pow(
346 (
347 ((x[
gix(
"pc",r1,r2,psec)]*pol_mktDirInt_d) / (x[
gix(
"pc",r1,r2,
priPr.size()+p2)]*pol_mktDirInt_d_pSubstituted))
348 /
349 ((pc_1*pol_mktDirInt_d_1) / (pc_1_pSubstituted*pol_mktDirInt_d_1_pSubstituted))
350 ), es_d
351 );
352 }
353 }
354
355 for (uint p3=0;p3<
othPr.size();p3++){
357 if(es_d){
360
361 g[cix] *= pow(
362 (
363 (x[
gix(
"pc",r1,r2,psec)]*pol_mktDirInt_d / pc_pSubstituted)
364 /
365 (pc_1*pol_mktDirInt_d_1 / pc_1_pSubstituted)
366 ), es_d
367 );
368 }
369 }
370
371 g[cix] += x[
gix(
"dc",r1,r2,p)]-additionalDemand;
373
374
375
378 p1v = 1-q1;
381 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);
383
384
387 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;
389
390
394 p1v = 1-q1;
395 g[cix] = x[
gix("dc",r1,r2,p)] -
396 pow(
397 q1 * pow(x[
gix("da",r1,r2,p)],(psi-1)/psi)
398 + p1v * pow(x[
gix("dl",r1,r2,psec)],(psi-1)/psi),
399 psi/(psi-1)
400 );
402
403
405
406 g[cix] = x[
gix("dl",r1,r2,p)]-x[
gix("sl",r1,r2,p)];
407 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
408 g[cix] += x[
gix(
"rt",r1,r2,p,r2To)]-x[
gix(
"rt",r1,r2To,p,r2)];
409 }
410 for (uint p2=0;p2<
priPr.size();p2++){
412 g[cix] -= a_pr*x[
gix(
"sl",r1,r2,p2)];
413 }
415
416
418 g[cix] = x[
gix("dl",r1,r2,p)];
419 for (uint p2=0;p2<
secPr.size();p2++){
421 g[cix] -= a*x[
gix(
"sl",r1,r2,p2+
nPriPr)];
422 }
424
425
426
427
428
429
432 pol_mktDirInt_s =
gpd("pol_mktDirInt_s",
l2r[r1][r2],
priPr[p]);
434
435
437 double intRate =
MTHREAD->MD->getDoubleSetting("ir");
438 double Pct = carbonPrice*intRate;
439 double omega =
gpd("co2content_products",
l2r[r1][r2],
priPr[p])/1000;
440
441
442 g[cix] = x[
gix("sc",r1,r2,p)]-ff*pow((x[
gix("pc",r1,r2,p)]-omega*Pct)*pol_mktDirInt_s,sigma);
443
444
445
447
448
449
452 r1v = 1-t1;
455 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);
457
458
461 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;
463
464
467 r1v = 1-t1;
469 g[cix] = x[
gix("sc",r1,r2,p)] -
470 pow(
471 t1 * pow(x[
gix("sa",r1,r2,p)],(eta-1)/eta)
472 + r1v * pow(x[
gix("sl",r1,r2,p)],(eta-1)/eta),
473 eta/(eta-1)
474 );
476
477
482
483
485 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
486 cix =
gix(12, r1, r2, p,r2To);
488 g[cix] = (x[
gix(
"pl",r1,r2To,p)]-x[
gix(
"pl",r1,r2,p)]-ct);
489 }
491
492
495 g[cix] = mv - x[
gix("pl",r1,r2,p+
nPriPr)];
496 for (uint p2=0;p2<
priPr.size();p2++){
498 g[cix] += a * x[
gix(
"pl",r1,r2,p2)];
499 }
501
502
503
504
505
506
507
508
509
510
512
513
515
516
517
518 g[cix] = -in;
521 }
523
525
526
527
528
529
530 return true;
531}
532
533
534
535
542}
543
545
546}
547
548
549bool
551 Index& nnz_h_lag, IndexStyleEnum& index_style){
552
553
555
570
571 for(uint i=0;i<l1regIds.size();i++){
572 std::vector<int> l2ChildrenIds;
574 std::vector<ModelRegion*> l2Childrens = l1Region->
getChildren(
true);
575 for(uint j=0;j<l2Childrens.size();j++){
576 l2ChildrenIds.push_back(l2Childrens[j]->getRegId());
577 }
578 if(l2ChildrenIds.size()){
579 l2r.push_back(l2ChildrenIds);
580 }
581 }
582
583
586
587
589
590
592
593
595
596
598
599
601
602
603
604
605
606 }
607
609
612
614
616
618
619
620
621
622
623
624
625
626 index_style = C_STYLE;
627
629 return true;
630}
631
632bool
634
635
636 for (Index i=0; i<n; i++) {
639 }
640
641
642 for (Index i=0; i<m; i++) {
644 switch (direction){
646 g_l[i] = 0.;
647 g_u[i] = 0.;
648 break;
650 g_l[i] = -2e19;
651 g_u[i] = 0.;
652 break;
654 g_l[i] = 0.;
655 g_u[i] = 2e19;
656 break;
657 }
658 }
659 return true;
660}
661
662bool
664 Index m, bool init_lambda, Number* lambda){
665
666
667
668
669
670
671
672
673
674
675 assert(init_x == true);
676 assert(init_z == false);
677 assert(init_lambda == false);
678
679 VarMap::iterator viter;
680
681
682 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
683
684 int vdomtype = viter->second.domain;
686 for(uint r1=0;r1<
l2r.size();r1++){
687 for(uint r2=0;r2<
l2r[r1].size();r2++){
688 for(uint p=0;p<
priPr.size();p++){
690 }
691 }
692 }
694 for(uint r1=0;r1<
l2r.size();r1++){
695 for(uint r2=0;r2<
l2r[r1].size();r2++){
696 for(uint p=0;p<
secPr.size();p++){
698 }
699 }
700 }
702 for(uint r1=0;r1<
l2r.size();r1++){
703 for(uint r2=0;r2<
l2r[r1].size();r2++){
704 for(uint p=0;p<
allPr.size();p++){
706 }
707 }
708 }
710 for(uint r1=0;r1<
l2r.size();r1++){
711 for(uint r2=0;r2<
l2r[r1].size();r2++){
712 for(uint p=0;p<
allPr.size();p++){
713 for(uint r2To=0;r2To<
l2r[r1].size();r2To++){
715 }
716 }
717 }
718 }
719 } else {
721 }
722 }
723
724
725
726 return true;
727}
728
729
730void
732 Index n, const Number* x, const Number* z_L, const Number* z_U,
733 Index m, const Number* g, const Number* lambda,
734 Number obj_value, const IpoptData* ip_data, IpoptCalculatedQuantities* ip_cq){
735
736 printf("\n\nObjective value\n");
737 printf("f(x*) = %e\n", obj_value);
738
739
740
741 VarMap::iterator viter;
742
743
744 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
745
746 int vdomtype = viter->second.domain;
748 for(uint r1=0;r1<
l2r.size();r1++){
749 for(uint r2=0;r2<
l2r[r1].size();r2++){
750 for(uint p=0;p<
priPr.size();p++){
751 spd(x[
gix(viter->first,r1,r2,p)],viter->first,
l2r[r1][r2],
priPr[p]);
752 }
753 }
754 }
756 for(uint r1=0;r1<
l2r.size();r1++){
757 for(uint r2=0;r2<
l2r[r1].size();r2++){
758 for(uint p=0;p<
secPr.size();p++){
759 spd(x[
gix(viter->first,r1,r2,p)],viter->first,
l2r[r1][r2],
secPr[p]);
760
761 }
762 }
763 }
765 for(uint r1=0;r1<
l2r.size();r1++){
766 for(uint r2=0;r2<
l2r[r1].size();r2++){
767 for(uint p=0;p<
allPr.size();p++){
768 spd(x[
gix(viter->first,r1,r2,p)],viter->first,
l2r[r1][r2],
allPr[p]);
769 }
770 }
771 }
773 for(uint r1=0;r1<
l2r.size();r1++){
774 for(uint r2=0;r2<
l2r[r1].size();r2++){
775 for(uint p=0;p<
allPr.size();p++){
776 for(uint r2To=0;r2To<
l2r[r1].size();r2To++){
777
778
779
780 spd(x[
gix(viter->first,r1,r2,p,r2To)],viter->first,
l2r[r1][r2],
allPr[p],
DATA_NOW,
false,
i2s(
l2r[r1][r2To]));
781 }
782 }
783 }
784 }
785 } else {
787 }
788 }
789
790
792
795
798
803
804 for (int i=0;i<n+m+1;i++) {
806 }
808
809}
810
811
812
813
814
815
816
817
818
819
820bool
821Opt::eval_f(Index n,
const Number* x,
bool new_x, Number& obj_value){
823
824 return true;
825}
826
827bool
829
830 gradient(
tag_f,n,x,grad_f);
831
832 return true;
833}
834
835bool
836Opt::eval_g(Index n,
const Number* x,
bool new_x, Index m, Number* g){
837
839
840 return true;
841}
842
843bool
844Opt::eval_jac_g(Index n,
const Number *x,
bool new_x,Index m, Index nele_jac,
845 Index* iRow, Index *jCol, Number* values){
846 if (values == NULL) {
847
848
849 for(Index idx=0; idx<
nnz_jac; idx++)
850 {
853 }
854 }
855 else {
856
857
859
860 for(Index idx=0; idx<
nnz_jac; idx++)
861 {
862 values[idx] =
jacval[idx];
863
864 }
865 }
866 return true;
867}
868
869bool
870Opt::eval_h(Index n,
const Number* x,
bool new_x, Number obj_factor, Index m,
const Number* lambda,
871 bool new_lambda, Index nele_hess, Index* iRow, Index* jCol, Number* values){
872
873
874
875 if (values == NULL) {
876
877
878
879 for(Index idx=0; idx<
nnz_L; idx++)
880 {
883 }
884 }
885 else {
886
887
888
889 for(Index idx = 0; idx<n ; idx++)
891 for(Index idx = 0; idx<m ; idx++)
892 x_lam[n+idx] = lambda[idx];
893 x_lam[n+m] = obj_factor;
894
896
897 Index idx = 0;
898 for(Index idx_total = 0; idx_total <
nnz_L_total ; idx_total++)
899 {
901 {
902 values[idx] =
hessval[idx_total];
903 idx++;
904 }
905 }
906 }
907
908 return true;
909
910
911}
912
913
914
915
916void
918
919 Number *xp = new double[n];
920 Number *lamp = new double[m];
921 Number *zl = new double[m];
922 Number *zu = new double[m];
923
924 adouble *xa = new adouble[n];
925 adouble *g = new adouble[m];
926 adouble *lam = new adouble[m];
927 adouble sig;
928 adouble obj_value;
929
930 double dummy;
931
932
933 int i,j,k,l,ii;
934
935 x_lam =
new double[n+m+1];
936
937
939
940
941
943
944 for(Index idx=0;idx<n;idx++)
945 xa[idx] <<= xp[idx];
946
948
949 obj_value >>= dummy;
950
951 trace_off();
952
954
955 for(Index idx=0;idx<n;idx++)
956 xa[idx] <<= xp[idx];
957
959
960
961 for(Index idx=0;idx<m;idx++)
962 g[idx] >>= dummy;
963
964 trace_off();
965
967
968 for(Index idx=0;idx<n;idx++)
969 xa[idx] <<= xp[idx];
970 for(Index idx=0;idx<m;idx++)
971 lam[idx] <<= 1.0;
972 sig <<= 1.0;
973
975
976 obj_value *= sig;
978
979 for(Index idx=0;idx<m;idx++)
980 obj_value += g[idx]*lam[idx];
981
982 obj_value >>= dummy;
983
984 trace_off();
985
986
987
988
991
996
998
1000
1003
1004 unsigned int **JP_f=NULL;
1005 unsigned int **JP_g=NULL;
1006 unsigned int **HP_f=NULL;
1007 unsigned int **HP_g=NULL;
1008 unsigned int *HP_length=NULL;
1009 unsigned int *temp=NULL;
1010
1011 int ctrl_H;
1012
1013 JP_f = (unsigned int **) malloc(sizeof(unsigned int*));
1014 JP_g = (unsigned int **) malloc(m*sizeof(unsigned int*));
1015 HP_f = (unsigned int **) malloc(n*sizeof(unsigned int*));
1016 HP_g = (unsigned int **) malloc(n*sizeof(unsigned int*));
1017 HP_t = (
unsigned int **) malloc((n+m+1)*
sizeof(
unsigned int*));
1018 HP_length = (unsigned int *) malloc((n)*sizeof(unsigned int));
1019 ctrl_H = 0;
1020
1021 hess_pat(
tag_f, n, xp, HP_f, ctrl_H);
1022
1023 indopro_forward_safe(
tag_f, 1, n, xp, JP_f);
1024 indopro_forward_safe(
tag_g, m, n, xp, JP_g);
1025 nonl_ind_forward_safe(
tag_g, m, n, xp, HP_g);
1026
1027 for (i=0;i<n;i++)
1028 {
1029 if (HP_f[i][0]+HP_g[i][0]!=0)
1030 {
1031 if (HP_f[i][0]==0)
1032 {
1033 HP_t[i] = (
unsigned int *) malloc((HP_g[i][0]+
HPOFF)*
sizeof(
unsigned int));
1034 for(j=0;j<=(int) HP_g[i][0];j++)
1035 {
1036 HP_t[i][j] = HP_g[i][j];
1037 }
1038 HP_length[i] = HP_g[i][0]+
HPOFF;
1039 }
1040 else
1041 {
1042 if (HP_g[i][0]==0)
1043 {
1044 HP_t[i] = (
unsigned int *) malloc((HP_f[i][0]+
HPOFF)*
sizeof(
unsigned int));
1045 for(j=0;j<=(int) HP_f[i][0];j++)
1046 {
1047 HP_t[i][j] = HP_f[i][j];
1048 }
1049 HP_length[i] = HP_f[i][0]+
HPOFF;
1050 }
1051 else
1052 {
1053 HP_t[i] = (
unsigned int *) malloc((HP_f[i][0]+HP_g[i][0]+
HPOFF)*
sizeof(
unsigned int));
1054 k = l = j = 1;
1055 while ((k<=(int) HP_f[i][0]) && (l <= (int) HP_g[i][0]))
1056 {
1057 if (HP_f[i][k] < HP_g[i][l])
1058 {
1059 HP_t[i][j]=HP_f[i][k];
1060 j++; k++;
1061 }
1062 else
1063 {
1064 if (HP_f[i][k] == HP_g[i][l])
1065 {
1066 HP_t[i][j]=HP_f[i][k];
1067 l++;j++;k++;
1068 }
1069 else
1070 {
1071 HP_t[i][j]=HP_g[i][l];
1072 j++;l++;
1073 }
1074 }
1075 }
1076
1077
1078 for(ii=k;ii<=(int) HP_f[i][0];ii++)
1079 {
1080 HP_t[i][j] = HP_f[i][ii];
1081 j++;
1082 }
1083
1084
1085 for(ii=l;ii<=(int) HP_g[i][0];ii++)
1086 {
1087 HP_t[i][j] = HP_g[i][ii];
1088 j++;
1089 }
1090
1091 }
1092 }
1094 HP_length[i] = HP_f[i][0]+HP_g[i][0]+
HPOFF;
1095 }
1096 else
1097 {
1098 HP_t[i] = (
unsigned int *) malloc((
HPOFF+1)*
sizeof(
unsigned int));
1101 }
1102
1103
1104
1105
1106
1107
1108
1109 }
1110
1111
1112
1113
1114 for (i=0;i<m;i++)
1115 {
1116
1117 HP_t[n+i] = (
unsigned int *) malloc((JP_g[i][0]+1)*
sizeof(
unsigned int));
1118 HP_t[n+i][0]=JP_g[i][0];
1119
1120
1121
1122 for(j=1;j<= (int) JP_g[i][0];j++)
1123 {
1124 HP_t[n+i][j]=JP_g[i][j];
1125
1126
1127
1128
1129 if (HP_length[JP_g[i][j]] <=
HP_t[JP_g[i][j]][0]+1)
1130 {
1131
1132
1133
1134
1135
1136 temp = (
unsigned int *) malloc((
HP_t[JP_g[i][j]][0]+1)*
sizeof(
unsigned int));
1137 for(l=0;l<=(int)
HP_t[JP_g[i][j]][0];l++)
1138 {
1139 temp[l] =
HP_t[JP_g[i][j]][l];
1140
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 unsigned int machin = JP_g[i][j];
1155
1156
1157 HP_t[JP_g[i][j]] = (
unsigned int *) malloc(2*HP_length[JP_g[i][j]]*
sizeof(
unsigned int));
1158 HP_length[JP_g[i][j]] = 2*HP_length[JP_g[i][j]];
1159
1160
1161 for(l=0;l<=(int)temp[0];l++)
1162 HP_t[JP_g[i][j]][l] =temp[l];
1163 free(temp);
1164
1165
1166
1167 }
1168 HP_t[JP_g[i][j]][0] =
HP_t[JP_g[i][j]][0]+1;
1169 HP_t[JP_g[i][j]][
HP_t[JP_g[i][j]][0]] = i+n;
1170 }
1171 }
1172
1173
1174 for(j=1;j<= (int) JP_f[0][0];j++)
1175 {
1176 if (HP_length[JP_f[0][j]] <=
HP_t[JP_f[0][j]][0]+1)
1177 {
1178 temp = (
unsigned int *) malloc((
HP_t[JP_f[0][j]][0])*
sizeof(
unsigned int));
1179 for(l=0;l<=(int)
HP_t[JP_f[0][j]][0];l++)
1180 temp[l] =
HP_t[JP_f[0][j]][l];
1181 free(
HP_t[JP_f[0][j]]);
1182 HP_t[JP_f[0][j]] = (
unsigned int *) malloc(2*HP_length[JP_f[0][j]]*
sizeof(
unsigned int));
1183 HP_length[JP_f[0][j]] = 2*HP_length[JP_f[0][j]];
1184 for(l=0;l<=(int)temp[0];l++)
1185 HP_t[JP_f[0][j]][l] =temp[l];
1186 free(temp);
1187 }
1188 HP_t[JP_f[0][j]][0] =
HP_t[JP_f[0][j]][0]+1;
1189 HP_t[JP_f[0][j]][
HP_t[JP_f[0][j]][0]] = n+m;
1190 }
1191
1192 HP_t[n+m] = (
unsigned int *) malloc((JP_f[0][0]+2)*
sizeof(
unsigned int));
1193 HP_t[n+m][0]=JP_f[0][0]+1;
1194 for(j=1;j<= (int) JP_f[0][0];j++)
1195 HP_t[n+m][j]=JP_f[0][j];
1196 HP_t[n+m][JP_f[0][0]+1]=n+m;
1197
1199
1200 nnz_h_lag = 0;
1201 for (i=0;i<n;i++)
1202 {
1203 for (j=1;j<=(int)
HP_t[i][0];j++)
1204 if ((
int)
HP_t[i][j] <= i)
1205 nnz_h_lag++;
1206 free(HP_f[i]);
1207 free(HP_g[i]);
1208 }
1210
1213
1217
1219
1224
1225 unsigned int ind = 0;
1226
1227 for (int i=0;i<n;i++)
1228 for (
unsigned int j=1;j<=
HP_t[i][0];j++)
1229 {
1230 if (((
int)
HP_t[i][j]>=i) &&((
int)
HP_t[i][j]<n))
1231 {
1234 }
1235 }
1236
1237 ind = 0;
1238 for (int i=0;i<n+m+1;i++)
1239 for (
unsigned int j=1;j<=
HP_t[i][0];j++)
1240 {
1241 if ((
int)
HP_t[i][j]>=i)
1242 {
1245 }
1246 }
1247
1248 for (i=0;i<m;i++) {
1249 free(JP_g[i]);
1250 }
1251
1252 free(JP_f[0]);
1253 free(JP_f);
1254 free(JP_g);
1255 free(HP_f);
1256 free(HP_g);
1257 free(HP_length);
1258
1259 delete[] lam;
1260 delete[] g;
1261 delete[] xa;
1262 delete[] zu;
1263 delete[] zl;
1264 delete[] lamp;
1265 delete[] xp;
1266
1267}
1268
1269
1270
1271
1272const int
1273Opt::gip(
const string &varName)
const{
1274 map<string, int>::const_iterator p;
1277 return p->second;
1278 }
1279 else {
1280 msgOut(
MSG_CRITICAL_ERROR,
"Asking the initial position in the concatenated array of a variable ("+varName+
") that doesn't exist!");
1281 return 0;
1282 }
1283}
1284
1285const int
1288}
1289
1290const int
1292 VarMap::const_iterator p;
1293 p=
vars.find(varName);
1294 if(p !=
vars.end()) {
1295 return p->second.domain;
1296 }
1297 else {
1299 return 0;
1300 }
1301}
1302
1303const int
1305 return cons.at(cn).domain;
1306}
1307
1308template<class T> const int
1310
1311
1312
1313 int dType =
gdt(v_or_c);
1314 int othCountriesRegions = 0;
1315 int othCountriesRegions_r2case = 0;
1316 for (uint i=0;i<r1Ix;i++){
1317 othCountriesRegions +=
l2r[i].size();
1318 }
1319 for (uint i=0;i<r1Ix;i++){
1320 othCountriesRegions_r2case +=
l2r[i].size()*
l2r[i].size();
1321 }
1322
1323 switch (dType){
1325 return gip(v_or_c)+(othCountriesRegions+r2Ix)*
nPriPr+prIx;
1327 return gip(v_or_c)+(othCountriesRegions+r2Ix)*
nSecPr+prIx;;
1329 return gip(v_or_c)+(othCountriesRegions+r2Ix)*
nAllPr+prIx;
1331 return gip(v_or_c)+(othCountriesRegions_r2case)*
nAllPr+(r2Ix*
nPriPr+prIx)*
l2r[r1Ix].size()+r2IxTo;
1333 return gip(v_or_c)+(othCountriesRegions_r2case)*
nAllPr+(r2Ix*
nSecPr+prIx)*
l2r[r1Ix].size()+r2IxTo;
1335 return gip(v_or_c)+(othCountriesRegions_r2case)*
nAllPr+(r2Ix*
nAllPr+prIx)*
l2r[r1Ix].size()+r2IxTo;
1336
1337
1342 default:
1344 return 0;
1345 }
1346}
1347
1348
1349const int
1350Opt::gix(
const string &varName,
const int& r1Ix,
const int& r2Ix,
const int& prIx,
const int& r2IxTo)
const{
1351
1352 map <string, vector < vector < vector < vector <int> > > > >::const_iterator p;
1355 return p->second[r1Ix][r2Ix][prIx][r2IxTo];
1356 }
1357 else {
1359 return 0;
1360 }
1361}
1362
1363const int
1364Opt::gix(
const int &cn,
const int& r1Ix,
const int& r2Ix,
const int& prIx,
const int& r2IxTo)
const{
1365 return cpositions[cn][r1Ix][r2Ix][prIx][r2IxTo];
1366}
1367
1368void
1370 int vInitialPosition = 0;
1371 int cInitialPosition = 0;
1372 VarMap::iterator viter;
1373 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
1374 initPos.insert(pair<string, int>(viter->first, vInitialPosition));
1375 initPos_rev.insert(pair<int, string>(vInitialPosition,viter->first));
1377 }
1378 for (uint i=0;i<
cons.size();i++){
1379 cInitPos.push_back(cInitialPosition);
1381 }
1382}
1383
1384void
1386
1387
1388 VarMap::iterator viter;
1389 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
1390 vpositions.insert(pair<
string, vector < vector < vector < vector <int> > > > >(viter->first,
buildPositionVector(viter->first, viter->second.domain)));
1391 }
1392
1393 for (uint i=0; i<
cons.size();i++){
1395 }
1396
1397}
1398
1399template<class T> vector < vector < vector < vector <int> > > >
1401 int pVectorSize;
1402
1403 switch (dType){
1405 pVectorSize=
priPr.size();
1406 break;
1408 pVectorSize=
secPr.size();
1409 break;
1411 pVectorSize=
allPr.size();
1412 break;
1414 pVectorSize=
priPr.size();
1415 break;
1417 pVectorSize=
secPr.size();
1418 break;
1420 pVectorSize=
allPr.size();
1421 break;
1423 pVectorSize=
allPr.size();
1424 break;
1427 break;
1428 default:
1430 }
1431
1432
1433 vector < vector < vector < vector <int> > > > positionsToAdd;
1434 for(uint r1=0;r1<
l2r.size();r1++){
1435 vector < vector < vector <int> > > dim1;
1436 for(uint r2=0;r2<
l2r[r1].size();r2++){
1437 vector < vector <int> > dim2;
1438 for(uint p=0;p<pVectorSize;p++){
1439 vector <int> dim3;
1440 for(uint r2To=0;r2To<
l2r[r1].size();r2To++){
1442 }
1443 dim2.push_back(dim3);
1444 }
1445 dim1.push_back(dim2);
1446 }
1447 positionsToAdd.push_back(dim1);
1448 }
1449 return positionsToAdd;
1450}
1451
1452
1453void
1455
1457 VarMap::iterator viter;
1458 for (viter =
vars.begin(); viter !=
vars.end(); ++viter) {
1460 }
1461
1462
1467 for(uint i=0;i<
cons.size();i++){
1471 continue;
1474 continue;
1477 continue;
1478 } else {
1480 }
1481 }
1482
1484}
1485
1486int
1488 int elements = 0;
1489 switch (domain){
1497 for(uint r1=0;r1<
l2r.size();r1++){
1499 }
1500 return elements;
1502 for(uint r1=0;r1<
l2r.size();r1++){
1504 }
1505 return elements;
1507 for(uint r1=0;r1<
l2r.size();r1++){
1509 }
1510 return elements;
1512 return 1;
1515 default:
1517 }
1518}
1519
1520int
1522 for(uint i=0;i<
cons.size();i++){
1523 if(i!=
cons.size()-1){
1524 if (idx >=
gip(i) && idx <
gip(i+1)){
1525 return cons[i].direction;
1526 }
1527 } else {
1529 return cons[i].direction;
1530 }
1531 }
1532 }
1534}
1535
1536double
1538
1539 map <int, string>::const_iterator p;
1541 p--;
1542 VarMap::const_iterator p2;
1543 p2=
vars.find(p->second);
1544 if(p2 !=
vars.end()) {
1546 if (p2->second.l_bound_var == ""){
1547 return p2->second.l_bound;
1548 } else {
1550 }
1551 }
else if (bound_type==
UBOUND){
1552 if (p2->second.u_bound_var == ""){
1553 return p2->second.u_bound;
1554 } else {
1556 }
1557 } else {
1559 }
1560 }
1561 else {
1563 }
1564 return 0.;
1565}
1566
1567double
1569
1570 int r1,r2,p,r2to;
1572
1573
1575 if(r2to){
1577 } else {
1579 }
1580 } else {
1581 if(r2to){
1583 } else {
1584
1586 }
1587 }
1588}
1589
1592 for(uint i=0;i<
cons.size();i++){
1593 if(i!=
cons.size()-1){
1594 if (idx >=
gip(i) && idx <
gip(i+1)){
1596 }
1597 } else {
1600 }
1601 }
1602 }
1604}
1605
1606
1607void
1608Opt::unpack(
int ix_h,
int domain,
int initial,
int &r1_h,
int &r2_h,
int&p_h,
int&r2to_h,
bool fullp){
1609 ix_h = ix_h-initial;
1610 double ix=0;
1611 bool r2flag = false;
1612 int pIndexToAdd = 0;
1613 int np=0;
1621 r1_h=0;r2_h=0;p_h=0;r2to_h=0;
1622 return;
1623 } else {
1625 }
1627 r2flag = true;
1628 }
1631
1632 }
1633
1634 for (uint r1=0;r1<
l2r.size();r1++){
1635 for (uint r2=0;r2<
l2r[r1].size();r2++){
1636 for (uint p=0;p<np;p++){
1637 if(!r2flag){
1638 if(ix==ix_h){
1639 r1_h=r1;
1640 r2_h=r2;
1641 p_h=p+pIndexToAdd;
1642 r2to_h=0;
1643 return;
1644 }
1645 ix++;
1646 } else {
1647 for (uint r2To=0;r2To<
l2r[r1].size();r2To++){
1648 if(ix==ix_h){
1649 r1_h=r1;
1650 r2_h=r2;
1651 p_h=p+pIndexToAdd;
1652 r2to_h=r2To;
1653 return;
1654 }
1655 ix++;
1656 }
1657 }
1658 }
1659 }
1660 }
1662}
1663
1664int
1666 for(uint i=0;i<
cons.size();i++){
1671 return i;
1672 }
1673 }
1675}
1676
1677
1678void
1680
1681 unsigned int **jacpat=NULL;
1682 int options_j[3];
1683 double *x;
1684 int retv_j = -1;
1685
1686 options_j[0] = 0;
1687 options_j[1] = 0;
1688 options_j[2] = 0;
1689 jacpat =
new unsigned int* [
nCons];
1690 x =
new double[
nVar];
1691
1693
1695
1696 for (
int i=0;i<
nCons;i++) {
1697 for (int j=1;j<=jacpat[i][0];j++){
1698 vector <int> nzjelement;
1699 nzjelement.push_back(i);
1700 nzjelement.push_back(jacpat[i][j]);
1702 }
1703 }
1704}
1705
1706void
1708
1709 unsigned int **hesspat=NULL;
1710 int options_h=0;
1711 double *x;
1712 int retv_h = -1;
1713
1714 hesspat =
new unsigned int* [(
nVar+
nCons+1)];
1716
1718
1719 for (
int i=0;i<(
nVar);i++) {
1720 for (int j=1;j<=hesspat[i][0];j++){
1721 if(hesspat[i][j]<=i){
1722 vector <int> nzhelement;
1723 nzhelement.push_back(i);
1724 nzhelement.push_back(hesspat[i][j]);
1726 }
1727 }
1728 }
1729}
1730
1731void
1733
1734 cout <<
"Num of variables: " <<
nVar <<
" - Num of constrains:" <<
nCons << endl;
1735 cout << "IDX;ROW;COL" << endl;
1738 }
1739
1740 cout <<
"Dense jacobian: " <<
nCons *
nVar <<
" elements" << endl;
1741 cout <<
"Dense hessian: " <<
nVar*(
nVar-1)/2+
nVar <<
" elements" << endl;
1742
1743
1744}
1745
1746
1747const Number&
1749 return (a<b)?b:a;
1750}
1751const adouble&
1752Opt::mymax(
const adouble& a,
const adouble& b){
1753 return (a<b)?b:a;
1754}
1755
1756
1757bool
1758Opt::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){
1759 int itnumber = iter;
1760 if(itnumber%10==0){
1762 }
1763 return true;
1764}
1765
1766int
1769}
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789void
1790Opt::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){
1792 end_var.
name = name;
1799 vars.insert(std::pair<std::string, endvar >(name, end_var));
1800}
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843void
1845
1846
1847
1848 vector < vector < vector <double> > > in_temp;
1849 for (uint r1=0;r1<
l2r.size();r1++){
1850 vector < vector <double> > dim1;
1851 for (uint r2=0;r2<
l2r[r1].size();r2++){
1852 vector <double> dim2;
1856 dim2.push_back(this_in);
1857 }
1858 dim1.push_back(dim2);
1859 }
1860 in_temp.push_back(dim1);
1861 }
1863}
@ 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)
#define CONSTRAIN_START_LOOP(pVector, cn)
#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.