coin-or / python-mip

Python-MIP: collection of Python tools for the modeling and solution of Mixed-Integer Linear programs
Eclipse Public License 2.0
518 stars 92 forks source link

Representing expressions by creating a new variable #150

Closed MelvinNgCL closed 2 years ago

MelvinNgCL commented 3 years ago

Hi all,

I have come up with a problem recently while trying to minimize cost in a simple power grid system. The system has a load, which is to be powered by either buying power from an external source or from a generator. The system is also given the ability to sell excess power from the generator should there be more than enough power and the system is only allowed to choose to either buy or sell power at any one time. A simple version of the code can be seen below.

The data is interpreted as hourly data across 1 day, which is represented by a 24 x 1 matrix. The objective of the function would be to minize the cost of power exchange, given by (costofbuypowerbought - costofsellingpowersold)

from mip import* import numpy as np

n = 24

BuyC = np.zeros((n,1)) SellC = np.zeros((n,1)) Load = np.zeros((n,1)) Generator = np.zeros((n,1))

for i in range(n): BuyC[i] = 15 SellC[i] = 16 Load[i] = 15

for i in range(7,10): Generator[i] = 16

m = Model(sense=MINIMIZE, solver_name=CBC) ExcC = [m.add_var(name = 'ExcC', var_type=CONTINUOUS) for i in range(n)] Pbuy = [m.add_var(name = 'Pbuy', var_type=CONTINUOUS) for i in range(n)] Psell = [m.add_var(name = 'Psell', var_type=CONTINUOUS) for i in range(n)] gammabuy = [m.add_var(name = 'gammabuy', var_type=BINARY) for i in range(n)] gammasell = [m.add_var(name = 'gammasell',var_type=BINARY) for i in range(n)]

for i in range(n): m += (float(BuyC[i])Pbuy[i]) - (float(SellC[i])Psell[i]) == ExcC[i] m += Pbuy[i] - Psell[i] + float(Generator[i]) == float(Load[i]) m += Pbuy[i] >= 0 m += Pbuy[i] <= gammabuy[i]100 m += Psell[i] >= 0 m += Psell[i] <= gammasell[i]100 m += gammabuy[i] + gammasell[i] <= 1

m.objective = minimize(xsum((float(BuyC[i])Pbuy[i]) - (float(SellC[i])Psell[i]) for i in range(1,n)))

print(m.optimize()) print(m.objective_value)

f = open("trial.txt", "w") for i in m.vars: f.write('{} : {}\n'.format(i.name, i.x))

f.close()

As the generator is set to only turn on for 3 hours that day, the system would be able to sell the power at that 3 hours. The code above would generate an output that depicts the answer that is wanted. However, if i used the commented first line in the for loop (In Italics) and replace the objective function with

m.objective = minimize(xsum(ExcC[i] for i in range(1,n)))

the program would return that the problem is infeasible, when both expressions appear to represent the same problem.

Please let me know if there is any way to explain the problem and if there is any way around it. Thank you.

jurasofish commented 3 years ago

m.add_var() has a default lower bound of 0. Add lb=-INF: ExcC = [m.add_var(name = 'ExcC', lb=-INF, var_type=CONTINUOUS) for i in range(n)].

let us know how you go

MelvinNgCL commented 3 years ago

Hi,

Thank you I believe the problem is rectified