Skip to content

Commit

Permalink
Incorporate bounds into variable definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Foggalong committed Apr 25, 2024
1 parent d70735f commit c854716
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 94 deletions.
28 changes: 12 additions & 16 deletions Example2/n2-test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@
"CPU model: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz, instruction set [SSE2|AVX|AVX2]\n",
"Thread count: 4 physical cores, 8 logical processors, using up to 8 threads\n",
"\n",
"Optimize a model with 2 rows, 3 columns and 3 nonzeros\n",
"Model fingerprint: 0x41322026\n",
"Optimize a model with 1 rows, 3 columns and 2 nonzeros\n",
"Model fingerprint: 0xd120428b\n",
"Model has 2 quadratic objective terms\n",
"Model has 2 quadratic constraints\n",
"Coefficient statistics:\n",
Expand All @@ -207,8 +207,7 @@
" QObjective range [5e-01, 5e-01]\n",
" Bounds range [0e+00, 0e+00]\n",
" RHS range [1e+00, 1e+00]\n",
"Presolve removed 1 rows and 0 columns\n",
"Presolve time: 0.04s\n",
"Presolve time: 0.01s\n",
"Presolved: 7 rows, 8 columns, 12 nonzeros\n",
"Presolved model has 3 second-order cone constraints\n",
"Ordering time: 0.00s\n",
Expand All @@ -228,7 +227,7 @@
" 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",
"\n",
"Barrier solved model in 5 iterations and 0.05 seconds (0.00 work units)\n",
"Barrier solved model in 5 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective 6.68366724e-01\n",
"\n",
"Standard: [0.00000001 0.99999999]\n",
Expand All @@ -244,8 +243,8 @@
"model_rbs = gp.Model(\"n02robust\")\n",
"\n",
"# initialise w for both models, z for robust model \n",
"w_rbs = model_rbs.addMVar(shape=n, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z_rbs = model_rbs.addVar(name=\"z\")\n",
"w_rbs = model_rbs.addMVar(shape=n, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z_rbs = model_rbs.addVar(lb=0.0, name=\"z\")\n",
"\n",
"model_rbs.setObjective(\n",
" w_rbs.transpose()@mubar - (lam/2)*w_rbs.transpose()@(sigma@w_rbs) - kappa*z_rbs,\n",
Expand All @@ -255,7 +254,6 @@
"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 >= 0, name=\"z positive\")\n",
"\n",
"# solve problem with Gurobi, print alongside standard solution for comparison\n",
"model_rbs.optimize()\n",
Expand Down Expand Up @@ -284,8 +282,8 @@
"CPU model: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz, instruction set [SSE2|AVX|AVX2]\n",
"Thread count: 4 physical cores, 8 logical processors, using up to 8 threads\n",
"\n",
"Optimize a model with 2 rows, 3 columns and 3 nonzeros\n",
"Model fingerprint: 0xe31c34b4\n",
"Optimize a model with 1 rows, 3 columns and 2 nonzeros\n",
"Model fingerprint: 0x6c4430f5\n",
"Model has 2 quadratic objective terms\n",
"Model has 2 quadratic constraints\n",
"Coefficient statistics:\n",
Expand All @@ -295,8 +293,7 @@
" QObjective range [5e-01, 5e-01]\n",
" Bounds range [0e+00, 0e+00]\n",
" RHS range [1e+00, 1e+00]\n",
"Presolve removed 1 rows and 0 columns\n",
"Presolve time: 0.02s\n",
"Presolve time: 0.01s\n",
"Presolved: 7 rows, 8 columns, 12 nonzeros\n",
"Presolved model has 3 second-order cone constraints\n",
"Ordering time: 0.00s\n",
Expand All @@ -317,7 +314,7 @@
" 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",
"\n",
"Barrier solved model in 6 iterations and 0.06 seconds (0.00 work units)\n",
"Barrier solved model in 6 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective 1.74999882e+00\n",
"\n",
"Standard: [0.0000000076 0.9999999924]\n",
Expand All @@ -333,8 +330,8 @@
"model_rbs = gp.Model(\"n02robust_k0\")\n",
"\n",
"# initialise w for both models, z for robust model \n",
"w_rbs = model_rbs.addMVar(shape=n, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z_rbs = model_rbs.addVar(name=\"z\")\n",
"w_rbs = model_rbs.addMVar(shape=n, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z_rbs = model_rbs.addVar(lb=0.0, name=\"z\")\n",
"\n",
"model_rbs.setObjective(\n",
" w_rbs.transpose()@mubar - (lam/2)*w_rbs.transpose()@(sigma@w_rbs) - kappa*z_rbs,\n",
Expand All @@ -344,7 +341,6 @@
"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 >= 0, name=\"z positive\")\n",
"\n",
"# solve problem with Gurobi, print alongside standard solution for comparison\n",
"model_rbs.optimize()\n",
Expand Down
25 changes: 12 additions & 13 deletions Scratch/numerical-error.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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.03 seconds (0.00 work units)\n",
"Barrier solved model in 8 iterations and 0.04 seconds (0.00 work units)\n",
"Optimal objective -8.12500000e-01\n",
"\n",
"w = [0.50000 0.37500 0.12500]\n"
Expand All @@ -162,7 +162,7 @@
"model = gp.Model(\"standardGS\")\n",
"\n",
"# define variable of interest as a continuous \n",
"w = model.addMVar(shape=problem_size, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"w = model.addMVar(shape=problem_size, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"\n",
"# set the objective function\n",
"model.setObjective(\n",
Expand All @@ -172,8 +172,8 @@
"# add sub-to-half constraints\n",
"model.addConstr(M @ w == m, name=\"sum-to-half\")\n",
"# add weight-bound constraints\n",
"model.addConstr(w >= lower_bound, name=\"lower bound\")\n",
"model.addConstr(w <= upper_bound, name=\"upper bound\")\n",
"model.addConstr(w >= lower_bound, name=\"lower-bound\")\n",
"model.addConstr(w <= upper_bound, name=\"upper-bound\")\n",
"\n",
"# solve the problem with Gurobi\n",
"model.optimize()\n",
Expand Down Expand Up @@ -231,8 +231,8 @@
"CPU model: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz, instruction set [SSE2|AVX|AVX2]\n",
"Thread count: 4 physical cores, 8 logical processors, using up to 8 threads\n",
"\n",
"Optimize a model with 23 rows, 4 columns and 25 nonzeros\n",
"Model fingerprint: 0x97227d81\n",
"Optimize a model with 22 rows, 4 columns and 24 nonzeros\n",
"Model fingerprint: 0x52c6cd50\n",
"Model has 3 quadratic objective terms\n",
"Model has 3 quadratic constraints\n",
"Coefficient statistics:\n",
Expand All @@ -242,7 +242,7 @@
" QObjective range [1e+00, 5e+00]\n",
" Bounds range [0e+00, 0e+00]\n",
" RHS range [5e-01, 1e+00]\n",
"Presolve removed 22 rows and 1 columns\n",
"Presolve removed 21 rows and 1 columns\n",
"Presolve time: 0.01s\n",
"Presolved: 9 rows, 8 columns, 14 nonzeros\n",
"Presolved model has 3 second-order cone constraints\n",
Expand All @@ -263,7 +263,7 @@
" 4 -8.12263522e-01 -8.12579837e-01 1.20e-14 2.22e-16 2.64e-05 0s\n",
" 5 -8.12499544e-01 -8.12500834e-01 8.52e-13 3.00e-15 1.07e-07 0s\n",
"\n",
"Barrier solved model in 5 iterations and 0.02 seconds (0.00 work units)\n",
"Barrier solved model in 5 iterations and 0.03 seconds (0.00 work units)\n",
"Optimal objective -8.12499544e-01\n",
"\n",
"Standard: w = [0.50000 0.37500 0.12500],\n",
Expand All @@ -277,8 +277,8 @@
"model = gp.Model(\"robustGS\")\n",
"\n",
"# define variables of interest as a continuous\n",
"w = model.addMVar(shape=problem_size, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z = model.addVar(name=\"z\")\n",
"w = model.addMVar(shape=problem_size, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z = model.addVar(lb=0.0, name=\"z\")\n",
"\n",
"# setup the robust objective function\n",
"model.setObjective(\n",
Expand All @@ -287,12 +287,11 @@
"\n",
"# add quadratic uncertainty constraint\n",
"model.addConstr(z**2 <= np.inner(w, omega@w), name=\"uncertainty\")\n",
"model.addConstr(z >= 0, name=\"z positive\")\n",
"# add sub-to-half constraints\n",
"model.addConstr(M @ w == m, name=\"sum-to-half\")\n",
"# add weight-bound constraints~\n",
"model.addConstr(w >= lower_bound, name=\"lower bound\")\n",
"model.addConstr(w <= upper_bound, name=\"upper bound\")\n",
"model.addConstr(w >= lower_bound, name=\"lower-bound\")\n",
"model.addConstr(w <= upper_bound, name=\"upper-bound\")\n",
"\n",
"# solve the problem with Gurobi\n",
"model.optimize()\n",
Expand Down
42 changes: 17 additions & 25 deletions Scratch/risk-minimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,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.02 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 All @@ -314,7 +314,7 @@
"model = gp.Model(\"standardGS\")\n",
"\n",
"# define variable of interest as a continuous \n",
"w = model.addMVar(shape=problem_size, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"w = model.addMVar(shape=problem_size, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"\n",
"# set the objective function\n",
"model.setObjective(\n",
Expand Down Expand Up @@ -401,8 +401,8 @@
"CPU model: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz, instruction set [SSE2|AVX|AVX2]\n",
"Thread count: 4 physical cores, 8 logical processors, using up to 8 threads\n",
"\n",
"Optimize a model with 23 rows, 4 columns and 25 nonzeros\n",
"Model fingerprint: 0xb99edb8a\n",
"Optimize a model with 22 rows, 4 columns and 24 nonzeros\n",
"Model fingerprint: 0x5b10d4aa\n",
"Model has 3 quadratic objective terms\n",
"Model has 3 quadratic constraints\n",
"Coefficient statistics:\n",
Expand All @@ -412,7 +412,7 @@
" QObjective range [1e+00, 5e+00]\n",
" Bounds range [0e+00, 0e+00]\n",
" RHS range [5e-01, 1e+00]\n",
"Presolve removed 22 rows and 1 columns\n",
"Presolve removed 21 rows and 1 columns\n",
"Presolve time: 0.01s\n",
"Presolved: 9 rows, 8 columns, 14 nonzeros\n",
"Presolved model has 3 second-order cone constraints\n",
Expand Down Expand Up @@ -447,8 +447,8 @@
"model = gp.Model(\"robustGS\")\n",
"\n",
"# define variables of interest as a continuous\n",
"w = model.addMVar(shape=problem_size, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z = model.addVar(name=\"z\")\n",
"w = model.addMVar(shape=problem_size, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z = model.addVar(lb=0.0, name=\"z\")\n",
"\n",
"# setup the robust objective function\n",
"model.setObjective(\n",
Expand All @@ -457,7 +457,6 @@
"\n",
"# add quadratic uncertainty constraint\n",
"model.addConstr(z**2 <= np.inner(w, omega@w), name=\"uncertainty\")\n",
"model.addConstr(z >= 0, name=\"z positive\")\n",
"# add sub-to-half constraints\n",
"model.addConstr(M @ w == m, name=\"sum-to-half\")\n",
"# add weight-bound constraints\n",
Expand Down Expand Up @@ -568,7 +567,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -628,28 +627,22 @@
"CPU model: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz, instruction set [SSE2|AVX|AVX2]\n",
"Thread count: 4 physical cores, 8 logical processors, using up to 8 threads\n",
"\n",
"Optimize a model with 5 rows, 51 columns and 101 nonzeros\n",
"Model fingerprint: 0x8c48581e\n",
"Optimize a model with 4 rows, 51 columns and 100 nonzeros\n",
"Model fingerprint: 0x74debc94\n",
"Model has 1275 quadratic objective terms\n",
"Model has 50 quadratic constraints\n",
"Coefficient statistics:\n",
" Matrix range [1e+00, 1e+00]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" Matrix range [1e+00, 1e+00]\n",
" QMatrix range [7e-06, 1e+00]\n",
" Objective range [7e-01, 1e+00]\n",
" QObjective range [5e-02, 1e+00]\n",
" Bounds range [0e+00, 0e+00]\n",
" RHS range [5e-01, 5e-01]\n",
"Presolve removed 3 rows and 0 columns\n",
"Presolve removed 2 rows and 0 columns\n",
"\n",
"Continuous model is non-convex -- solving as a MIP\n",
"\n",
"Presolve removed 3 rows and 0 columns\n",
"Presolve removed 2 rows and 0 columns\n",
"Presolve time: 0.01s\n",
"Presolved: 2552 rows, 1327 columns, 7600 nonzeros\n",
"Presolved model has 1275 quadratic objective terms\n",
Expand All @@ -671,7 +664,7 @@
"Cutting planes:\n",
" RLT: 6\n",
"\n",
"Explored 1 nodes (1828 simplex iterations) in 0.26 seconds (0.10 work units)\n",
"Explored 1 nodes (1828 simplex iterations) in 0.22 seconds (0.10 work units)\n",
"Thread count was 8 (of 8 available processors)\n",
"\n",
"Solution count 1: -0.976358 \n",
Expand Down Expand Up @@ -736,9 +729,9 @@
"model_rbs = gp.Model(\"n50robust\")\n",
"\n",
"# initialise w for both models, z for robust model\n",
"w_std = model_std.addMVar(shape=n, vtype=GRB.CONTINUOUS, name=\"w\") \n",
"w_rbs = model_rbs.addMVar(shape=n, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z_rbs = model_rbs.addVar(name=\"z\")\n",
"w_std = model_std.addMVar(shape=n, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\") \n",
"w_rbs = model_rbs.addMVar(shape=n, lb=0.0, vtype=GRB.CONTINUOUS, name=\"w\")\n",
"z_rbs = model_rbs.addVar(lb=0.0, name=\"z\")\n",
"\n",
"# define the objective functions for both models\n",
"model_std.setObjective(\n",
Expand All @@ -758,7 +751,6 @@
"\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 >= 0, name=\"z positive\")\n",
"\n",
"# since working with non-trivial size, set a time limit\n",
"time_limit = 60*5 # 5 minutes\n",
Expand Down
Loading

0 comments on commit c854716

Please sign in to comment.