Search code examples
arraysmaxlinear-programminggurobi

How to get maximum value of matrix containing linear expressions?


I hope that someone can help me. For the solution of an optimisation problem I have to get the maximum of a Matrix containing linear expressions to minimize this value in a second step.

For example I have the unbounded decision variables x and y

x.append(m.addVar(vtype=GRB.CONTINUOUS, lb=-GRB.INFINITY, ub=+GRB.INFINITY, name="x")))
y.append(m.addVar(vtype=GRB.CONTINUOUS, lb=-GRB.INFINITY, ub=+GRB.INFINITY, name="y")))

and the Matrix M = [0.25*x,0.25*x+y]. The maximum of the Matrix should be saved as M_max. Later the objective is to minimize M_max --> m.setObjective( M_max , GRB.MINIMIZE)

When I try it by typing in M_max = amax(M) I always get back the first element, here 0.25x. What operation returns the "real" maximum value? (Of Course my model is more complicated but I hope that you can understand my problem)

Thanks a lot for your help!


Solution

  • The manual approach would be:

    introduce aux-var z (-inf, inf, cont)
    add constraints
        0.25*x <= z
        0.25*x+y <= z
    minimize (z)
    

    Not sure if gurobi nowadays provide some automatic way.

    Edit It seems newer gurobi-versions provide this functionality (of automatic reformulation) like explained here (python-docs; you need to check if those are available for your interface too; which could be python)

    max_ ( variables )

    Used to set a decision variable equal to the maximum of a list of decision variables (or constants). You can pass the arguments as a Python list or as a comma-separated list.

     # example probably based on the assumption:
     #   import: from gurobipy import *
     m.addConstr(z == max_(x, y, 3))
     m.addConstr(z == max_([x, y, 3]))
    

    You did not show what amax is you used. If it's numpy's amax or anything outside of gurobi, you cannot use it! Gurobi-vars don't behave as classic fp-variables and every operation on those variable-objects need to be backed by gurobi (often hidden through operator-overloading) or else gurobi can't make sure it's formalizing a valid mathematical-model.