-3

I created a model, but having trouble understanding how to list my formulation (retrieve an LP file, list of constraints) and understanding if I entered by Constraint expressions correctly? The optimization works in PULP but not with PYSCIPOPT. Please see below:

model = Model('name')

shift_starts = {}
load = {}
capacity = {}
unmet_demand = {}

for zone in zones:

    for timeslot in timeslots:

        load[zone,timeslot] = model.addVar(vtype="C",name="load(%s,%s)" % (zone,timeslot), lb=0.0)
        capacity[zone,timeslot] = model.addVar(vtype="C",name="capacity(%s,%s)" % (zone,timeslot), lb=0.0)
        unmet_demand[zone,timeslot] = model.addVar(vtype="C",name="unmet_demand(%s,%s)" % (zone,timeslot), lb=0.0)


        for length in shift_lengths:

            shift_starts[zone,length,timeslot] = model.addVar(vtype="I",name="shift_starts(%s,%s,%s)" % (zone,length,timeslot), lb=0) 


for zone in zones:

    for timeslot in timeslots[:11]:

        rhs = Expr()

        for length in shift_lengths:

            if length - timeslot > 1:


                rhs += quicksum(shift_starts[zone, length, timeslot2] for timeslot2 in timeslots[timeslot-length+1:])

                rhs += quicksum(shift_starts[zone, length, timeslot2] for timeslot2 in timeslots[:timeslot+1])

            else:

                rhs += quicksum(shift_starts[zone, length, timeslot2] for timeslot2 in timeslots[timeslot-length+1:timeslot+1])

        model.addCons(capacity[zone,timeslot] <= rhs*productivity[zone][0], name="capacity(%s,%s)" % (zone,timeslot))

for zone in zones:

    for timeslot in timeslots[12:]:

        rhs = Expr()

        for length in shift_lengths:

            rhs += quicksum(shift_starts[zone, length, timeslot2] for timeslot2 in timeslots[timeslot-length+1:timeslot+1])

        model.addCons(capacity[zone,timeslot] <= rhs*productivity[zone][0], name="capacity(%s,%s)" % (zone,timeslot))

for zone in zones:

    for timeslot in timeslots:

        model.addCons(unmet_demand[zone, timeslot] >= load[zone, timeslot] - capacity[zone, timeslot], name="unmet_demand(%s,%s)" % (zone,timeslot))

for zone in zones:

    model.addCons(load[zone,0] >= unmet_demand[zone,(t-1)] + arrivals[zone][0], name="load(%s,%s)" % (zone,timeslot))

    for timeslot in timeslots[1:]:

        model.addCons(load[zone,timeslot] >= unmet_demand[zone,timeslot-1] + arrivals[zone][timeslot], name="load(%s,%s)" % (zone,timeslot))

rhs = Expr()

for length in shift_lengths:

    model.addCons(quicksum(shift_starts[zone, length, timeslot] for zone in zones for timeslot in timeslots) <= max_shifts[length-1], name="shifts(%s)" % (length))

    rhs += quicksum(shift_starts[zone, length, timeslot] for zone in zones for timeslot in timeslots) * length

model.setObjective(quicksum(shift_starts[zone,length,timeslot]  for (zone, length, timeslot) in shift_starts), "minimize")

model.optimize()

Are my constraints being set correctly?

1 Answers1

0

Please note that it is not possible to execute your code right now because some variables have not been defined yet. The syntax seems to be okay, but sorry, we won't check the correctness of your model for you. I would suggest that you use model.writeProblem("model.cip") to export your model into the *.cip format. This format is human-readable and might allow you to spot the issue in your code.

mueldgog
  • 383
  • 1
  • 7
  • Thank you, I was not finding the documentation on this package very clear. Creating the .cip file made it easy for me to review the constraints, found one small issue and corrected it. The model seems to be correct now. Is there a way to load a model from a .lp or .mps file? or save to these formats? I am not seeing it in the documentation. – mistryneil Feb 10 '20 at 16:47
  • Yes, you can read a problem by using something like `m = Model()` and afterwards `m.readProblem("mymodel.lp")`. – mueldgog Feb 11 '20 at 11:35