please help, how can i fix this multiplication error
回答済みAttached is a code for an optimization problem,
Look at the bolded constraint ( transmission constraint), I'm trying to multiply each generator lets say type 1 for all 24 periods by 0.4 and all type 2 generators by 0.3 for all 24 periods and so on. But the code is ignoring this constraint completely. When I tried to just multiply 0.5 * output [0,0] the code gives me = 0.5 which is wrong
output [0,0] = 20 and thus 0.5 * 20 = 10 but that is not what the code is doing so please help
import numpy as np
import pandas as pd
import gurobipy as gp
from gurobipy import GRB
# tested with Python 3.7.0 & Gurobi 9.0
# Parameters
ntypes = 10
nperiods = 24
maxstart0 = 5
generators = np.ones((10,), dtype=int)
demand = [81,78,79,80,78,85,91,92,93,94,93,92,91,90,87,86,85,88,90,91,88,87,84,82]
min_load = [4 , .3 , .1, 2, 1, 4 , .3 , .1, 2, 1]
max_load = [20.00, 17.50, 4.000, 15 ,10, 20.00, 17.50, 4.000, 15 ,10 ]
base_cost = [1000, 2600, 3000,1000, 2600, 3000,1000, 2600, 3000,1000]
per_mw_cost = [2, 1.3, 3,2, 1.3, 3,2, 1.3, 3,2]
startup_cost = [2000, 1000, 500,2000, 1000, 500,2000, 1000, 500,200]
model = gp.Model('PowerGeneration')
ngen = model.addVars(ntypes, nperiods, vtype=GRB.INTEGER, name="ngen")
nstart = model.addVars(ntypes, nperiods, vtype=GRB.INTEGER, name="nstart")
output = model.addVars(ntypes, nperiods, vtype=GRB.CONTINUOUS, name="genoutput")
# Generator count
numgen = model.addConstrs(ngen[type, period] <= generators[type]
for type in range(ntypes) for period in range(nperiods))
# Respect minimum and maximum output per generator type
min_output = model.addConstrs((output[type, period] >= min_load[type] * ngen[type, period])
for type in range(ntypes) for period in range(nperiods))
max_output = model.addConstrs((output[type, period] <= max_load[type] * ngen[type, period])
for type in range(ntypes) for period in range(nperiods))
----------------------------------------------------------------------------------------------
# Transmission Constraints
Line_Dfax = [0.5,0.4,0.3,0.2,0.1,0,-0.1,-0.2,-0.3,-0.4]
Line_Con = model.addConstrs((Line_Dfax[type] * output[type,period] <= 10 )
for type in range(ntypes) for period in range(nperiods))
-----------------------------------------------------------------------------------------------
# Meet demand
meet_demand = model.addConstrs(gp.quicksum(output[type, period] for type in range(ntypes)) >= demand[period]
for period in range(nperiods))
# Provide sufficient reserve capacity
reserve = model.addConstrs(gp.quicksum(max_load[type]*ngen[type, period] for type in range(ntypes)) >= 1.15*demand[period]
for period in range(nperiods))
# Startup constraint
startup0 = model.addConstrs((ngen[type,0] <= maxstart0 + nstart[type,0])
for type in range(ntypes))
startup = model.addConstrs((ngen[type,period] <= ngen[type,period-1] + nstart[type,period])
for type in range(ntypes) for period in range(1,nperiods))
# Objective: minimize total cost
active = gp.quicksum(base_cost[type]*ngen[type,period]
for type in range(ntypes) for period in range(nperiods))
per_mw = gp.quicksum(per_mw_cost[type]*(output[type,period] - min_load[type]*ngen[type,period])
for type in range(ntypes) for period in range(nperiods))
startup_obj = gp.quicksum(startup_cost[type]*nstart[type,period]
for type in range(ntypes) for period in range(nperiods))
model.setObjective(active + per_mw + startup_obj)
model.write('junk.lp')
model.optimize()
-
Could you please clarify what is the exact issue here?
Looking at the \(\texttt{junk.lp}\) file, I can see all transmission constraints constructed as expected:
R720: 0.5 genoutput[0,0] <= 10
...
R744: 0.4 genoutput[1,0] <= 10
...0 -
The issue is that this constraint is ignored. With transmission constraint and without it, the code gives the same results. If this constraint was applied correctly, then our generator 1 output won't be 20 GW, we would see generators 5- 10 generate more than generators 1- 5. But that is not what is happening here. When I tried to see what the code gives if i multiply 0.5 by output [0,0] the code gives 0.5 removing the output/over writing instead of multiplying, I don't know how to fix this problem.
0 -
If you observe that your transmission constraints have no impact on the solution then you should double check all your other constraints. If you have an idea of what a correct solution point should look like, you could check the X attribute value of each variable and check each constraint by hand whether it behaves as expected.
Currently all of your variables have a default lower bound of \(0\). You could try setting variable lower bounds to \(-\infty\).
ngen = model.addVars(ntypes, nperiods, lb=-GRB.INFINITY, vtype=GRB.INTEGER, name="ngen")
nstart = model.addVars(ntypes, nperiods, lb=-GRB.INFINITY, vtype=GRB.INTEGER, name="nstart")
output = model.addVars(ntypes, nperiods, lb=-GRB.INFINITY, vtype=GRB.CONTINUOUS, name="genoutput")However, I don't know whether your problem is still valid.
0 -
I verified that all constraints are working as expected except the transmission constraint. When I checked by hand for period 0, the sum was 15.45 <= 10 which should not be valid but the code did not report any issues which means that something wrong is happening here.
When you check the results for output[0,0], the code gives 20 which is fine but when you multiply 0.5 * output [0,0], the code gives 0.5 . So the code is overwriting the output with the value of the constant we multiply it with at that constraint and that's why the code is reporting no issues because the sum of these small values will never exceed 10.
My question is that why it is not multiplying the constants with the variable output? The results clearly show that this constraint is being ignored.
Also, what do you mean by not knowing whether my problem is still valid? I tried setting variable lower bounds to −∞ and that affected all other constraints but not the transmission constraint which I'm working on.
0 -
When you check the results for output[0,0], the code gives 20 which is fine but when you multiply 0.5 * output [0,0], the code gives 0.5 . So the code is overwriting the output with the value of the constant we multiply it with at that constraint and that's why the code is reporting no issues because the sum of these small values will never exceed 10.
I am confused. Are you saying that the solution point found by Gurobi is an infeasible one? If yes, could you please point to the exact constraints and variable values at the optimal solution point which violate the particular constraint. I could not observe any of this when I executed your code (with and without negative lower bounds for your variables).
In particular I am confused by the part
but when you multiply 0.5 * output [0,0], the code gives 0.5 . So the code is overwriting the output
When do you multiply output[0,0] by 0.5? Which output is overwritten? Could you please elaborate further on this?
Also, what do you mean by not knowing whether my problem is still valid? I tried setting variable lower bounds to −∞ and that affected all other constraints but not the transmission constraint which I'm working on.
It was just an idea since I don't know your model and its application. Thus, simply using negative lower bound for variables can completely change their meaning. For example, I don't know whether allowing for a negative \(\texttt{nstart}\) in your problem formulation makes sense. This is something you know better.
0 -
Hi Jaromil,
The constraint ( Transmission constraint ) was created to do the following:
- for period 0 : 0.5 * output [0,0] + 0.4 * output [1,0] + 0.3 * output [2,0] + 0.2 * output [3,0] + ... + (-0.4) * output [9,0] <= 10
- for period 1 : 0.5 * output [0,1] + 0.4 * output [1,1] + 0.3 * output [2,1] + 0.2 * output [3,1] + ... + (-0.4) * output [9,1] <= 10
- and so on for all 24 periods from 0 - 23
Based on the output values the code is giving me, this constraint is not doing what I wanted.
When do you multiply output[0,0] by 0.5? Which output is overwritten? Could you please elaborate further on this?
- In jupyter notebook, in a new cell, I tried to just do the following: 0.5 * output [0,0] and the answer was 0.5 which didn't make sense for me because according to my code, output [0,0] = 20 and thus I expected to see 0.5 * output [0,0] = 10 not 0.5!
I hope this explains the issue
0 -
Hi Hussain,
Thank you for clarifying, this helps a lot.
The constraint ( Transmission constraint ) was created to do the following:
- for period 0 : 0.5 * output [0,0] + 0.4 * output [1,0] + 0.3 * output [2,0] + 0.2 * output [3,0] + ... + (-0.4) * output [9,0] <= 10
- for period 1 : 0.5 * output [0,1] + 0.4 * output [1,1] + 0.3 * output [2,1] + 0.2 * output [3,1] + ... + (-0.4) * output [9,1] <= 10
- and so on for all 24 periods from 0 - 23
Based on the output values the code is giving me, this constraint is not doing what I wanted.
This is not what your constraint is doing right now because it is defined in a wrong way. The constraint should be defined as
Line_Con = model.addConstrs(gp.quicksum(Line_Dfax[type] * output[type,period] for type in range(ntypes)) <= 10
for period in range(nperiods))In jupyter notebook, in a new cell, I tried to just do the following: 0.5 * output [0,0] and the answer was 0.5 which didn't make sense for me because according to my code, output [0,0] = 20 and thus I expected to see 0.5 * output [0,0] = 10 not 0.5!
This is because the object output[0,0] itself does now have a numerical value. To access the solution point value, you have to access the respective variable's X attribute
print(0.5 * output[0,0].X)
0 -
I'm really sorry, just by explaining the problem to you I realized what I was doing wrong.
I think this solves it:
Line_Con = model.addConstrs(gp.quicksum(output[type, period]*Line_Dfax[type] for type in range(ntypes)) <= 10 for period in range(nperiods))
0 -
Thank you sir for your help! I appreciate it
0
サインインしてコメントを残してください。
コメント
9件のコメント