Exploreour modeling examples below, access them via GitHub, or download the entire collection. You can also check out examples for other programming languages, or dive into our functional code. MIP developers: Be sure to check out ticdat.
I am trying to implement a piecewise-linear objective in a gurobi optimization problem in python. When trying to get familiar with this method using the example from the gurobi web-site ( -scheduling/)
The mentioned example optimizes the profit from the production of several goods with limited production capacity, especially a limited amount of working hours of the staff. However, the latter constraint is not hard, but, for some bonus payment, the possible work-hours can be expanded (this is the piecewise-linear function). In the general problem formulation, the problem looks like this:
, where r depicts the specific revenues for good i" and x the share of this good, while the cost term provides potential extra cost for overtime.However, in the actual code, the objective misses the cost term, at least according to my understanding:
Are PWLObj always substracted from the main objective? Is there a way to e.g. multiply the PWLObj to the main objective function?I hope that I not just missed some trivial point (I am still pretty new to programming and optimizing alike). Thanks a lot for your time and support!
Similar to this question, I am wondering if anyone has example Julia code that they would be willing to share for how to pass vectors and matrices directly into Gurobi.jl to construct/solve a model. The Gurobi C interface qp example looked a bit limited.
Working toward completeness, here is a script that solves the problem with constraints. To play with setting variable bounds, I removed the implicit [0,+\infty) bounds and added them explicitly later. One remaining question I have is how to access the variables by name with GRBgetvarbyname (see second code block). How should I name the constraints / variables so that I can get their values by name after optimizing?
Ah yes, I think I did mean ["x","y","z"] for the above code, but in general can you add/access vectors of variables? What I am trying to do is provide a name vector with a single element "xyz" that corresponds to the vector of variables [x,y,z]. I tried the following unsuccessfully (though I also cannot get it to work by accessing the variables individually with names ["x","y","z"] either):
I have a binary linear program, where all my variables are binary. So, I have implemented the problem on python with Gurobi solver. I have implemented also a heuristic to find a near-optimal solution.
2- How I can introduce the solution returned by my heuristic to the API of Gurobi solver using python? I Can't find the right instructions to do. If there is a complete example that will be very helpful.
If the solution takes longer it may be the case that your initial solution was infeasible or your heuristic is worse than those implemented in Gurobi (they have some pretty good heuristics).In both cases the solver has to determine whether the solution is feasible or not (which takes some time), and then run mostly as usual. If either your solution is infeasible or their heuristic is better, your solution will be discarded, leading to longer times than no warm-start.
So here is my experience (and also some wisdom of other ppl i read) with setting lower bounds for minimization problems: Depending on the complexity of the model or your strategy for cutting invalid solutions of (tightness of the cuts) it might indeed be worse to set a lower bound, because it messes with the internal pricing gurobi uses. All the nodes that are still to be visited, get truncated to the same obj-value and there is no way to prioritize the more promising nodes, which mostly leads to vising random nodes, which then in turn also branch with the same obj-value and before you can derive any reasonable cuts you end up with a much bigger tree.
Now back to your issue when setting a good starting solution (or an upper bound in general): It is possible that setting such good starting solution (or even an optimal one) cuts off branches of the tree, which would otherwise be examinated and might produce better cuts or direction for moving that upper bound further up or something of this sorts. But in contrary to setting lower bounds, which seems to generally perform worse, this is more of an accident. In general it should be a good idea to set better initial solutions - it might just be worse in your specific case (problem instance)
Consider a consulting company that has three open positions: Tester, Java Developer, and Architect. The three top candidates (resources) for the positions are: Carlos, Joe, and Monika. The consulting company administered competency tests to each candidate in order to assess their ability to perform each of the jobs. The results of these tests are called matching scores. Assume that only one candidate can be assigned to a job, and at most one job can be assigned to a candidate.
The problem is to determine an assignment of resources and jobs such that each job is fulfilled, each resource is assigned to at most one job, and the total matching scores of the assignments is maximized.
To solve this assignment problem, we need to identify which resource is assigned to which job. We introduce a decision variable for each possible assignment of resources to jobs. Therefore, we have 9 decision variables.
The Model.addVars() method creates the decision variables for a Model object.This method returns a Gurobi tupledict object that contains the newly created variables. We supply the combinations object as the first argument to specify the variable indices. The name keyword is used to specify a name for the newly created decision variables. By default, variables are assumed to be non-negative.
All of the job constraints can be defined in a similarly succinct manner. For each job $j \in J$, take the summation of the decision variables over all the resources. We can write the corresponding job constraint as follows.
For example, we want a constraint that requires Carlos to be assigned to at most one of the jobs: either job 1 (Tester), job 2 (Java Developer ), or job 3 (Architect). We can write this constraint as follows.
Again, each of these constraints can be written in a succinct manner. For each resource $r \in R$, take the summation of the decision variables over all the jobs. We can write the corresponding resource constraint as follows.
The Web License Service (WLS) is a new Gurobi licensing service for containers. WLS is compatible with containers running on Docker and Kubernetes, including hosted environments on the Cloud such as Amazon Web Services (ECS and EKS) and Microsoft...
@a_user the newest version of renku has the ability to set custom environment variables before starting a session that will be available in the session. So you could set these every time you start a session and have the license in the session.
This has not been deployed anywhere but upgrading to this version is not hard. Which renku deployment are you using? Is it at University of Fribourg? If yes, I think I may have already spoken to a colleague of yours about this.
These give the WLS license ID, the access ID for that license, and the corresponding secret key. These credentials are private and should not be shared with anyone who is not entitled to use your license.
In a running Renkulab session you should upload your own gurobi.lic file to your home folder (a.k.a. /home/jovyan/work/gurobi-wls-project-setup). As the environment variables have already been specified in post-init.sh, the Gurobi optimizer should be available immediately.
You can test the installation of the license from a terminal by running the command gurobi.sh. Simultaneously, you can also check the tokens issued by Gurobi for the containers associated with your gurobi.lic file.
Going forward, you need to commit the changes (addition of the gurobi.lic file) in your own project (clone/fork etc.) if want the changes to be persistent. Also, in keeping with the Gurobi licensing requirements, this project will have to be private. And finally, students can upload their own license files to their Renku sessions/projects for your class.
@champost Thanks for your input. If I read the docs right setting env variables with these values gurobi should be able to acquire the license automatically without uploading the license file explicitly.
@a_user What I provided was a MWE for using Gurobi on Renku. If your students can make private forks of the my project, download and commit their own gurobi.lic licence file (a one time procedure) at the outset of your course then you can concentrate only on your course content for the rest of the semester !
You could also, as Gurobi is free for academic use, commit your gurobi.lic licence file to a private fork and enable access to your students. They would then use tokens issued under your WLS license for the containers they start. You will need to check the upper limit of container sessions for a single WLS license, which I think is quite large.
The project will be a collaborative one between a small group of students each, hence a shared fork, and the need(?) for separate licenses in a project. Though I will check how many tokens can be used for any given license simultaneously and see if that is an acceptable solution.
A license may have a baseline limit expressed with a number of concurrent sessions. A session is a continuous use of Gurobi software in a container or machine. In this case, you are entitled to run concurrent sessions up to the baseline, and also go over the baseline for a short period of time. This means that you can typically operate rolling updates or blue/green deployments where the number of concurrent sessions may temporarily exceed the baseline with a factor 2.
However, if the recorded usage goes over the baseline for too long or by too much, the token requests may be rejected, and your application will not be able to start new optimization. Note that you cannot release a token, even if you are done using Gurobi. This means that if the container or machine is shut down before the token lifespan has been reached, it will still count until its expiration time (5 minutes by default).
3a8082e126