Skip to main content

Cant Find Optimal Solution

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.
  • Maliheh Aramon
    • Gurobi Staff

    Hi Saina, 

    I don't see any change in the result with what I have given the algorithm.

    I am not sure what you are referring to? I do not see that you have provided the Gurobi Optimizer with any start solution. Could you please elaborate on what results you have given the algorithm? Please also post the code inside a code block with minimum input data that we can run (do not attach it as an image).

     Your implementation looks fine to me, although I am not sure what it is the idea behind using matrix variables and then building the objective and constraints via accessing the variables element-wise. You can use the Model.addVars() method if you are not aiming to construct linear or quadratic matrix expressions to represent constraints/objectives.

    Gurobi Python examples and/or Gurobi Jupyter Notebook Modeling Examples are great resources if you are interested to learn more about Gurobi Python API.

    Best regards,

    Maliheh

    0
  • SAinA
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you for your answer. y vector is an array of (309,1) data points.

    I am using element-wise access in the objective function because I receive an error in matrix multiplication.

    My second question is if it is correct to define something out of the objective function and use it inside the objective, like model 2 that I have added to the code.

    #Model 1
    import gurobipy as gp
    from gurobipy import GRB

    decision = np.zeros(len(Label_pred1))

        # Create a new model
    m = gp.Model()
        
    # Create variables
    z = m.addMVar(shape=(len(Label_pred1),1), vtype='I')
    y = Label_pred1
    b = m.addMVar(shape=(len(Label_pred1),1),vtype=GRB.BINARY) # for big M method

    # Set objective
    m.setObjective((gp.quicksum((20*z[i,0] + b[i,0]*(z[i,0]-y[i,0])*50+
                               (1-b[i,0])*(y[i,0]-z[i,0])*500) for i in range(len(Label_pred1)))),GRB.MINIMIZE)
        
    # Constants
    eps = 0.0001
    M   = 10000 + eps  # smallest possible given bounds on x and y

    # Add constraint: to make b zero or 1
    m.addConstrs(z[i,0] >= y[i,0] + eps - M * (1 - b[i,0]) for i in range(len(Label_pred1)))
    m.addConstrs(z[i,0] <= y[i,0] + M * b[i,0] for i in range(len(Label_pred1)))

    # Optimize model
    m.optimize()


    #Model 2
    import gurobipy as gp
    from gurobipy import GRB

    decision = np.zeros(len(Label_pred1))

    # Create a new model
    m = gp.Model()
        
        # Create variables
    z = m.addMVar(shape=(len(Label_pred1),1), vtype='I')
    y = Label_pred1
    b = m.addMVar(shape=(len(Label_pred1),1),vtype=GRB.BINARY) # for big M method

    Csh=0
    for i in range(len(Label_pred1)):
      Csh = Csh +b[i,0]*(z[i,0]-y[i,0])*50

    Csp=0
    for i in range(len(Label_pred1)):
      Csp = Csp +(1-b[i,0])*(y[i,0]-z[i,0])*500

    C=0
    for i in range(len(Label_pred1)):
      C = C +20*z[i,0]
     
    # Set objective
    m.setObjective((C + Csp + Csh) , GRB.MINIMIZE)
        
        # Constants
    eps = 0.0001
    M   = 10000 + eps  # smallest possible given bounds on x and y

     # Add constraint: to make b zero or 1
    m.addConstrs(z[i,0] >= y[i,0] + eps - M * (1 - b[i,0]) for i in range(len(Label_pred1)))
    m.addConstrs(z[i,0] <= y[i,0] + M * b[i,0] for i in range(len(Label_pred1)))

     # Optimize model
    m.optimize()
    0
  • Maliheh Aramon
    • Gurobi Staff

    Hi Saina, 

    The answer to your second question is yes, you can. Please see an implementation of your example using Python Matrix API below. Please check the documentations of MLinExpr() and MQuadExpr() to better understand the comments in the script.

    import gurobipy as gp
    from gurobipy import GRB

    y = np.random.rand(309)
    n = len(y)
    eps = 0.0001
    BigM = 10000 + eps

    m = gp.Model("model")

    z = m.addMVar(n, vtype="I")
    b = m.addMVar(n, vtype="B")
    # bprime = 1 - b
    bprime = m.addMVar(n, vtype="B")

    Csh = b @ (z - y) * 50
    # It is not possible to multiply matrix linear expressions, that's why we
    # are using bprime (matrix variables) instead of 1 - b (matrix linear expression)
    Csp = bprime @ (y - z) * 500
    obj = Csh + Csp
    # The linear terms should have shape (1,) to be added to a matrix quadratic
    # expression
    obj += gp.quicksum(z[i] * 20foriinrange(n))
    m.setObjective(obj, GRB.MINIMIZE)

    # Add a constraint enforcing bprime = 1 - b
    m.addConstr(bprime == 1 - b, name="c0")
    m.addConstr(z >= y + eps - BigM * bprime, name="c1")
    m.addConstr(z <= y + BigM * b, name="c2")

    m.optimize()

    Best regards, 

    Maliheh

    0
  • SAinA
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you very much. This was very helpful.

    0

Post is closed for comments.