# Tag Info

## New answers tagged python

3

Introduce linear constraints: $$\sum_{\text{h}} z[\text{h}][\text{driver}] \le 1 \quad\text{for each driver}$$

4

If you look at the output log, you'll see a primal infeasibility value of 2.0 (which is the sum of the absolute values of the violations in the two values you noted). That just means it will take more than one iteration to find an initial feasible solution. In other words, your iteration limit is too small.

4

One useful package that I've been using for optimization modeling is ticdat, which is great for validating the input data and performing sanity and integrity checks. Because the data we receive in applications are almost never clean and a lot needs to be written to ensure they are correctly handled by either throwing an error or fixing what needs to be fixed ...

4

I'll start with the lowest hanging fruit in the orchard: an IDE (with a good built-in debugger), to facilitate code development. Regarding reading/writing "data", I use XML libraries to save and retrieve things like the parameter settings used in runs. For applications where either the input date is complicated or I'm running multiple instances ...

4

In addition to profiler (mentioned by Kuifje), Python has quite a few unit testing libraries that can be used to test your application (unittest and pytest are both common). As in any commercial application, real-world OR applications should be flexible, maintainable and modularized. It is common to change some modelling objects (variables, constraints, etc),...

9

I like Stuart Mitchell's (maintainer of Pulp) tips, especially tip number 2 : use a profiler to track your bottlenecks. Quoting him: I can't tell you the number of times I have assumed the slow code was for one reason and then found it was another. I agree with him and use line_profiler to optimize the code (for Python). I have been able to drastically ...

2

Your question isn't entirely clear and isn't really an OR question, but I think what you are trying to do is the following: for j in n: for k in r: o += xsum(w[k] *y[k][jj] for jj in n if jj <= j) >= xsum (a[i][k]* x[i][j] for i in p)

1

I have added a more general question and answer that address the modeling aspect, independent of solver: How to linearize membership in a finite set You can apply this with $S=\{0,3,5\}$, $S=\{0,1,3,5\}$, and so on.

2

You can do something like this: possible_values = [10, 15] # add binary variables b = m.addVars(len(possible_values), vtype="B") # b[0] + b[1] == 1 m.addConstr(b.sum() == 1) # add indicator constraints: for i, val in enumerate(possible_values): # if b[i] == 1, then x.prod(f) == val m.addConstr((b[i] == 1) >> (x.prod(f) == val)) ...

1

The problem is that you create a tuplelist of a list. The function addVars returns a list of variables objects already. This means, you should be able to simply do: contribution = LinExpr(coefficients, bns)

1

Here is a small variation of RobPratt's answer. I will use two sets, as an example. Two sets of constants are given: $S_1 = \lbrace x_{1,1}, x_{1,2}, x_{1,3} \rbrace$ and $S_2 = \lbrace x_{2,1}, x_{2,2} \rbrace$. The goal is to choose 1 item from each set and ensure the sum of all items chosen is 100. Make one binary variables per item: $b_{1,1}, b_{1,2}, b_{... 3 Given values$v_i$with index set$I$(for example,$I=\{1,2,3,4,5\}$with$v=[10,20,50,60,30]$), you can enforce$x=v_i$for some$i\in I$by introducing binary variables$y_iand imposing linear constraints \begin{align} \sum_{i\in I} y_i &= 1 \tag1 \\ \sum_{i\in I} v_i y_i &= x \tag2 \end{align} Constraint(1)$chooses exactly one$i$with$y_i=...

1

For timing within the gurobi solver, maybe you can call <optimizer>.solve() method with report_timing=True, as described in pyomo's documentation at https://pyomo.readthedocs.io/en/stable/library_reference/solvers/gurobi_persistent.html I haven't used Gurobi before. If gurboipy logs the starting and ending time of the optimization, then you can ...

2

In your specific instance you use only a single index for a two-dimensional variable matrix, causing the KeyError. You should loop over the index sets from your model to produce the results, so for example: outputVariables_list = [model.variable_heatGenerationCoefficient_SpaceHeating[building, time_slot], model.variable_temperatureBufferStorage[building, ...

Top 50 recent answers are included