mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
Add _gurobipy suffix to all build_model functions
This commit is contained in:
@@ -183,7 +183,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 2,
|
||||
"id": "ac6f8c6f",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
|
||||
@@ -101,7 +101,7 @@
|
||||
"from miplearn.io import write_pkl_gz\n",
|
||||
"from miplearn.problems.multiknapsack import (\n",
|
||||
" MultiKnapsackGenerator,\n",
|
||||
" build_multiknapsack_model,\n",
|
||||
" build_multiknapsack_model_gurobipy,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Set random seed to make example reproducible\n",
|
||||
@@ -127,7 +127,7 @@
|
||||
"# Run the basic collector\n",
|
||||
"BasicCollector().collect(\n",
|
||||
" glob(\"data/multiknapsack/*\"),\n",
|
||||
" build_multiknapsack_model,\n",
|
||||
" build_multiknapsack_model_gurobipy,\n",
|
||||
" n_jobs=4,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
|
||||
@@ -39,7 +39,6 @@
|
||||
"cell_type": "markdown",
|
||||
"id": "830f3784-a3fc-4e2f-a484-e7808841ffe8",
|
||||
"metadata": {
|
||||
"jp-MarkdownHeadingCollapsed": true,
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
@@ -159,20 +158,22 @@
|
||||
"H 0 0 2.0000000 1.27484 36.3% - 0s\n",
|
||||
" 0 0 1.27484 0 4 2.00000 1.27484 36.3% - 0s\n",
|
||||
"\n",
|
||||
"Explored 1 nodes (38 simplex iterations) in 0.01 seconds (0.00 work units)\n",
|
||||
"Explored 1 nodes (38 simplex iterations) in 0.03 seconds (0.00 work units)\n",
|
||||
"Thread count was 20 (of 20 available processors)\n",
|
||||
"\n",
|
||||
"Solution count 3: 2 4 5 \n",
|
||||
"\n",
|
||||
"Optimal solution found (tolerance 1.00e-04)\n",
|
||||
"Best objective 2.000000000000e+00, best bound 2.000000000000e+00, gap 0.0000%\n"
|
||||
"Best objective 2.000000000000e+00, best bound 2.000000000000e+00, gap 0.0000%\n",
|
||||
"\n",
|
||||
"User-callback calls 143, time in user-callback 0.00 sec\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"from scipy.stats import uniform, randint\n",
|
||||
"from miplearn.problems.binpack import BinPackGenerator, build_binpack_model\n",
|
||||
"from miplearn.problems.binpack import BinPackGenerator, build_binpack_model_gurobipy\n",
|
||||
"\n",
|
||||
"# Set random seed, to make example reproducible\n",
|
||||
"np.random.seed(42)\n",
|
||||
@@ -193,7 +194,7 @@
|
||||
"print()\n",
|
||||
"\n",
|
||||
"# Optimize first instance\n",
|
||||
"model = build_binpack_model(data[0])\n",
|
||||
"model = build_binpack_model_gurobipy(data[0])\n",
|
||||
"model.optimize()"
|
||||
]
|
||||
},
|
||||
@@ -360,7 +361,9 @@
|
||||
"No other solutions better than -1279\n",
|
||||
"\n",
|
||||
"Optimal solution found (tolerance 1.00e-04)\n",
|
||||
"Best objective -1.279000000000e+03, best bound -1.279000000000e+03, gap 0.0000%\n"
|
||||
"Best objective -1.279000000000e+03, best bound -1.279000000000e+03, gap 0.0000%\n",
|
||||
"\n",
|
||||
"User-callback calls 490, time in user-callback 0.00 sec\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -369,7 +372,7 @@
|
||||
"from scipy.stats import uniform, randint\n",
|
||||
"from miplearn.problems.multiknapsack import (\n",
|
||||
" MultiKnapsackGenerator,\n",
|
||||
" build_multiknapsack_model,\n",
|
||||
" build_multiknapsack_model_gurobipy,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Set random seed, to make example reproducible\n",
|
||||
@@ -396,7 +399,7 @@
|
||||
"print()\n",
|
||||
"\n",
|
||||
"# Build model and optimize\n",
|
||||
"model = build_multiknapsack_model(data[0])\n",
|
||||
"model = build_multiknapsack_model_gurobipy(data[0])\n",
|
||||
"model.optimize()"
|
||||
]
|
||||
},
|
||||
@@ -535,20 +538,22 @@
|
||||
" 0 0 86.06884 0 15 93.92000 86.06884 8.36% - 0s\n",
|
||||
"* 0 0 0 91.2300000 91.23000 0.00% - 0s\n",
|
||||
"\n",
|
||||
"Explored 1 nodes (70 simplex iterations) in 0.07 seconds (0.00 work units)\n",
|
||||
"Explored 1 nodes (70 simplex iterations) in 0.08 seconds (0.00 work units)\n",
|
||||
"Thread count was 20 (of 20 available processors)\n",
|
||||
"\n",
|
||||
"Solution count 10: 91.23 93.92 93.98 ... 368.79\n",
|
||||
"\n",
|
||||
"Optimal solution found (tolerance 1.00e-04)\n",
|
||||
"Best objective 9.123000000000e+01, best bound 9.123000000000e+01, gap 0.0000%\n"
|
||||
"Best objective 9.123000000000e+01, best bound 9.123000000000e+01, gap 0.0000%\n",
|
||||
"\n",
|
||||
"User-callback calls 190, time in user-callback 0.00 sec\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"from scipy.stats import uniform, randint\n",
|
||||
"from miplearn.problems.pmedian import PMedianGenerator, build_pmedian_model\n",
|
||||
"from miplearn.problems.pmedian import PMedianGenerator, build_pmedian_model_gurobipy\n",
|
||||
"\n",
|
||||
"# Set random seed, to make example reproducible\n",
|
||||
"np.random.seed(42)\n",
|
||||
@@ -576,7 +581,7 @@
|
||||
"print()\n",
|
||||
"\n",
|
||||
"# Build and optimize model\n",
|
||||
"model = build_pmedian_model(data[0])\n",
|
||||
"model = build_pmedian_model_gurobipy(data[0])\n",
|
||||
"model.optimize()"
|
||||
]
|
||||
},
|
||||
@@ -694,7 +699,9 @@
|
||||
"Solution count 1: 213.49 \n",
|
||||
"\n",
|
||||
"Optimal solution found (tolerance 1.00e-04)\n",
|
||||
"Best objective 2.134900000000e+02, best bound 2.134900000000e+02, gap 0.0000%\n"
|
||||
"Best objective 2.134900000000e+02, best bound 2.134900000000e+02, gap 0.0000%\n",
|
||||
"\n",
|
||||
"User-callback calls 178, time in user-callback 0.00 sec\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -834,14 +841,16 @@
|
||||
"No other solutions better than -1986.37\n",
|
||||
"\n",
|
||||
"Optimal solution found (tolerance 1.00e-04)\n",
|
||||
"Best objective -1.986370000000e+03, best bound -1.986370000000e+03, gap 0.0000%\n"
|
||||
"Best objective -1.986370000000e+03, best bound -1.986370000000e+03, gap 0.0000%\n",
|
||||
"\n",
|
||||
"User-callback calls 238, time in user-callback 0.00 sec\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"from scipy.stats import uniform, randint\n",
|
||||
"from miplearn.problems.setpack import SetPackGenerator, build_setpack_model\n",
|
||||
"from miplearn.problems.setpack import SetPackGenerator, build_setpack_model_gurobipy\n",
|
||||
"\n",
|
||||
"# Set random seed, to make example reproducible\n",
|
||||
"np.random.seed(42)\n",
|
||||
@@ -865,7 +874,7 @@
|
||||
"print()\n",
|
||||
"\n",
|
||||
"# Build and optimize model\n",
|
||||
"model = build_setpack_model(data[0])\n",
|
||||
"model = build_setpack_model_gurobipy(data[0])\n",
|
||||
"model.optimize()"
|
||||
]
|
||||
},
|
||||
@@ -1374,13 +1383,15 @@
|
||||
" RLT: 1\n",
|
||||
" Relax-and-lift: 7\n",
|
||||
"\n",
|
||||
"Explored 1 nodes (234 simplex iterations) in 0.03 seconds (0.02 work units)\n",
|
||||
"Explored 1 nodes (234 simplex iterations) in 0.02 seconds (0.02 work units)\n",
|
||||
"Thread count was 20 (of 20 available processors)\n",
|
||||
"\n",
|
||||
"Solution count 5: 364722 368600 374044 ... 440662\n",
|
||||
"\n",
|
||||
"Optimal solution found (tolerance 1.00e-04)\n",
|
||||
"Best objective 3.647217661000e+05, best bound 3.647217661000e+05, gap 0.0000%\n"
|
||||
"Best objective 3.647217661000e+05, best bound 3.647217661000e+05, gap 0.0000%\n",
|
||||
"\n",
|
||||
"User-callback calls 677, time in user-callback 0.00 sec\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -1388,7 +1399,7 @@
|
||||
"import random\n",
|
||||
"import numpy as np\n",
|
||||
"from scipy.stats import uniform, randint\n",
|
||||
"from miplearn.problems.uc import UnitCommitmentGenerator, build_uc_model\n",
|
||||
"from miplearn.problems.uc import UnitCommitmentGenerator, build_uc_model_gurobipy\n",
|
||||
"\n",
|
||||
"# Set random seed to make example reproducible\n",
|
||||
"random.seed(42)\n",
|
||||
@@ -1424,7 +1435,7 @@
|
||||
" print()\n",
|
||||
"\n",
|
||||
"# Load and optimize the first instance\n",
|
||||
"model = build_uc_model(data[0])\n",
|
||||
"model = build_uc_model_gurobipy(data[0])\n",
|
||||
"model.optimize()"
|
||||
]
|
||||
},
|
||||
@@ -1532,7 +1543,9 @@
|
||||
"Solution count 1: 301 \n",
|
||||
"\n",
|
||||
"Optimal solution found (tolerance 1.00e-04)\n",
|
||||
"Best objective 3.010000000000e+02, best bound 3.010000000000e+02, gap 0.0000%\n"
|
||||
"Best objective 3.010000000000e+02, best bound 3.010000000000e+02, gap 0.0000%\n",
|
||||
"\n",
|
||||
"User-callback calls 326, time in user-callback 0.00 sec\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -1542,7 +1555,7 @@
|
||||
"from scipy.stats import uniform, randint\n",
|
||||
"from miplearn.problems.vertexcover import (\n",
|
||||
" MinWeightVertexCoverGenerator,\n",
|
||||
" build_vertexcover_model,\n",
|
||||
" build_vertexcover_model_gurobipy,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Set random seed to make example reproducible\n",
|
||||
@@ -1565,26 +1578,9 @@
|
||||
"print()\n",
|
||||
"\n",
|
||||
"# Load and optimize the first instance\n",
|
||||
"model = build_vertexcover_model(data[0])\n",
|
||||
"model = build_vertexcover_model_gurobipy(data[0])\n",
|
||||
"model.optimize()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9f12e91f",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-11-07T16:29:49.075852252Z",
|
||||
"start_time": "2023-11-07T16:29:49.050243601Z"
|
||||
},
|
||||
"collapsed": false,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
@@ -92,6 +92,8 @@
|
||||
"\n",
|
||||
"Solved in 15 iterations and 0.00 seconds (0.00 work units)\n",
|
||||
"Optimal objective 2.761000000e+03\n",
|
||||
"\n",
|
||||
"User-callback calls 56, time in user-callback 0.00 sec\n",
|
||||
"Set parameter PreCrush to value 1\n",
|
||||
"Set parameter LazyConstraints to value 1\n",
|
||||
"Gurobi Optimizer version 10.0.3 build v10.0.3rc0 (linux64)\n",
|
||||
|
||||
@@ -87,8 +87,10 @@
|
||||
"\n",
|
||||
"# Set up Python logging\n",
|
||||
"import logging\n",
|
||||
"\n",
|
||||
"logging.basicConfig(level=logging.WARNING)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def build_tsp_model_gurobipy_simplified(data):\n",
|
||||
" # Read data from file if a filename is provided\n",
|
||||
" if isinstance(data, str):\n",
|
||||
@@ -99,9 +101,7 @@
|
||||
"\n",
|
||||
" # Create set of edges between every pair of cities, for convenience\n",
|
||||
" edges = tuplelist(\n",
|
||||
" (i, j)\n",
|
||||
" for i in range(data.n_cities)\n",
|
||||
" for j in range(i + 1, data.n_cities)\n",
|
||||
" (i, j) for i in range(data.n_cities) for j in range(i + 1, data.n_cities)\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Add binary variable x[e] for each edge e\n",
|
||||
@@ -113,11 +113,8 @@
|
||||
" # Add constraint: must choose two edges adjacent to each city\n",
|
||||
" model.addConstrs(\n",
|
||||
" (\n",
|
||||
" quicksum(\n",
|
||||
" x[min(i, j), max(i, j)]\n",
|
||||
" for j in range(data.n_cities)\n",
|
||||
" if i != j\n",
|
||||
" ) == 2\n",
|
||||
" quicksum(x[min(i, j), max(i, j)] for j in range(data.n_cities) if i != j)\n",
|
||||
" == 2\n",
|
||||
" for i in range(data.n_cities)\n",
|
||||
" ),\n",
|
||||
" name=\"eq_degree\",\n",
|
||||
@@ -129,7 +126,7 @@
|
||||
" \"\"\"\n",
|
||||
" # Query current value of the x variables\n",
|
||||
" x_val = m.inner.cbGetSolution(x)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" # Initialize empty set of violations\n",
|
||||
" violations = []\n",
|
||||
"\n",
|
||||
@@ -164,9 +161,7 @@
|
||||
" \"\"\"\n",
|
||||
" print(f\"Enforcing {len(violations)} subtour elimination constraints\")\n",
|
||||
" for violation in violations:\n",
|
||||
" m.add_constr(\n",
|
||||
" quicksum(x[e[0], e[1]] for e in violation) >= 2\n",
|
||||
" )\n",
|
||||
" m.add_constr(quicksum(x[e[0], e[1]] for e in violation) >= 2)\n",
|
||||
"\n",
|
||||
" return GurobiModel(\n",
|
||||
" model,\n",
|
||||
@@ -494,7 +489,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"solver = LearningSolver(components=[]) # empty set of ML components\n",
|
||||
"solver = LearningSolver(components=[]) # empty set of ML components\n",
|
||||
"solver.optimize(test_data[0], build_tsp_model_gurobipy_simplified);"
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user