-2

Using Gurobi and Python I could optimally solve a linear problem for a given situation. However, when I am given a range in which a lower bound for one of the variable can increase in an increment of 1, I could not figure how to write the right syntax (for loop, while, or other), I think I am missing alot so to speak. Secondly, If I were to plot the optimal objective value sequentially obtained versus each increment of the lower bound of subject variable, how can I go about it? Honestly, I could not exactly imagine how would I retain for every solve its optimal value with its relevant lower bound individually to be shown in a single plot at the end.

  • What do you mean exactly with "an increment of 1", are you talking about integer programming, if your bound is different than the regular case it should be as easy as to adjust the constant term in your constraints. – Zafi Sep 06 '16 at 09:49
  • I meant that the model has to be solved sequentially by increasing the lower bound of a variable by 1 each time. So if subject variable is x bounded by LB and UB respectively, LB is increased by 1 over a predefined range, say [100-200] starting by 100 and ending at 200. UB remains constant. – Ayman Halim Sep 06 '16 at 10:27
  • So your lower bound changes while optimizing? What is your application? – Zafi Sep 06 '16 at 11:13
  • Also: any optimal solution with the lowest lower bound should be optimal with a higher lower bound as well. – Zafi Sep 06 '16 at 11:14
  • I am not sure if understood you right. However, I am being asked to modify the code so that I sequentially solve the model for each increment in the lower bound and plot the optimal objective vs. lower bound, kinda of sensitivity analysis. My issue is how to achieve this sequential solving since a static problem solving is straightforward. – Ayman Halim Sep 06 '16 at 11:20
  • Don't you mean you have to solve the optimization problem for different values for the lower bound to see the effect of the lower bound on the optimization problem? This would mean just solving the optimization problem for every lower bound. I fail to see the use of doing this in a sequential fashion. Can you write mathematically what you are trying to do – Zafi Sep 06 '16 at 13:31
  • Yes. I think I figured it out using numpy.linspace to create an array then for loop for iterations. Saving the objective function in an array indexed by the lower bound helped me in plotting. Thanks for your attention. – Ayman Halim Sep 07 '16 at 12:17
  • @AymanHalim consider http://stackoverflow.com/help/how-to-ask before asking the question. – n1tk Sep 27 '16 at 00:22

1 Answers1

0

First, define a class that acts as a wrapper for a Gurobi model. It should take a lower bound as an argument which you'll use when defining your variable.

class GurobiModel:

    def __init__(self, lowerBound):

        # create an empty Gurobi model object
        self.model = Model()

        # add your variable using the argument as the lower bound
        self.variable1 = self.model.addVar(lb=lowerBound,vtype=GRB.INTEGER, name='variable1')

        # add more variables, constraints, and objective function
        ...

    def solve(self):
        """Solves the model and returns the optimal objective value (or None if model is infeasible)."""
        self.model.optimize()

        if model.status == GRB.Status.OPTIMAL:
            # if model is optimal, return objective value
            return self.model.objVal
        else:
            print('Model is infeasible')
            return None

Now you can create a main function that creates a new GurobiModel object (with a specified lower bound for your variable of interest) and solves it to optimality:

def main():
    N = 50              # number of iterations
    result_set = []     # this stores tuples containing the lower bound and the objective function

    for i in range(N):
        model = GurobiModel(lowerBound = i)
        objVal = model.solve()
        result_set.append((i, objVal))

Hopefully this is enough to get you started. There's lots that could be done differently, but this will be fine for smaller models (for example, if your model is quite large, you wouldn't want to rebuild it in entirety every iteration; it would make more sense to extract the variable using the getVarByName() method and change the lower bound).

Bill
  • 670
  • 5
  • 6