Created
May 25, 2018 16:09
-
-
Save pierre-haessig/a00f97c0c9d46e24c32afba4625700ee to your computer and use it in GitHub Desktop.
Experiment with the ability to modify a constraint of an existing optimization problem modeled with JuMP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Modification of optimization constraints\n", | |
"\n", | |
"Experiment with the ability to modify a constraint of an existing optimization problem modeled with JuMP.\n", | |
"\n", | |
"discussion on forum: https://discourse.julialang.org/t/modify-equality-constraints-in-jump/\n", | |
"\n", | |
"JuMP 0.18 doc: http://www.juliaopt.org/JuMP.jl/0.18/probmod.html#modifying-constraints\n", | |
"\n", | |
"The doc says only the RHS of *inequalities* can be change, but @odow on the forum suggests that it may work also for equalities. Here is a test.\n", | |
"\n", | |
"→ seems to work indeed!\n", | |
"\n", | |
"PH, May 2018" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"v\"0.6.2\"" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"VERSION" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"using JuMP\n", | |
"using Clp" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"v\"0.18.0\"" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Pkg.installed(\"JuMP\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Create model, with 2 variables in the [0,1]² box" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
":Optimal" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"m = Model(solver = ClpSolver())\n", | |
"\n", | |
"# two variables\n", | |
"@variable(m, 0 <= x <= 1)\n", | |
"@variable(m, 0 <= y <= 1)\n", | |
"\n", | |
"# equality constraint\n", | |
"@constraint(m, mycon, x + y == 1.5)\n", | |
"\n", | |
"@objective(m, Max, y)\n", | |
"\n", | |
"solve(m)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(0.5, 1.0)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"getvalue(x), getvalue(y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$$ x + y = 1.5 $$" | |
], | |
"text/plain": [ | |
"x + y = 1.5" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mycon" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Modify the equality constraint" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/latex": [ | |
"$$ x + y = 0.5 $$" | |
], | |
"text/plain": [ | |
"x + y = 0.5" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"JuMP.setRHS(mycon, 0.5)\n", | |
"mycon" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\u001b[1m\u001b[33mWARNING: \u001b[39m\u001b[22m\u001b[33mSolver does not appear to support providing initial feasible solutions.\u001b[39m\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
":Optimal" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"solve(m)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(0.0, 0.5)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"getvalue(x), getvalue(y)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"TODO: performance analysis, comparing the two options:\n", | |
"\n", | |
"* generate the model from scratch, and then solve\n", | |
"* modify the preexisting model, and then solve\n", | |
"\n", | |
"(probably the result would depend on the solver's abilities for warm starts)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 0.6.2", | |
"language": "julia", | |
"name": "julia-0.6" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "0.6.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment