Optimization in Python: Pyomo and Gurobipy Workshop - Brent Austgen - UT Austin INFORMS
Vložit
- čas přidán 11. 02. 2021
- Join UT INFORMS student chapter officer Brent Austgen for a tutorial in implementing math models with pyomo and gurobipy. The tutorial includes an overview of these frameworks, a walk-through of some examples, and Q&A.
The examples presented in this workshop are available at: github.com/brentertainer/pyom...
UT Austin INFORMS Student Chapter:
Twitter: / informs_ut
Website: connect.informs.org/universit...
Thanks Brent for tutoring! Looking forward to the next tutorial!
This is a wonderful tutorial!!! Thank you so much!
Excited!
Great explanation! Thank you very much. Greetings from Technical University Kaiserslautern, Germany :)
Thanks for such nice explanation
Great lesson!
thanks for sharing!
Thanks for sharing. Seems Helen also from my country.
Very helpful video. I wonder if it is possible to define multiple bounds for a variable in PYOMO. Eg: 1
It is possible. I think the most straightforward way is to pass a rule to the `bounds` keyword like below.
>>> import pyomo.environ as pe
>>> model = pe.ConcreteModel()
>>> model.N = pe.RangeSet(2)
>>> model.x_lbs = pe.Param(model.N, initialize={1: 1, 2: 4})
>>> model.x_ubs = pe.Param(model.N, initialize={1: 3, 2: 8})
>>> def generate_x_bounds(model, i):
... return (model.x_lbs[i], model.x_ubs[i])
...
>>> model.x = pe.Var(model.N, bounds=generate_x_bounds)
>>> model.x.display()
x : Size=2, Index=N
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : 1 : None : 3 : False : True : Reals
2 : 4 : None : 8 : False : True : Reals
Extremely helpful tutorial, many thanks for sharing
thank you. this is very helpul tutorial. please can you upload your tutorial about cost optimization from csv file?
I found this tutorial very useful, now i know how to set n-dimensional parameters and decision variables according to the dimension of the dataframe. I wonder, what studies are coursing those students? is it sort of a master in operations research or it's just a course that contains oprimization topics?
Thank you, Robert. I (the presenter) am a PhD student in the Operations Research and Industrial Engineering program at UT-Austin. Most if not all of the attendees were also MS and PhD students in the same program. I was first introduced to Pyomo in 2013 while working on my undergraduate thesis, and I have built a working knowledge of it through various academic and industry projects. I know certain professors that favor this-or-that modeling or optimization tool, but they've never been the focus in any of my coursework. My intent for these tutorials is to bridge the gap between theory (classroom) and practice (research and industry).
hello, i have one problem. I already build my model with the first method from pyomo. I tried to use gurobi as a solver but i take this error "GurobiDirect does not support expressions of degree None". I want to ask if i can use the gurobi only as a solver and if yes then what is the problem?
Without viewing the model implementation, it is difficult to say. My best guess is that the model includes a non-polynomial expression.
Thank you for this wonderful video. But I am not sure about the idea indexing a set with binary variable at 28:28
Could you please explain how it actually works? The word "Binary" is a bit misleading. I would appreciate if you could share some links/documents. Thank you once again!
In our first attempt at setting up the model, we created the variable objects one-by-one via model.x1, ... model.x5. This works, of course, but it is not scalable. Suppose we wanted to setup the same type of problem but with 1000 variables instead of 5. We would not want to write 1000 lines of code to set up model.x1, ..., model.x1000. So instead, we define an index set N = {1, ...., 1000} so that we can define all 1000 variables in a single line of code.
In the line of code model.x = pe.Var(model.N, domain=pe.Binary), the positional argument model.N tells Pyomo to create a variable for each item in the set N. In other words, object model.x becomes indexed in model.N. So whereas before we had 5 different objects model.x1, ... model.x5, we now have one object model.x that is indexed and accessed as model.x[1], ..., model.x[5]. The keyword argument domain=pe.Binary tells Pyomo that each variable is only allowed to assume a value of 0 or 1. If the domain is not specified, Pyomo assumes by default that each variable is defined on the real number line (i.e., pe.Reals).
The Pyomo documentation is reasonably well-written. I recommend you check out these links:
1. the Var class: pyomo.readthedocs.io/en/stable/library_reference/aml/index.html?highlight=pe.Var#pyomo.environ.Var
2. predefined virtual sets: pyomo.readthedocs.io/en/stable/pyomo_modeling_components/Sets.html#predefined-virtual-sets
For the binary knapsack problem, what if you have two objectives?
Sorry for getting back to you so slowly. Would you please expand on how you mean this? Are you talking about vector-valued objectives? Or just multiple scalar-valued objectives that you can switch in and out as needed?
Hello, Great example and video. Thank you. Could you also show how to import variable and parameter sets from an excel file to pyomo?
Sure, great idea!
I added an example to the repository linked in the description. It is in the directory labeled "loading_data".
Thank you Brent. I will be looking forward to new videos👍🏻
is there a way to install the glpk solver in python
The glpk solver (sometimes called glpsol) is a piece of software that is installed separately from Python. There is also a glpk package for Python that serves as an alternative to pyomo. If you want to use glpk with pyomo, you first need to install the glpk package (for Windows/Linux/macOS), then just change SolverFactory('gurobi') to SolverFactory('glpk') in the Python code.
I recently learned a very simple way to setup GLPK for Python/Pyomo. If you use Anaconda to manage your Python environments, you can simply `conda install glpk` to install the glpsol binary to the active environment. As long as you are in that environment, you can invoke glpsol (e.g., from pyomo).
Is it possible to solve multiple LPP's within pyomo?
I take LPP to mean linear programming problem, but correct me if you mean otherwise. Do you mean solving multiple problems in parallel?
Yes a linear programming problem where the parameters for a certain constraint change
Nice and helpful video. However two commen errors in github codes. (1) graph.nodes or, graph.edges are not iterable. (2) module 'matplotlib.cbook' has no attribute 'is_string_like'. I am wondering if you could update the github codes. Thank you in advance.
Thanks for the notice. I just checked and all the notebooks run without these issues in my environment. Would you mind identifying which cells in which notebooks are failing? And also which version of networkx and matplotlib you are using? You are welcome to open an issue on GitHub.
@@BrentAustgen Thank you so much for your reply. I am not sure but it could be a version mismatch. I am using version '1.11' and '3.1.1' of 'networkx' and 'matplotlib' respectively. Would you like to share the versions that you used to run? Thank you in advance.
@@raihanmasud3578 Same for matplotlib, but I am using version 2.4 of networkx. I will check if that is the issue. If it is, I will reimplement to accommodate both versions.
@@raihanmasud3578 And come to think of it, this is a great consideration. I should include the list of what all libraries/versions are in my environment in the repository.
@@BrentAustgen Yes, It would be a nice idea if you could include all versions including python version also. Thank you.
model.n = pe.Var(domain=pe.Binary)
model.m = pe.Var(domain=pe.Binary)
model.k = pe.Var(domain=pe.Binary)
********* gives this error; what shall i do
WARNING: Implicitly replacing the Component attribute n (type=) on block unknown with a new Component
(type=). This is usually
indicative of a modelling error. To avoid this warning, use
block.del_component() and block.add_component().
WARNING: Implicitly replacing the Component attribute m (type=) on block unknown with a new Component
(type=). This is usually
indicative of a modelling error. To avoid this warning, use
block.del_component() and block.add_component().
WARNING: Implicitly replacing the Component attribute k (type=) on block unknown with a new Component
(type=). This is usually
indicative of a modelling error. To avoid this warning, use
block.del_component() and block.add_component().
It seems like model.n was already defined before you tried making it a Var.
Is it possible to get ur email @Brent, I have an DEA optimization model I'd like to implement in pyomo. But I'm struggling a bit.
My full name is the description of this video. If you search for that online, my website should be one of the top hits, and you can find my contact information there.
I managed to solve my problem sir. Thank you.