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

How to Use the addGenConstrMax Function with Decision Variables

回答済み

コメント

6件のコメント

  • Ryuta Tamura
    Gurobi Staff Gurobi Staff

    Hi,

    The second argument of addGenConstrMax must be a list of "Var" object. It seems that your input_vars is a list of LinExpr object. In this case you can use auxiliary variables (e.g. vector a), and define the constraints of a[i] is  equal to input_vars[i]. Then,you can call addGenConstrMax like as:

    addGenConstrMax(max_value_t, a, name=f"max_value_constraint_{t}")

    Thanks,
    Ryuta

    0
  • Juntong Zhao
    First Question
    First Comment

    Hello, Ryuta,

    Thank you for your response. However, the actual situation might differ slightly from what you mentioned. We can observe that input_vars is a list, structured like this: [0.5, 0, 0.7, 0, 0.9]. I understand your point that you intend for the vector a[t] to correspond to an input_vars, but this seems to present some issues. Here is the relevant portion of the code:

    max_value = model.addVars(time_indices, vtype=GRB.CONTINUOUS, name='max_value')
    a=model.addVars(time_indices, vtype=GRB.CONTINUOUS, name='a')
    fortintime_indices:
    max_value = model.addVars(time_indices, vtype=GRB.CONTINUOUS, name='max_value')

    a=model.addVars(time_indices, vtype=GRB.CONTINUOUS, name='a')

    for t in time_indices:
    max_value_t=model.addVar(vtype=GRB.CONTINUOUS, name=f'max_value_{t}')

    input_vars= [
    (x[station - 1] / s_max[station - 1]) * merged_data[station].loc[t, 'value']
    for station in range(1, num_stations+1)
    ]

    model.addConstr(a[t] ==input_vars, name=f'a_{t}')
    model.addGenConstrMax(max_value_t, a, name=f'max_value_constraint_{t}')
    max_value[t] = max_value_t

    model.addConstr(
    t_duration* quicksum(
    max_value[t] fortintime_indices
    ) >= t_min_connect,
    "connectivity_constraint"
    )

    If you could kindly help me revise it, I would be most grateful!

    Best regards,
    Juntong

    0
  • Ryuta Tamura
    Gurobi Staff Gurobi Staff

    Hi,

    According to the part of your first post where you print input_vars:

    [<gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>, <gurobi.LinExpr: 0.0 <gurobi.Var *Awaiting Model Update*>>]

    This implies that input_vars is a list of LinExpr. LinExpr object and Var object are different things, and addGenConstrMax accept the list of "Var" object.

    I missed the outer loop of your code, if you fix it like this, will it work?:

    # max_value = model.addVars(time_indices, vtype=GRB.CONTINUOUS, name='max_value')
    max_value = {}
    a = model.addVars(time_indices, range(1, num_stations+1), vtype=GRB.CONTINUOUS, name='a')
    for t in time_indices: max_value_t=model.addVar(vtype=GRB.CONTINUOUS, name=f'max_value_{t}') input_vars= [ (x[station - 1] / s_max[station - 1]) * merged_data[station].loc[t, 'value'] for station in range(1, num_stations+1) ] for station in range(1, num_stations+1):
      model.addConstr(a[t,station] ==input_vars[station], name=f'a_{t}_{station}') model.addGenConstrMax(max_value_t, a.select(t,'*'), name=f'max_value_constraint_{t}') max_value[t] = max_value_t model.addConstr( t_duration* quicksum( max_value[t] for t in time_indices ) >= t_min_connect, "connectivity_constraint" )

    Thanks,
    Ryuta

    0
  • Juntong Zhao
    First Question
    First Comment
     

    Thank you for your response; it worked perfectly! I really appreciate your help! I also hope this post can assist others who encounter the same issue.

    Additionally, Ryuta, I’ve come across another problem. It seems that the program has crashed, possibly due to the large size of the dataset I’m trying to process. Do you have any suggestions on how to address this issue?

    Gurobi Optimizer version 11.0.3 build v11.0.3rc0 (linux64 - "Ubuntu 20.04.4 LTS")

    CPU model: AMD EPYC 9124 16-Core Processor, instruction set [SSE2|AVX|AVX2|AVX512]
    Thread count: 16 physical cores, 32 logical processors, using up to 20 threads

    Optimize a model with 126144161 rows, 132451320 columns and 129520768 nonzeros
    Model fingerprint: 0x38a75b55
    Model has 126144000 general constraints
    Variable types: 132451200 continuous, 120 integer (40 binary)
    Coefficient statistics:
      Matrix range     [2e-01, 1e+01]
      Objective range  [1e+00, 1e+01]
      Bounds range     [1e+00, 1e+00]
      RHS range        [5e+00, 4e+05]
    Killed

    Thank you in advance!
    Juntong

    0
  • Ryuta Tamura
    Gurobi Staff Gurobi Staff

    Hi,
    Your model looks large, so your program may have crashed due to memory problems.

    Optimize a model with 126144161 rows, 132451320 columns and 129520768 nonzeros

    Checking memory usage may reveal the cause.

    There is an article about out of memory, but in this case, since the problem occurs in the before solving part, other way may be necessary. For example, one direction is to consider the possibility of reducing the size of the problem by splitting it up.

    Thanks,
    Ryuta

    0
  • Juntong Zhao
    First Question
    First Comment

    I will carefully read the paper you provided. 
    Thanks a lot!
    Juntong

    0

サインインしてコメントを残してください。