Hot answers tagged

5

Introduce a supersource node $s$, a supersink node $t$, arcs from $s$ to each source, and arcs from each sink to $t$. Arc $(s,i)$ has zero cost and capacity equal to supply[i]. Arc $(i,t)$ has zero cost and capacity equal to -supply[i]. All original nodes have supply zero, $s$ has supply equal to the sum of positive supplies, and $t$ has supply equal to ...


5

You are just importing the pyomo.environ module while the tutorials probably use the from syntax. These variables are inside pyomo.environ so you have 3 alternatives: Import them explicitly from pyomo.environ import Var, NonNegativeReals Import them using a wildcard from pyomo.environ import * (this is considered an antipattern) Import the module (with an ...


5

You could apply the following trick inside your objective function: x1 = min(x[1],x[2]) x2 = max(x[1],x[2]) Now x1 <= x2 automatically holds and you don't need the constraint. (Assuming you can live with <= instead of <).


4

From the syntax used in your specific example, s_init is most likely used as a callback. Here's a simple Python example of how this works: >>> def get_square(val): ... """The callback.""" ... return val ** 2 ... >>> def caller(func, val): ... return func(val) ... >>> caller(get_square, 5) ...


4

Your code is correct, you have used pulp.LpVariable.dicts correctly. For better readability, it is often better to use dictionaries. So the idea is to convert your data from lists to dicts as follows: # convert data into dicts dict_bike_profit = dict(zip(bike_types, bike_profit)) # simple dict dict_bike_stock = dict(zip(part_names, parts_stock)) # simple ...


3

try: model.set_timeslots = pyo.RangeSet(0,95) model.variable_x = pyo.Var(model.set_timeslots, within=pyo.Binary) i0 = 20 def temperatureConstraintRule(model, t): if t == model.set_timeslots.first(): return model.variable_x[t] == i0 + model.variable_x[t] return model.variable_x[t] == model.variable_x[t-1] + model.variable_x[t] model....


3

You can use tee = True as a parameter for .solve in Pyomo. Moreover, to access the optimality gap you can use the following code in Pyomo: msolver = SolverFactory('glpk') solution = msolver.solve(m, tee=True) data = solution.Problem._list Then you have a list of detailed information about the problem's solution. For instance LB = data[0].lower_bound UB = ...


2

The mentioned link is about using gurobipy (gurobi's interface for python) to model your problem and solve it using gurobi. If you want to model the problem using Pyomo and use gurobi as a solver, you need to add the gurobi solver to the system's path. For that do the followings: In Windows search for Edit the system environment variables (I am not familiar ...


2

Assuming you are writing the norms as explicit mathematical expressions (e.g. $|x_1|+|x_2|...$) you can just use our own Octeract Engine. As of next Monday (15 Feb 2021) it's free for everyone, and it will most likely destroy any problem that size, especially compared to any open source solver I could recommend as an alternative. It has a Python API, and ...


2

Let $x_2^1$ be the variable in the first model, and $x_2^2$ be the corresponding variable in the second model. Also, let $\hat{x}_2^1$ be the optimal value of $x_2^1$, after solving the first model. So if you want to add the optimal value of $x_2^1$ in the second model, use the constraint $$ x_2^2 = \hat{x}_2^1 $$


2

One way to model this is to add a dummy arc from the sink to the source and impose flow balance of 0 at every node, including the source and sink. But if you prefer the conditional constraint, I think the proper syntax is: m.addConstrs( (flow.sum('*',j) - flow.sum(j,'*') == 0 for j in vertices if j != 2 and j != 7), "node")


2

CPLEX treats the seed as a parameter. The parameter name varies by API; for Python it seems to be "parameters.randomseed". The docs somewhat unhelpfully state that "[t]he default value of this parameter changes with each release" (but do not specify what it is in the current release). Note that a change to the seed is not guaranteed to ...


2

If you add pyo. to the beginning of your solver definition line, it works and gives the solution as follow: param_capacityOfPlants : Size=2, Index=set_plants, Domain=Any, Default=None, Mutable=False Key : Value San_Diego : 600 Seattle : 350 param_demandAtMarkets : Size=3, Index=set_markets, Domain=Any, Default=None, Mutable=False ...


2

Replace the Def and last line of your code with the following lines: model.constraint_supply = pyo.ConstraintList() for i in model.set_plants: model.constraint_supply.add(sum(model.variable_x[i, j] for j in model.set_markets) <= model.param_capacityOfPlants[i]) model.constraint_supply.pprint() The output will be as follow: constraint_supply : ...


2

You can create a binary decision variable as: from docplex.mp.model import Model m = Model(...) my_var = m.binary_var("name_of_this_var") The variable is just an object and does not know how many indices it has. You can then maintain your own variable dictionary. So if you have a variable defined for the indices i,k,s,m,t, you could create a ...


2

I'd go with Python because Python is a general purpose programming language and is much more widely used in industry than MATLAB. In the industry you rarely find yourself just code optimization algorithms or models in your daily work. It is more like you write an entire pipeline from data acquisition, data cleaning, model development (both statistical ...


Only top voted, non community-wiki answers of a minimum length are eligible