Same code but different results

48 views
Skip to first unread message

Leo Young

unread,
May 20, 2025, 10:56:07 AMMay 20
to YALMIP
Hi Prof. Johan Löfberg,

I hope you are doing well.

Recently, I found that the same code got different results on my macbook and windows PC,
where the matlab versions and yalmip versions are differnt (Matlab R2024a+yalmip'20230622' for windows and Matlab R2020b+yalmip'20210331' for macbook), while gurobi is the same (10.0.3).

It could be understood if there are some minor tolerance difference, however, the results from two platforms are totally different.
To be specific, I got -6.693404915092e6 for windows, and -1.034687874883e+07 for macbook.

I also upload the code and data used in the attachment, I hope that could be useful for you to understand the issues that I met.

Best regards,
Liu
untitled1.zip

Johan Löfberg

unread,
May 21, 2025, 3:48:01 AMMay 21
to YALMIP
You should use the most recent version of YALMIP (develop branch), and absolutely not use gurobi 10 since many bugs have been cleaned up in the very hard mixed-integer nonconvex quadratic solver since that version


Optimize a model with 5304 rows, 3541 columns and 9384 nonzeros
Model fingerprint: 0x886e9b8d
Model has 504 quadratic constraints
Variable types: 3253 continuous, 288 integer (288 binary)
Coefficient statistics:
  Matrix range     [1e-02, 3e+00]
  QMatrix range    [1e+00, 5e+00]
  QLMatrix range   [1e+00, 5e+00]
  Objective range  [5e-01, 2e+02]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 5e+04]
Presolve removed 4407 rows and 2484 columns
Presolve time: 0.05s
Presolved: 1465 rows, 1505 columns, 4099 nonzeros
Presolved model has 288 SOS constraint(s)
Variable types: 1201 continuous, 304 integer (304 binary)
Found heuristic solution: objective -8136910.337

Root relaxation: objective -1.058233e+07, 750 iterations, 0.02 seconds (0.01 work units)

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0 -1.058e+07    0   21 -8136910.3 -1.058e+07  30.1%     -    0s
H    0     0                    -1.03739e+07 -1.058e+07  2.01%     -    0s
     0     0 -1.058e+07    0   21 -1.037e+07 -1.058e+07  1.95%     -    0s
     0     0 -1.058e+07    0   21 -1.037e+07 -1.058e+07  1.94%     -    0s
     0     0 -1.057e+07    0   24 -1.037e+07 -1.057e+07  1.92%     -    0s
     0     0 -1.057e+07    0   23 -1.037e+07 -1.057e+07  1.91%     -    0s
     0     0 -1.057e+07    0   23 -1.037e+07 -1.057e+07  1.90%     -    0s
H    0     0                    -1.03739e+07 -1.055e+07  1.74%     -    0s
H    0     0                    -1.03790e+07 -1.055e+07  1.69%     -    0s
     0     0 -1.052e+07    0   25 -1.038e+07 -1.052e+07  1.36%     -    0s
     0     0 -1.050e+07    0   22 -1.038e+07 -1.050e+07  1.17%     -    0s

Cutting planes:
  Gomory: 5
  Implied bound: 1
  MIR: 11
  Flow cover: 17

Explored 1 nodes (944 simplex iterations) in 0.29 seconds (0.10 work units)
Thread count was 8 (of 8 available processors)

Solution count 5: -1.0379e+07 -1.03739e+07 -1.03739e+07 ... -8.13691e+06

Leo Young

unread,
May 21, 2025, 11:13:44 AMMay 21
to YALMIP
Thanks for your response Prof. Johan Löfberg, I've tried to change the gurobi versions, but still got similar results on the windows system as below. According to the results that you've got, I can tell it's quite close to my macbook version results, so maybe something weird is happening with my PC. 

Yalmip develop version + Gurobi 11.0.2
Set parameter Username
Set parameter LicenseID to value 2669230
Set parameter MIPGap to value 0.01
Set parameter NodefileDir to value ""
Set parameter NonConvex to value 2
Set parameter FuncNonlinear to value 1
Academic license - for non-commercial use only - expires 2026-05-21
Gurobi Optimizer version 11.0.2 build v11.0.2rc0 (win64 - Windows 11+.0 (26100.2))

CPU model: 11th Gen Intel(R) Core(TM) i7-11700KF @ 3.60GHz, instruction set [SSE2|AVX|AVX2|AVX512]
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads

Optimize a model with 5232 rows, 3541 columns and 9384 nonzeros
Model fingerprint: 0x8ee7bd83

Model has 504 quadratic constraints
Variable types: 3253 continuous, 288 integer (288 binary)
Coefficient statistics:
  Matrix range     [1e-02, 3e+00]
  QMatrix range    [1e+00, 5e+00]
  QLMatrix range   [1e+00, 5e+00]
  Objective range  [5e-01, 2e+02]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 5e+04]
Presolve removed 4335 rows and 2412 columns
Presolve time: 0.01s
Presolved: 1465 rows, 1577 columns, 4171 nonzeros

Presolved model has 288 SOS constraint(s)
Variable types: 1273 continuous, 304 integer (304 binary)
Found heuristic solution: objective -5888802.934

Root relaxation: objective -6.856245e+06, 771 iterations, 0.01 seconds (0.01 work units)


    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0 -6856245.1    0   20 -5888802.9 -6856245.1  16.4%     -    0s
H    0     0                    -5888927.342 -6856245.1  16.4%     -    0s
     0     0 -6851052.9    0   24 -5888927.3 -6851052.9  16.3%     -    0s
     0     0 -6850912.0    0   23 -5888927.3 -6850912.0  16.3%     -    0s
H    0     0                    -6532375.108 -6850912.0  4.88%     -    0s
     0     0 -6848376.6    0   21 -6532375.1 -6848376.6  4.84%     -    0s
     0     0 -6809176.9    0   20 -6532375.1 -6809176.9  4.24%     -    0s
     0     0 -6808285.9    0   20 -6532375.1 -6808285.9  4.22%     -    0s
     0     0 -6758170.9    0   23 -6532375.1 -6758170.9  3.46%     -    0s
     0     0 -6726985.5    0   20 -6532375.1 -6726985.5  2.98%     -    0s
     0     0 -6715499.1    0   21 -6532375.1 -6715499.1  2.80%     -    0s
H    0     0                    -6656672.484 -6705133.9  0.73%     -    0s
H    0     0                    -6661802.713 -6705133.9  0.65%     -    0s
H    0     0                    -6665907.499 -6705133.9  0.59%     -    0s
H    0     0                    -6667257.302 -6705133.9  0.57%     -    0s
H    0     0                    -6672387.530 -6705133.9  0.49%     -    0s
H    0     0                    -6676492.316 -6705133.9  0.43%     -    0s

Cutting planes:
  Gomory: 3
  MIR: 16
  Flow cover: 50

Explored 1 nodes (922 simplex iterations) in 0.20 seconds (0.11 work units)
Thread count was 16 (of 16 available processors)

Solution count 9: -6.67649e+06 -6.67239e+06 -6.66726e+06 ... -5.8888e+06

Optimal solution found (tolerance 1.00e-02)
Best objective -6.676492316480e+06, best bound -6.705133946160e+06, gap 0.4290%
Yalmip develop version + Gurobi 12.0.2
Set parameter Username
Set parameter LicenseID to value 2669230
Set parameter MIPGap to value 0.01
Set parameter NodefileDir to value ""
Set parameter NonConvex to value 2
Academic license - for non-commercial use only - expires 2026-05-21
Gurobi Optimizer version 12.0.2 build v12.0.2rc0 (win64 - Windows 11.0 (26100.2))

CPU model: 11th Gen Intel(R) Core(TM) i7-11700KF @ 3.60GHz, instruction set [SSE2|AVX|AVX2|AVX512]
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads

Non-default parameters:
MIPGap  0.01
LogToConsole  0
NonConvex  2

Optimize a model with 5232 rows, 3541 columns and 9384 nonzeros
Model fingerprint: 0x8ee7bd83

Model has 504 quadratic constraints
Variable types: 3253 continuous, 288 integer (288 binary)
Coefficient statistics:
  Matrix range     [1e-02, 3e+00]
  QMatrix range    [1e+00, 5e+00]
  QLMatrix range   [1e+00, 5e+00]
  Objective range  [5e-01, 2e+02]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 5e+04]
Presolve removed 4335 rows and 2412 columns
Presolve time: 0.01s
Presolved: 1465 rows, 1577 columns, 4171 nonzeros

Presolved model has 288 SOS constraint(s)
Variable types: 1273 continuous, 304 integer (304 binary)
Found heuristic solution: objective -4366197.272

Root relaxation: objective -6.856245e+06, 774 iterations, 0.01 seconds (0.01 work units)


    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0 -6856245.1    0   21 -4366197.3 -6856245.1  57.0%     -    0s
H    0     0                    -6672598.602 -6856245.1  2.75%     -    0s
H    0     0                    -6687117.067 -6856245.1  2.53%     -    0s
     0     0 -6850986.6    0   20 -6687117.1 -6850986.6  2.45%     -    0s
     0     0 -6848392.1    0   21 -6687117.1 -6848392.1  2.41%     -    0s
     0     0 -6808185.3    0   21 -6687117.1 -6808185.3  1.81%     -    0s
     0     0 -6806372.6    0   21 -6687117.1 -6806372.6  1.78%     -    0s

Cutting planes:
  Gomory: 1
  Implied bound: 1
  MIR: 13
  Flow cover: 72

Explored 1 nodes (868 simplex iterations) in 0.16 seconds (0.10 work units)
Thread count was 16 (of 16 available processors)

Solution count 4: -6.68712e+06 -6.6726e+06 -4.3662e+06 -4.3662e+06

Optimal solution found (tolerance 1.00e-02)
Best objective -6.687117067468e+06, best bound -6.725091583588e+06, gap 0.5679%

Johan Löfberg

unread,
May 21, 2025, 11:24:40 AMMay 21
to YALMIP
what does the log look like on the macbook

Leo Young

unread,
May 21, 2025, 11:37:17 AMMay 21
to YALMIP
Hi Prof. Johan Löfberg,

Since I haven't tried to change the solver nor the yalmip version yet on this macbook, that's what I have right now as below.

Yalmip 20210331+gurobi 10.0.3
Set parameter Username

Set parameter MIPGap to value 0.01
Set parameter CrossoverBasis to value 0

Set parameter NodefileDir to value ""
Set parameter NonConvex to value 2
Set parameter PreSOS2BigM to value 0
Set parameter TuneTrials to value 3
Academic license - for non-commercial use only - expires 2025-09-09
Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (mac64[rosetta2])

CPU model: Apple M1
Thread count: 8 physical cores, 8 logical processors, using up to 8 threads

Optimize a model with 5304 rows, 3253 columns and 9384 nonzeros
Model fingerprint: 0x67f0a016

Model has 504 quadratic constraints
Variable types: 2965 continuous, 288 integer (288 binary)
Coefficient statistics:
  Matrix range     [1e-02, 2e+00]

  QMatrix range    [1e+00, 5e+00]
  QLMatrix range   [1e+00, 5e+00]
  Objective range  [5e-01, 2e+02]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 5e+04]
Presolve removed 4407 rows and 2196 columns
Presolve time: 0.03s
Presolved: 1537 rows, 1361 columns, 4243 nonzeros
Presolved model has 144 SOS constraint(s)
Variable types: 1129 continuous, 232 integer (232 binary)
Found heuristic solution: objective -9540398.060

Root relaxation: objective -1.058233e+07, 792 iterations, 0.01 seconds (0.01 work units)


    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0 -1.058e+07    0   22 -9540398.1 -1.058e+07  10.9%     -    0s
     0     0 -1.058e+07    0   23 -9540398.1 -1.058e+07  10.9%     -    0s
     0     0 -1.054e+07    0   23 -9540398.1 -1.054e+07  10.4%     -    0s
     0     0 -1.053e+07    0   23 -9540398.1 -1.053e+07  10.4%     -    0s
H    0     0                    -9874711.499 -1.053e+07  6.66%     -    0s
     0     0 -1.052e+07    0   22 -9874711.5 -1.052e+07  6.55%     -    0s
     0     0 -1.052e+07    0   23 -9874711.5 -1.052e+07  6.55%     -    0s
H    0     0                    -1.01252e+07 -1.052e+07  3.91%     -    0s
     0     0 -1.047e+07    0   22 -1.013e+07 -1.047e+07  3.43%     -    0s
H    0     0                    -1.01588e+07 -1.044e+07  2.75%     -    0s
H    0     0                    -1.03469e+07 -1.044e+07  0.88%     -    0s
     0     0 -1.044e+07    0   24 -1.035e+07 -1.044e+07  0.88%     -    0s

Cutting planes:
  Gomory: 3
  MIR: 11
  Flow cover: 23

Explored 1 nodes (926 simplex iterations) in 0.27 seconds (0.08 work units)

Thread count was 8 (of 8 available processors)

Solution count 5: -1.03469e+07 -1.01588e+07 -1.01252e+07 ... -9.5404e+06


Optimal solution found (tolerance 1.00e-02)
Best objective -1.034687874883e+07, best bound -1.043844173063e+07, gap 0.8849%

Johan Löfberg

unread,
May 21, 2025, 11:42:21 AMMay 21
to YALMIP
Ok.

For some reason, the model YALMIP sends appears to be different on your PC. Even weirder, there are fewer constraints but objective is worse

Optimize a model with 5304 rows can be seen on my machine, and your mac.

It would be interesting if you put a break in callgurobi and look at interfacedata.K and report that on your two machines

Even better, save interfacedata to a file and post that file for debugging

Leo Young

unread,
May 21, 2025, 12:11:45 PMMay 21
to YALMIP
Hi Prof.  Johan Löfberg,

Here are the two interfacedata files as in the attachements, hope these would be helpful. It seems the only difference is the F_struc, but I cannot see more information further.

interfacedata_windows.mat
interfacedata_mac.mat

Johan Löfberg

unread,
May 21, 2025, 12:28:41 PMMay 21
to YALMIP
And just to rule out the possibility, are you 100% sure that you run the same file and use the same data on the two machines

Leo Young

unread,
May 21, 2025, 12:35:21 PMMay 21
to YALMIP
Yes, I've just tried to download the same files that I had uploaded on the forum, it still kept the same results on the windows pc.

Johan Löfberg

unread,
May 21, 2025, 1:20:03 PMMay 21
to YALMIP
Your PC model starts to differ after the 3780th inequality.

The 3781st inequality is added in this code
            con = [con, p_imp(d,:) + p_PV_use(d,:) + p_WT_use(d,:) + p_FC(d,:) == ...
                        p_EC(d,:) + p_EH(d,:) + p_Elz(d,:) + p_PSS(d,:) + p_exp(d,:) +...
                        p_ld_elc_std(d,:) + p_CP(d,:) - un_elc_ld(d,:)];

But that's an equality! Nope

K>> [p_imp(d,:) + p_PV_use(d,:) + p_WT_use(d,:) + p_FC(d,:) == ...
                        p_EC(d,:) + p_EH(d,:) + p_Elz(d,:) + p_PSS(d,:) + p_exp(d,:) +...
                        p_ld_elc_std(d,:) + p_CP(d,:) - un_elc_ld(d,:)]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|   ID|                     Constraint|   Coefficient range|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|   #1|       Equality constraint 1x24|              1 to 1|
|   #2|   Element-wise inequality 24x1|           1 to 1344|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

It is basically this which actually gives a warning now, as it is a very common mistake (not sure why it doesn't warn on the more complex expression)
[sdpvar(1,24) == ...
                        sdpvar(1,24) +...
                        sdpvar(1,24)]
Most likely you meant to write a + b, but you wrote a +b
This can easily lead to bugs, as [a +b] is a vector with two elements
If you really want to use unitary plus, you will have to edit sdpvar/uplus
and delete this message
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|   ID|                     Constraint|   Coefficient range|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|   #1|       Equality constraint 1x24|              1 to 1|
|   #2|   Element-wise inequality 24x1|              1 to 1|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 You are essentially doing this

K>> [1 == ...
                        1 +...
                        1]

ans =

     1     1


which is equivalent to [1 == 1, +1]

However, that should happen on all machines, the thing is it appears to behave differently on your PC?  Hence, put a break on line 656 and display con, and it should look something like

|   #294|              Element-wise inequality 144x1|              1 to 1|
|   #295|              Element-wise inequality 144x1|          1 to 12600|
|   #296|                   Equality constraint 1x24|              1 to 1|
|   #297|               Element-wise inequality 24x1|           1 to 1344|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

with 296 and 297 being the split up intended equality, and my only guess at the moment is that your PC returns something different, i.e. the parser in MATLAB on that machine is doing something different

Leo Young

unread,
May 21, 2025, 2:30:35 PMMay 21
to YALMIP
Hi Prof. Johan Löfberg,

Thanks for clarifying the problem, I do have differnt constraints at the mentioned break point, and thereby it lacks 24*3 (3 iterations here for parameter d) constraints in total.
I think that's why I have 5232 constraints only with my PC, but after modifying several times the format that I use in the coding, it keeps the same situation as before (even though I completely delete all the "...").

|   #294|              Element-wise inequality 144x1|              1 to 1|
|   #295|              Element-wise inequality 144x1|          1 to 12600|
|   #296|                   Equality constraint 1x24|           1 to 1344|
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Johan Löfberg

unread,
May 21, 2025, 3:07:31 PMMay 21
to YALMIP
When I clean up your code so that an equality is defined, my machine coincides with your pc, i.e objective -6.69e6

I.e, for some reason, your matlab version on your pc interprets

[x == y +...
          z]

as x==y+z

in contrast to my pc and your mac where it is interpretd as [x == y +z] i.e [x==y, z] which the correct way as I understand (but very common cause for bugs when that space is missing)

Leo Young

unread,
May 21, 2025, 3:27:15 PMMay 21
to YALMIP
Ok, that's fine, so it means -6.69e6 would be the correct answer if I didn't make it wrong. 
Anyhow, thank you so much for all the help that you've supported, I really appreciate it!

Johan Löfberg

unread,
May 21, 2025, 3:33:12 PMMay 21
to YALMIP
Yes, unless there are other bugs in your code, or in gurobi etc
Reply all
Reply to author
Forward
0 new messages