メインコンテンツへスキップ

Ignored upper bound set on variable

回答済み

コメント

6件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Jaromił Najman
    • Gurobi Staff

    Hi Nicolas,

    Could you post a minimal working example to make the issue reproducible as described in Posting to the Community Forum?

    Best regards,
    Jaromił

    1
  • Nicolas Campbell
    • Gurobi-versary
    • First Comment
    • First Question

    Hi,

    Sorry it has taken a while to respond. I was away and it would take me time to make a minimal working example. I have made one now and I hope you can solve my issues.

    from gurobipy import *
    import numpy as np
    import pandas as pd
    import numpy as np
    zones = ['R1','SO1','MO1','LO3','H4']
    times = [str(i) for i in range(1,1440)]
    epcompile = pd.read_excel('miniwork.xlsx', index_col=0)
    cum_epcompile= epcompile.cumsum()

    ub = {"R1":20.75, "SO1":epcompile["SO1"].max(),"MO1":27.933,
    "LO3":epcompile["LO3"].max(),"H4":epcompile["H4"].max()}
    lb = {"R1":0.8, "SO1":0.36,"MO1":1.0,"LO3":9.7,"H4":13.59}

    model = Model('flex')
    #Variables
    x = model.addVars(times,zones, name = "x", ub=ub, lb = lb, vtype= 'S')
    e = model.addVars(times,zones, name = "e", vtype = 'C')
    cumm = model.addVars(times,zones, name = "cumm",vtype = 'C')
    peak = model.addVar(name = "peak", vtype = 'C')

    #Constraints
    model.addConstrs(cumm[times[i], zone] == cum_epcompile.loc[cum_epcompile.index[i], zone] for zone in zones for i in range(1439))
    model.addConstrs(e[times[0], zone] == cum_epcompile.loc[cum_epcompile.index[0], zone] for zone in zones)
    model.addConstrs(e[time,zone] == x[times[time_index-1], zone] + e[times[time_index-1], zone] for zone in zones for time_index, time in enumerate(times)
    if time != times[0])
    model.addConstrs(e[time,zone] >= (0.98)*cumm[time,zone] for time in times for zone in zones
    if time != times[0])
    model.addConstrs(e[time,zone] <= 200+(1.02)*cumm[time,zone] for time in times for zone in zones
    if time != times[0])
    model.addConstrs(x[time,zone] >= x[times[time_index-1],zone]-0.2*ub[zone] for zone in zones for time_index, time in enumerate(times)
    if time != times[0])
    model.addConstrs(x[time,zone] <= x[times[time_index-1],zone]+0.2*ub[zone] for zone in zones for time_index, time in enumerate(times)
    if time != times[0])
    model.addConstrs(peak >= sum(x[time, zone] for zone in zones) for time in times)
    model.addConstrs(e[times[1438],zone] <=1.0001*cumm[times[1438],zone] for zone in zones)

    #Objective
    model.setObjective(peak, GRB.MINIMIZE)
    model.Params.timeLimit = 800
    model.optimize()

    An excel sheet is needed for the data that is used. I don't see a way of simplifying it feasibly. Is there a way I can email it. It needs to follow the data for one of the constraints. 

    After the optimization is solved I can see that the solution goes against BOTH the upper and lower bounds of the 'x' variable. 'x' is a semi-continuous variable, meaning it can be 0 or between the upper and lower bound (my understanding). Using the following code it can be seen it does not stay between the bounds ub and lb.

    matrix_agg = [[x[a,b].X for a in times] for b in zones]
    agg = pd.DataFrame(matrix_agg, columns=times, index=zones)
    agg.loc['aggregate', times] = agg.sum(axis=0)

    with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
    print(agg.loc['LO3',times])

    The x values for LO3 are printed. The upper and lower bounds are 15.7 and 9.7, respectively. The values are far beyond both bounds.

    Thank you for any help and please let me know how I can send the excel sheet so it can be ran. 

    Nico

    0
  • Nicolas Campbell
    • Gurobi-versary
    • First Comment
    • First Question

    Small addition:

    I did test out adding another constraint to x in the code 

    model.addConstrs(x[time,zone] <=ub[zone] for time in times for zone in zones)

    This successfully constrains 'x', however, I do not know a way to place a lower bound constraint while still allowing 'x' to equal 0 as well. If the resolution cannot be found for fixing ub and lb, it would be good to know how to do this at least. 

    Thanks

    0
  • Eli Towle
    • Gurobi Staff

    The indices of the \( \texttt{x} \) variables are all combinations of \( \texttt{times} \) and \( \texttt{zones} \):

    x = model.addVars(times, zones, ub=ub, lb=lb, vtype='S', name='x')

    However, the keys for the \( \texttt{lb} \) and \( \texttt{ub} \) dictionaries you pass as arguments to Model.addVars() are \( \texttt{zones} \):

    # only indexed by zones
    lb = {"R1": 0.8, "SO1": 0.36,"MO1": 1.0,"LO3": 9.7,"H4": 13.59}
    ub = {"R1": 20.75, "SO1": epcompile["SO1"].max(),"MO1": 27.933,
    "LO3": epcompile["LO3"].max(),"H4": epcompile["H4"].max()}

    This isn't a supported way of setting bounds with Model.addVars(). From the Model.addVars() documentation:

    You can also provide a Python dict as the argument. In that case, the value for each variable will be pulled from the dict, using the indices argument to build the keys. For example, if the variables created by this method are indexed as x[i,j], then the dict provided for the argument should have an entry for each possible (i,j) value.

    None of the \( \texttt{dict} \) keys match the indices argument, so the bounds are never properly set. To fix the issue, modify \( \texttt{lb} \) and \( \texttt{ub} \) so the keys match the indices of the \( \texttt{x} \) variables. E.g.:

    lb2 = {(i, j): lb[j] for i in times for j in zones}
    ub2 = {(i, j): ub[j] for i in times for j in zones}
    x = model.addVars(times, zones, ub=ub2, lb=lb2, vtype='S', name='x')
    1
  • Nicolas Campbell
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you so much. This worked perfectly and you explanation was great. I also want to say that from what I have seen on other posts you guys are great help in this community. I am sure everyone appreciates it!

    Nico

    0

投稿コメントは受け付けていません。