Skip to content

Commit

Permalink
Bypassed Gurobi's issue with np.inner
Browse files Browse the repository at this point in the history
  • Loading branch information
Foggalong committed Apr 25, 2024
1 parent c854716 commit c82f19e
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 210 deletions.
74 changes: 37 additions & 37 deletions Example2/n2-test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
" 6 1.74999618e+00 1.75000030e+00 0.00e+00 0.00e+00 2.06e-06 0s\n",
" 7 1.75000000e+00 1.75000000e+00 0.00e+00 0.00e+00 2.06e-09 0s\n",
"\n",
"Barrier solved model in 7 iterations and 0.03 seconds (0.00 work units)\n",
"Barrier solved model in 7 iterations and 0.02 seconds (0.00 work units)\n",
"Optimal objective 1.75000000e+00\n",
"\n",
"Standard: [0.00000001 0.99999999]\n"
Expand Down Expand Up @@ -197,9 +197,9 @@
"Thread count: 4 physical cores, 8 logical processors, using up to 8 threads\n",
"\n",
"Optimize a model with 1 rows, 3 columns and 2 nonzeros\n",
"Model fingerprint: 0xd120428b\n",
"Model fingerprint: 0xea4b60b6\n",
"Model has 2 quadratic objective terms\n",
"Model has 2 quadratic constraints\n",
"Model has 1 quadratic constraint\n",
"Coefficient statistics:\n",
" Matrix range [1e+00, 1e+00]\n",
" QMatrix range [1e-01, 4e+00]\n",
Expand All @@ -208,30 +208,31 @@
" Bounds range [0e+00, 0e+00]\n",
" RHS range [1e+00, 1e+00]\n",
"Presolve time: 0.01s\n",
"Presolved: 7 rows, 8 columns, 12 nonzeros\n",
"Presolved model has 3 second-order cone constraints\n",
"Presolved: 6 rows, 7 columns, 10 nonzeros\n",
"Presolved model has 2 second-order cone constraints\n",
"Ordering time: 0.00s\n",
"\n",
"Barrier statistics:\n",
" AA' NZ : 1.700e+01\n",
" Factor NZ : 2.800e+01\n",
" Factor Ops : 1.400e+02 (less than 1 second per iteration)\n",
" AA' NZ : 1.500e+01\n",
" Factor NZ : 2.100e+01\n",
" Factor Ops : 9.100e+01 (less than 1 second per iteration)\n",
" Threads : 1\n",
"\n",
" Objective Residual\n",
"Iter Primal Dual Primal Dual Compl Time\n",
" 0 -2.28881626e-02 1.22656250e+00 9.39e-01 1.43e-01 3.26e-01 0s\n",
" 1 6.91883948e-01 1.06699842e+00 1.98e-01 1.57e-07 5.84e-02 0s\n",
" 2 6.44324078e-01 7.21385315e-01 2.17e-07 1.73e-13 7.71e-03 0s\n",
" 3 6.67762956e-01 6.69433547e-01 6.21e-09 6.66e-16 1.67e-04 0s\n",
" 4 6.68355745e-01 6.68371340e-01 1.53e-13 2.00e-15 1.56e-06 0s\n",
" 5 6.68366724e-01 6.68367382e-01 3.57e-11 6.05e-13 6.58e-08 0s\n",
" 0 -5.73852130e-02 1.22656250e+00 8.97e-01 1.41e-01 3.51e-01 0s\n",
" 1 5.91715790e-01 9.52821847e-01 2.13e-01 1.51e-07 6.38e-02 0s\n",
" 2 5.15372316e-01 5.96285470e-01 2.34e-07 6.30e-10 8.99e-03 0s\n",
" 3 5.49225634e-01 5.53202935e-01 4.81e-10 1.05e-15 4.42e-04 0s\n",
" 4 5.52127256e-01 5.52382277e-01 1.12e-13 5.55e-16 2.83e-05 0s\n",
" 5 5.52206847e-01 5.52232334e-01 3.52e-12 3.66e-15 2.83e-06 0s\n",
" 6 5.52216328e-01 5.52218236e-01 1.38e-11 2.96e-14 2.12e-07 0s\n",
"\n",
"Barrier solved model in 5 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective 6.68366724e-01\n",
"Barrier solved model in 6 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective 5.52216328e-01\n",
"\n",
"Standard: [0.00000001 0.99999999]\n",
"Robust: [0.85714285 0.14285715]\n"
"Robust: [0.83155134 0.16844866]\n"
]
}
],
Expand All @@ -253,7 +254,7 @@
"# add sum-to-half constraints to both models\n",
"model_rbs.addConstr(np.ones([1,2]) @ w_std == 1, name=\"sum-to-one\")\n",
"# add quadratic uncertainty constraint to the robust model\n",
"model_rbs.addConstr(z_rbs**2 >= np.inner(w_rbs, omega@w_rbs), name=\"uncertainty\")\n",
"model_rbs.addConstr(z_rbs**2 >= w_rbs.transpose()@omega@w_rbs, name=\"uncertainty\")\n",
"\n",
"# solve problem with Gurobi, print alongside standard solution for comparison\n",
"model_rbs.optimize()\n",
Expand All @@ -265,7 +266,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that if $\\kappa = 0$, the standard solution not _exactly_ recovered, only accurate to eight decimal places."
"Note that if $\\kappa = 0$, the standard solution not _exactly_ recovered, only accurate to seven decimal places."
]
},
{
Expand All @@ -283,9 +284,9 @@
"Thread count: 4 physical cores, 8 logical processors, using up to 8 threads\n",
"\n",
"Optimize a model with 1 rows, 3 columns and 2 nonzeros\n",
"Model fingerprint: 0x6c4430f5\n",
"Model fingerprint: 0xcd1e4542\n",
"Model has 2 quadratic objective terms\n",
"Model has 2 quadratic constraints\n",
"Model has 1 quadratic constraint\n",
"Coefficient statistics:\n",
" Matrix range [1e+00, 1e+00]\n",
" QMatrix range [1e-01, 4e+00]\n",
Expand All @@ -294,31 +295,30 @@
" Bounds range [0e+00, 0e+00]\n",
" RHS range [1e+00, 1e+00]\n",
"Presolve time: 0.01s\n",
"Presolved: 7 rows, 8 columns, 12 nonzeros\n",
"Presolved model has 3 second-order cone constraints\n",
"Presolved: 6 rows, 7 columns, 10 nonzeros\n",
"Presolved model has 2 second-order cone constraints\n",
"Ordering time: 0.00s\n",
"\n",
"Barrier statistics:\n",
" AA' NZ : 1.700e+01\n",
" Factor NZ : 2.800e+01\n",
" Factor Ops : 1.400e+02 (less than 1 second per iteration)\n",
" AA' NZ : 1.500e+01\n",
" Factor NZ : 2.100e+01\n",
" Factor Ops : 9.100e+01 (less than 1 second per iteration)\n",
" Threads : 1\n",
"\n",
" Objective Residual\n",
"Iter Primal Dual Primal Dual Compl Time\n",
" 0 1.22656250e+00 1.22656250e+00 1.04e+00 6.81e-01 5.08e-01 0s\n",
" 1 1.54200502e+00 1.52822126e+00 4.89e-02 1.42e-01 7.24e-02 0s\n",
" 2 1.64909037e+00 1.63852570e+00 5.38e-08 3.21e-02 1.66e-02 0s\n",
" 3 1.74359514e+00 1.75057927e+00 3.23e-10 8.27e-05 7.48e-04 0s\n",
" 4 1.74999141e+00 1.75002072e+00 1.01e-12 9.47e-08 2.99e-06 0s\n",
" 5 1.74999468e+00 1.75000778e+00 3.11e-12 3.00e-08 1.33e-06 0s\n",
" 6 1.74999882e+00 1.75000092e+00 1.06e-11 1.95e-09 2.11e-07 0s\n",
" 0 1.22656250e+00 1.22656250e+00 1.08e+00 6.94e-01 4.95e-01 0s\n",
" 1 1.61470687e+00 1.61496484e+00 5.67e-02 1.55e-01 6.97e-02 0s\n",
" 2 1.67968985e+00 1.70168242e+00 6.24e-08 2.02e-02 1.10e-02 0s\n",
" 3 1.74763426e+00 1.75020504e+00 7.06e-14 2.52e-05 2.97e-04 0s\n",
" 4 1.74999548e+00 1.75001176e+00 1.87e-14 2.84e-08 1.82e-06 0s\n",
" 5 1.74999978e+00 1.75000067e+00 4.92e-13 9.33e-10 9.90e-08 0s\n",
"\n",
"Barrier solved model in 6 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective 1.74999882e+00\n",
"Barrier solved model in 5 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective 1.74999978e+00\n",
"\n",
"Standard: [0.0000000076 0.9999999924]\n",
"Robust: [0.0000000230 0.9999999770]\n"
"Robust: [0.0000001531 0.9999998469]\n"
]
}
],
Expand All @@ -340,7 +340,7 @@
"# add sum-to-half constraints to both models\n",
"model_rbs.addConstr(np.ones([1,2]) @ w_std == 1, name=\"sum-to-one\")\n",
"# add quadratic uncertainty constraint to the robust model\n",
"model_rbs.addConstr(z_rbs**2 >= np.inner(w_rbs, omega@w_rbs), name=\"uncertainty\")\n",
"model_rbs.addConstr(z_rbs**2 >= w_rbs.transpose()@omega@w_rbs, name=\"uncertainty\")\n",
"\n",
"# solve problem with Gurobi, print alongside standard solution for comparison\n",
"model_rbs.optimize()\n",
Expand Down
16 changes: 13 additions & 3 deletions Scratch/numerical-error.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
"source": [
"# Numerical Error\n",
"\n",
"The purpose of this notebook is to illustrate the numerical error which comes from setting $\\kappa = 0$."
"The purpose of this notebook is to illustrate the numerical error which comes from setting $\\kappa = 0$ and using \n",
"\n",
"```python3\n",
"model.addConstr(z**2 <= np.inner(w, omega@w), name=\"uncertainty\")\n",
"```\n",
"as the cone constraint rather than\n",
"```python3\n",
"model.addConstr(z**2 <= w.transpose()@omega@w, name=\"uncertainty\")\n",
"```\n",
"This has been fixed in the latest version of all other notebooks."
]
},
{
Expand Down Expand Up @@ -150,7 +159,7 @@
" 7 -8.12500000e-01 -8.12500040e-01 0.00e+00 2.78e-17 1.01e-08 0s\n",
" 8 -8.12500000e-01 -8.12500000e-01 2.22e-16 5.55e-17 1.01e-11 0s\n",
"\n",
"Barrier solved model in 8 iterations and 0.04 seconds (0.00 work units)\n",
"Barrier solved model in 8 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective -8.12500000e-01\n",
"\n",
"w = [0.50000 0.37500 0.12500]\n"
Expand Down Expand Up @@ -287,6 +296,7 @@
"\n",
"# add quadratic uncertainty constraint\n",
"model.addConstr(z**2 <= np.inner(w, omega@w), name=\"uncertainty\")\n",
"# model.addConstr(z**2 <= w.transpose()@omega@w, name=\"uncertainty\")\n",
"# add sub-to-half constraints\n",
"model.addConstr(M @ w == m, name=\"sum-to-half\")\n",
"# add weight-bound constraints~\n",
Expand All @@ -302,7 +312,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that even though $\\kappa = 0$, we have $z\\neq0$. This suggests that a small amount of numerical error has been introduced as a result of asking Gurobi to optimize $z$ as well as $w$. "
"Note that even though $\\kappa = 0$, we have $z\\neq0$. This suggests that a small amount of numerical error has been introduced."
]
}
],
Expand Down
Loading

0 comments on commit c82f19e

Please sign in to comment.