Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys. I'm having an issue with a Python PulP MILP problem. You can find the simplified code that reproduces the problem here:
Python
from pulp import *
machines = 2
I = range(machines)
positions = 2
J = range(positions)
years = 10
T = range(years)
age = {0: 5, 1: 7}

IR = 0.06
df = 0.3

costs = {(0,0):300, (0,1):200, (1,0):500, (1,1):350}

factor = {}
finalcosts = {}
for i in I:
    for j in J:
        for t in T:
            for k in range(age[i]):
                factor[t,k] = ((1-df)**k)/((1+IR)**t)
                finalcosts[i,j,t,k] = costs[i,j]*factor[t,k]
                
prob = LpProblem("TrialProb",LpMinimize)


Prob_vars = LpVariable.dicts("probvars", ((Machine, Position,Year, Age) for Machine in I for Position in J for Year in T for Age in range(age[i])),0,None, LpInteger)


This gives me a 'finalcosts' variable with a size of 240 which is what I want, with all the correct values. But the 'Prob_vars' are of a size 260, counting the second index k for the first index i as well. Meaning that while in 'finalcosts' for i=0, k=0:4 and for i=2, k=0:6 (which is what I want), for the 'Prob_vars' decision variable index k=0:6 for both i=1 & i=2.

I'm fairly new to Python so I can't quite grasp where the problem lies.

What I have tried:

I've tried all combinations that I could think of for different expressions of the 'Prob_vars' but nothing works properly. Like taking the indices expressions out of the variable statement:
Python
for i in I:
    for j in J:
        for t in T:
            for Age in range(lifetime[i]):
                Prob_vars = LpVariable.dicts("probvars", ((Machine, Position,Year, 
                Age), 0,None, LpInteger)

I also looked anywhere I could think of online but I can't find an answer to this.
Posted
Updated 8-Aug-18 18:27pm

1 solution

Prob_vars = LpVariable.dicts("probvars", ((Machine, Position,Year, Age) for Machine in I for Position in J for Year in T for Age in range(age[i])),0,None, LpInteger)

You're iterating over "Machine" twice in the inner loop while holding index "i".
 
Share this answer
 
Comments
Member 13941108 9-Aug-18 8:16am    
Thank you Gerry!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900