# convert x=min(x, N) function into a constraint, integer programming python

In this case, the shop want to maximise the profit that is there $$n$$ products the shop can procure, but the shop only has $$w$$ budget. The cost of each product $$x_i$$ is $$c_i$$.

I used prediction model and predict each product $$x_i$$ will have total $$N_i$$ sales in the future 10 days. The selling price of each product $$x_i$$ is $$s_i$$.

So, I give the objective function $$f$$:

$$max f=\sum_{i}^{n}(s_i - c_i)x_i \\ s.t.\\ x_i \in Z \\ \sum_{i}^{n} c_ix_i \leq w \\ x_i \leq N_i\\ i = 1,2, ..., n$$

where $$n$$, $$w$$, $$s_i$$, $$c_i$$ and $$N_i$$ are all constants.

I want to solve this integer programming by python. But what I find online is more about linear programming. Is there any friendly Python package solver?

• Hi @mmmm, are you trying to model that $x_i$ cannot exceed $N_i$? In that case you should impose $x_i\leq N_i$ directly
– Sune
Nov 7, 2022 at 21:17
• Your Maxf is the function that you are going to maximize. You'd need a solver either scipy/optimize or commercial ones like Gurobi/CPLEX\ I am not sure about the 2nd constraint. Assuming $x_i$ is your decision variable then is it like either $x_i$ retains its value or takes $N_i$ if $N_i$<$x_i$? Nov 7, 2022 at 22:25
• As stated by @prubin you need to use a solver like gurobipy. But one thing, your sales forecast says $N_i$. Do you really want to produce $x_i$ fewer than $N_i$? While there are more complicated cost like waste or lost sales but I guess it should be $x_i>=N_i$ while constraining the budget. Otherwise depending upon revenue & cost solver may take most of x as 0.\ Gurobi has python API or you can try scipy /optimize as problem is linear. Nov 7, 2022 at 23:44
• @Sutanu You cannot sell more than the demand, so $x_i \le N_i$ is correct. Nov 8, 2022 at 0:47