Objective value does not equal X * obj
AnsweredRecently I encountered that (in some of my models) the variable values seem to be incorrectly stored into my data. When I try the following
int i, error;
double total_obj_by_hand=0, total_obj_by_gurobi;
error = GRBgetdblattrarray(model, "X", 0, n_vars, var_values);
for (i = 0; i < n_vars; i++)
{
total_obj_by_hand += obj[i] * var_values[i];
}
error = GRBgetdblattr(model, "ObjVal", &total_obj_by_gurobi);
total_obj_by_hand equals 38913.512444 whereas total_obj_by_gurobi equals 14459.446702. The value calculated by gurobi is the correct value and it is also the same as in the logs:
Gurobi 11.0.3 (win64) logging started Wed Oct 30 22:30:55 2024
Set parameter LogToConsole to value 0
Set parameter Username
Set parameter Method to value 2
Set parameter ScaleFlag to value 0
Set parameter BarHomogeneous to value 1
Set parameter Crossover to value 0
Set parameter PreDual to value 1
Set parameter IISMethod to value 1
Set parameter LogFile to value "solver_output_sub_model_no_investment_year_2040_hours_168-335.log"
Set parameter PreDepRow to value 0
Set parameter PrePasses to value 1
Set parameter Presolve to value 1
Set parameter SolutionTarget to value 1
Set parameter Threads to value 20
Academic license - for non-commercial use only - expires 2025-07-28
Gurobi Optimizer version 11.0.3 build v11.0.3rc0 (win64 - Windows Server 2022.0 (20348.2))
CPU model: AMD EPYC 7763 64-Core Processor, instruction set [SSE2|AVX|AVX2]
Thread count: 128 physical cores, 256 logical processors, using up to 20 threads
Optimize a model with 583921 rows, 5818297 columns and 7681188 nonzeros
Model fingerprint: 0xa87f1fe8
Coefficient statistics:
Matrix range [3e-01, 3e+02]
Objective range [1e-04, 5e+00]
Bounds range [2e-04, 1e+04]
RHS range [1e-03, 5e+03]
Presolve time: 5.90s
Presolved: 4693737 rows, 4929427 columns, 10433877 nonzeros
Elapsed ordering time = 5s
Elapsed ordering time = 12s
Ordering time: 40.46s
Barrier statistics:
Dense cols : 2612
Free vars : 15120
AA' NZ : 1.036e+08
Factor NZ : 1.885e+08 (roughly 5.0 GB of memory)
Factor Ops : 9.104e+10 (roughly 1 second per iteration)
Threads : 20
Objective Residual
Iter Primal Dual Primal Dual Compl Time
0 -5.88606747e+07 8.13608041e+08 6.80e+00 2.45e+01 4.74e+03 63s
1 -2.78151617e+07 2.84502805e+08 1.24e-01 5.48e+01 2.51e+02 66s
2 -9.98303883e+06 3.03218792e+07 3.80e-03 6.87e+00 2.67e+01 70s
3 -2.76744772e+06 1.48926049e+06 1.43e-04 3.02e-01 1.44e+00 75s
4 -6.46189690e+05 2.05578055e+05 6.46e-05 5.82e-02 2.32e-01 80s
5 -2.89496376e+05 8.24808878e+04 8.92e-05 2.43e-02 9.11e-02 84s
6 -1.29259697e+05 4.29666842e+04 1.09e-04 1.16e-02 3.62e-02 87s
7 -5.82207565e+04 2.64891749e+04 6.22e-05 5.05e-03 1.43e-02 92s
8 -1.53701777e+04 1.86243513e+04 3.78e-05 1.05e-03 4.43e-03 97s
9 -3.05119085e+03 1.73603405e+04 3.90e-05 6.92e-04 2.52e-03 101s
10 3.88504674e+03 1.66818847e+04 4.89e-05 5.14e-04 1.53e-03 104s
11 6.84335416e+03 1.61699582e+04 4.04e-05 3.83e-04 1.08e-03 108s
12 9.26236707e+03 1.53876329e+04 6.47e-05 1.90e-04 6.72e-04 112s
13 1.05629909e+04 1.51217563e+04 5.14e-05 1.27e-04 4.90e-04 118s
14 1.14694206e+04 1.49443122e+04 4.01e-05 8.65e-05 3.68e-04 124s
15 1.22305095e+04 1.48424680e+04 3.04e-05 6.44e-05 2.75e-04 131s
16 1.26328017e+04 1.47541480e+04 2.52e-05 4.63e-05 2.21e-04 136s
17 1.30412164e+04 1.46799027e+04 2.71e-05 3.16e-05 1.70e-04 142s
18 1.32922358e+04 1.46524075e+04 2.31e-05 2.66e-05 1.41e-04 148s
19 1.35107442e+04 1.46157187e+04 1.85e-05 2.02e-05 1.14e-04 155s
20 1.36757239e+04 1.45924749e+04 1.70e-05 1.64e-05 9.45e-05 161s
21 1.37928950e+04 1.45672678e+04 1.84e-05 1.24e-05 7.97e-05 167s
22 1.39266370e+04 1.45536142e+04 2.01e-05 1.03e-05 6.44e-05 172s
23 1.40640736e+04 1.45394539e+04 1.55e-05 8.34e-06 4.88e-05 178s
24 1.41564317e+04 1.45296884e+04 1.69e-05 7.07e-06 3.83e-05 185s
25 1.42112128e+04 1.45206053e+04 1.77e-05 5.94e-06 3.17e-05 190s
26 1.42502894e+04 1.45059667e+04 1.51e-05 4.26e-06 2.62e-05 197s
27 1.42836994e+04 1.44974210e+04 1.34e-05 3.32e-06 2.19e-05 203s
28 1.43106236e+04 1.44916794e+04 1.31e-05 2.71e-06 1.85e-05 210s
29 1.43229037e+04 1.44870985e+04 1.55e-05 2.25e-06 1.68e-05 216s
30 1.43536970e+04 1.44838869e+04 2.20e-05 1.93e-06 1.33e-05 223s
31 1.43625473e+04 1.44806663e+04 2.02e-05 1.63e-06 1.21e-05 230s
32 1.43746661e+04 1.44782098e+04 2.14e-05 1.40e-06 1.06e-05 237s
33 1.43874006e+04 1.44761145e+04 3.04e-05 1.22e-06 9.08e-06 244s
34 1.44096536e+04 1.44735288e+04 2.16e-05 9.93e-07 6.54e-06 251s
35 1.44189796e+04 1.44722073e+04 1.77e-05 8.86e-07 5.45e-06 258s
36 1.44264673e+04 1.44704791e+04 1.52e-05 7.52e-07 4.50e-06 264s
37 1.44330811e+04 1.44693868e+04 1.26e-05 6.71e-07 3.72e-06 271s
38 1.44346819e+04 1.44675690e+04 1.18e-05 5.39e-07 3.37e-06 279s
39 1.44387219e+04 1.44671483e+04 9.95e-06 5.08e-07 2.91e-06 285s
40 1.44416664e+04 1.44662623e+04 8.57e-06 4.45e-07 2.52e-06 293s
41 1.44438342e+04 1.44657001e+04 7.86e-06 4.06e-07 2.24e-06 300s
42 1.44461559e+04 1.44651909e+04 6.74e-06 3.70e-07 1.95e-06 307s
43 1.44480103e+04 1.44645619e+04 5.82e-06 3.26e-07 1.69e-06 315s
44 1.44497077e+04 1.44640106e+04 4.97e-06 2.89e-07 1.46e-06 322s
45 1.44515599e+04 1.44637214e+04 4.43e-06 2.69e-07 1.25e-06 330s
46 1.44523108e+04 1.44631360e+04 3.99e-06 2.30e-07 1.11e-06 337s
47 1.44531871e+04 1.44625270e+04 3.52e-06 1.90e-07 9.56e-07 345s
48 1.44538067e+04 1.44622639e+04 3.22e-06 1.72e-07 8.66e-07 352s
49 1.44548487e+04 1.44619445e+04 3.43e-06 1.51e-07 7.27e-07 361s
50 1.44555323e+04 1.44615529e+04 2.97e-06 1.25e-07 6.16e-07 369s
51 1.44560923e+04 1.44612643e+04 2.40e-06 1.07e-07 5.30e-07 377s
52 1.44564826e+04 1.44611187e+04 3.47e-06 9.73e-08 4.75e-07 386s
53 1.44568788e+04 1.44610285e+04 3.03e-06 9.15e-08 4.25e-07 395s
54 1.44569764e+04 1.44609030e+04 9.63e-06 8.35e-08 4.02e-07 404s
55 1.44574561e+04 1.44607709e+04 7.82e-06 7.52e-08 3.39e-07 414s
56 1.44576482e+04 1.44607494e+04 7.10e-06 7.38e-08 3.18e-07 422s
57 1.44577982e+04 1.44606481e+04 6.57e-06 6.75e-08 2.92e-07 433s
58 1.44579318e+04 1.44605783e+04 6.06e-06 6.32e-08 2.71e-07 443s
59 1.44580596e+04 1.44605208e+04 5.58e-06 5.96e-08 2.52e-07 453s
60 1.44581684e+04 1.44604964e+04 5.15e-06 5.81e-08 2.38e-07 464s
61 1.44583408e+04 1.44604193e+04 4.50e-06 5.34e-08 2.13e-07 474s
62 1.44584618e+04 1.44603407e+04 4.04e-06 4.87e-08 1.92e-07 485s
63 1.44585820e+04 1.44602974e+04 3.57e-06 4.60e-08 1.76e-07 496s
64 1.44587094e+04 1.44602563e+04 3.15e-06 4.36e-08 1.58e-07 507s
65 1.44587853e+04 1.44602326e+04 2.84e-06 4.21e-08 1.48e-07 517s
66 1.44588579e+04 1.44602035e+04 2.47e-06 4.04e-08 1.38e-07 527s
67 1.44589152e+04 1.44601651e+04 3.61e-06 3.81e-08 1.28e-07 545s
68 1.44589858e+04 1.44601346e+04 3.15e-06 3.63e-08 1.17e-07 559s
69 1.44590154e+04 1.44600472e+04 2.99e-06 3.10e-08 1.05e-07 568s
70 1.44590890e+04 1.44599775e+04 2.92e-06 2.68e-08 9.08e-08 578s
71 1.44591323e+04 1.44599396e+04 3.88e-06 2.45e-08 8.26e-08 586s
72 1.44591625e+04 1.44598904e+04 3.58e-06 2.16e-08 7.44e-08 599s
73 1.44592130e+04 1.44598592e+04 3.07e-06 1.97e-08 6.60e-08 611s
74 1.44592226e+04 1.44598493e+04 2.97e-06 1.92e-08 6.40e-08 623s
75 1.44592550e+04 1.44598274e+04 2.64e-06 1.78e-08 5.83e-08 632s
76 1.44592725e+04 1.44598044e+04 2.50e-06 1.64e-08 5.42e-08 644s
77 1.44593024e+04 1.44597770e+04 2.20e-06 1.49e-08 4.81e-08 655s
78 1.44593202e+04 1.44597507e+04 2.02e-06 1.34e-08 4.36e-08 668s
79 1.44593456e+04 1.44597242e+04 1.78e-06 1.18e-08 3.82e-08 679s
80 1.44593541e+04 1.44597129e+04 1.72e-06 1.11e-08 3.62e-08 689s
81 1.44593734e+04 1.44596921e+04 1.53e-06 9.87e-09 3.22e-08 699s
82 1.44593855e+04 1.44596808e+04 1.41e-06 9.17e-09 2.99e-08 711s
83 1.44594011e+04 1.44596717e+04 1.26e-06 8.61e-09 2.74e-08 722s
84 1.44594142e+04 1.44596630e+04 1.12e-06 8.09e-09 2.51e-08 732s
85 1.44594294e+04 1.44596489e+04 9.58e-07 7.26e-09 2.20e-08 744s
86 1.44594383e+04 1.44596344e+04 8.85e-07 6.38e-09 1.96e-08 753s
87 1.44594467e+04 1.44596219e+04 8.08e-07 5.64e-09 1.75e-08 763s
88 1.44594519e+04 1.44596173e+04 8.28e-07 5.37e-09 1.65e-08 774s
89 1.44594595e+04 1.44596091e+04 7.51e-07 1.50e-07 1.50e-08 784s
90 1.44594692e+04 1.44595993e+04 5.51e-06 3.52e-07 1.29e-08 794s
91 1.44594755e+04 1.44595869e+04 4.97e-06 2.90e-07 1.11e-08 804s
92 1.44594791e+04 1.44595842e+04 4.68e-06 2.76e-07 1.05e-08 810s
93 1.44594847e+04 1.44595812e+04 4.18e-06 3.10e-07 9.59e-09 819s
94 1.44594859e+04 1.44595798e+04 4.07e-06 3.00e-07 9.34e-09 825s
95 1.44594907e+04 1.44595746e+04 3.66e-06 2.67e-07 8.36e-09 833s
96 1.44594982e+04 1.44595708e+04 2.97e-06 2.72e-07 7.22e-09 842s
97 1.44594995e+04 1.44595688e+04 2.87e-06 2.47e-07 6.89e-09 848s
98 1.44594995e+04 1.44595688e+04 2.87e-06 2.47e-07 6.89e-09 855s
99 1.44595019e+04 1.44595651e+04 2.66e-06 3.95e-07 6.29e-09 863s
100 1.44595042e+04 1.44595616e+04 2.46e-06 3.52e-07 5.67e-09 871s
101 1.44595047e+04 1.44595605e+04 2.41e-06 3.51e-07 5.50e-09 877s
102 1.44595047e+04 1.44595605e+04 2.41e-06 3.37e-07 5.50e-09 883s
103 1.44595057e+04 1.44595595e+04 2.33e-06 3.74e-07 5.32e-09 890s
104 1.44595075e+04 1.44595581e+04 4.41e-06 3.54e-07 5.00e-09 896s
105 1.44595084e+04 1.44595555e+04 4.25e-06 3.26e-07 4.65e-09 902s
106 1.44595089e+04 1.44595549e+04 4.16e-06 3.38e-07 4.54e-09 910s
107 1.44595117e+04 1.44595536e+04 3.66e-06 3.15e-07 4.13e-09 917s
108 1.44595121e+04 1.44595531e+04 3.59e-06 3.10e-07 4.05e-09 923s
109 1.44595121e+04 1.44595531e+04 3.59e-06 2.98e-07 4.05e-09 929s
110 1.44595126e+04 1.44595526e+04 3.51e-06 2.91e-07 3.95e-09 934s
111 1.44595134e+04 1.44595522e+04 3.38e-06 2.88e-07 3.84e-09 942s
112 1.44595136e+04 1.44595516e+04 3.35e-06 2.75e-07 3.76e-09 948s
113 1.44595136e+04 1.44595515e+04 3.35e-06 2.77e-07 3.75e-09 954s
114 1.44595141e+04 1.44595509e+04 3.17e-06 2.64e-07 3.63e-09 960s
115 1.44595158e+04 1.44595493e+04 2.87e-06 2.88e-07 3.30e-09 968s
116 1.44595161e+04 1.44595489e+04 2.83e-06 2.79e-07 3.24e-09 976s
117 1.44595161e+04 1.44595489e+04 2.83e-06 2.85e-07 3.23e-09 982s
118 1.44595162e+04 1.44595487e+04 2.81e-06 2.80e-07 3.20e-09 989s
119 1.44595166e+04 1.44595486e+04 2.88e-06 2.80e-07 3.16e-09 994s
120 1.44595167e+04 1.44595485e+04 2.86e-06 2.78e-07 3.14e-09 1000s
121 1.44595170e+04 1.44595476e+04 2.73e-06 2.52e-07 3.01e-09 1006s
122 1.44595170e+04 1.44595476e+04 2.73e-06 2.62e-07 3.01e-09 1013s
123 1.44595170e+04 1.44595476e+04 2.73e-06 2.62e-07 3.01e-09 1019s
124 1.44595196e+04 1.44595452e+04 2.22e-06 2.00e-07 2.52e-09 1027s
125 1.44595198e+04 1.44595448e+04 2.33e-06 1.93e-07 2.46e-09 1034s
126 1.44595198e+04 1.44595448e+04 2.33e-06 1.94e-07 2.46e-09 1040s
127 1.44595198e+04 1.44595448e+04 2.33e-06 1.95e-07 2.46e-09 1046s
128 1.44595199e+04 1.44595447e+04 2.34e-06 1.92e-07 2.45e-09 1053s
129 1.44595200e+04 1.44595447e+04 2.32e-06 2.00e-07 2.43e-09 1059s
130 1.44595200e+04 1.44595447e+04 2.32e-06 2.34e-07 2.43e-09 1067s
131 1.44595206e+04 1.44595441e+04 2.21e-06 2.23e-07 2.32e-09 1074s
132 1.44595208e+04 1.44595438e+04 2.17e-06 2.40e-07 2.27e-09 1081s
133 1.44595208e+04 1.44595438e+04 2.17e-06 2.45e-07 2.27e-09 1088s
134 1.44595208e+04 1.44595437e+04 2.17e-06 2.46e-07 2.26e-09 1094s
135 1.44595224e+04 1.44595426e+04 4.83e-06 2.20e-07 2.00e-09 1103s
136 1.44595224e+04 1.44595426e+04 4.83e-06 2.19e-07 2.00e-09 1110s
137 1.44595224e+04 1.44595426e+04 4.83e-06 2.20e-07 2.00e-09 1117s
138 1.44595224e+04 1.44595426e+04 4.83e-06 2.20e-07 2.00e-09 1124s
139 1.44595226e+04 1.44595418e+04 4.70e-06 2.04e-07 1.90e-09 1130s
140 1.44595226e+04 1.44595418e+04 4.70e-06 2.04e-07 1.90e-09 1137s
141 1.44595227e+04 1.44595418e+04 4.68e-06 1.90e-07 1.89e-09 1144s
142 1.44595227e+04 1.44595418e+04 4.68e-06 1.99e-07 1.89e-09 1151s
143 1.44595227e+04 1.44595418e+04 4.68e-06 1.97e-07 1.88e-09 1158s
144 1.44595227e+04 1.44595417e+04 4.67e-06 2.04e-07 1.88e-09 1165s
145 1.44595227e+04 1.44595417e+04 4.67e-06 2.04e-07 1.88e-09 1172s
Barrier performed 145 iterations in 1172.31 seconds (904.85 work units)
Sub-optimal termination - objective 1.44594467e+04
However I don´t get why my calculation returns a wrong result. Is there something I am missing?
I also tried to remove most of the parameters I set and encountered that this solves the problem. But I have no idea why this should change anything...
Thank you very much in advance!
0
-
Hi Christian,
it might be possible that you are missing the objective constant.
double objcon = 0.0;
error = GRBGetdblattr(model, "ObjCon", &objcon);
for (i = 0; i < n_vars; i++) { total_obj_by_hand += obj[i] * var_values[i]; }
total_obj_by_hand += objcon;Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
1 comment