Skip to main content

Possible to define objective parameters in constraints?

Answered

Comments

5 comments

  • Official comment
    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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    Hi Tiril!

    Could you share a minimal reproducible example of the code which throws the error? We will then be able to give you better advice.

    Best regards
    Jonasz

    0
  • Tiril Amundsen
    • Gurobi-versary
    • First Comment
    • First Question
    import pandas as pd
    import gurobipy as gp
    from gurobipy import *
    import numpy as np
    from gurobipy import GRB
    import matplotlib.pyplot as plt
    import sys

    ## Sets ##
    criteria = [0, 1, 2, 3, 4]
    decisionM = [0, 1]
    alternative = [0, 1]

    ## Parameters ##

    weight = np.array( [[0.03, 0.09, 0.28, 0.14, 0.46], [0.39, 0.15, 0.08, 0.06, 0.32]])

    V1 = np.array([[59800000, 68, 0.5, 0.5, 40], [65622000, 165, 0.58, 0.5, 76]])

    V2 = np.array([[59800000, 68, 1, 1, 1100], [65622000, 165, 0.75, 0.5, 450]])

    V1_min = []
    for c in criteria:
        if V1[0,c]<V1[1,c]:
            V1_min.append(V1[0,c])
        else:
            V1_min.append(V1[1,c])
            

    V2_min = []
    for c in criteria:
        if V2[0,c]<V2[1,c]:
            V2_min.append(V1[0,c])
        else:
            V2_min.append(V2[1,c])            


    ## Model ##
    m = gp.Model('Weighted-sum-method')

    #Decision variable
    x = m.addVars(alternative, decisionM, vtype=GRB.BINARY, name='x')

    # Defining U1
    for c in criteria:
        for a in alternative:
            m.addConstr(
                    (U1[a,c] == V1_min[c]/V1[a,c]), 'Utility1')
        
    # Defining U2            
    for c in criteria:
        for a in alternative:
            m.addConstr(
                    (U2[a,c] == V2_min[c]/V2[a,c]), 'Utility1')
               
    # Only one alternative can be chosen by each decision maker
    for a in alternative: 
        m.addConstr(
                (sum(x[a,d] for d in decisionM) == 1), 'onealternative')
        
    # Objective function
    obj = m.setObjective(sum(x[a,0]*weight[0,c]*U1[a,c] for a in alternative 
                             for c in criteria) + sum(x[a,1]*weight[1,c]*U2[a,c] 
                                 for a in alternative for c in criteria), 
                                    GRB.MAXIMIZE)
        
    m.optimize()
    0
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    Hi Tiril,

    1. What are \(U1, U2\)? Are they variables? If so, you need to define them as such upfront, for example by:

    U1 = m.addVars(alternative, criteria, vtype="C", name="u1")
    U2 = m.addVars(alternative, criteria, vtype="C", name="u2")

    just after the model definition. Adding these lines solved the problem on my machine.

    2. On a side note, you may want to give all constraints different names. Your code names both U1 and U2 constraints 'Utility1'.

    Best regards
    Jonasz

    0
  • Tiril Amundsen
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you, it worked!

    0

Post is closed for comments.