1

I am following a book that explains advanced linear programs. So far it's been good, but this example has me stumped on how to properly write the objective functions and constraints.

linear program example

Below you'll find my attempt at coding the left coefficient but as you can see it doesn't seem correct.

My implementation so far is as follow

   import pulp as lp

# data
r = 0.02
T = 2
J = 3
E = 6
K = {(1): 2, (2): 3, (3): 4, (4): 4, (5): 6, (6): 7}
C_j_t = {(1, 1): 2, (1, 2): 3, (2, 1): 4, (2, 2): 5, (3, 1): 6, (3, 2): 7}
F_j_e = {(1, 1): 2, (1, 2): 3, (2, 1): 4, (2, 2): 5, (3, 1): 6, (3, 2): 7}
I_j_e = {(1, 1): 2, (1, 2): 3, (2, 1): 4, (2, 2): 5, (3, 1): 6, (3, 2): 7}
# F_j_e = 1

prob = lp.LpProblem('Foobar Village Highway Problem', lp.LpMaximize)

X_j_t = lp.LpVariable.dicts("X",
                            [(j, t)
                             for j in range(1, J)
                             for t in range(1, T)],
                            cat='Continuous')

coef_left = {}                
for t in range(1, T):
    for j in range(1, J):
        coef_left[(j, t)] = (1 + r)**-t * C_j_t[(j, t)]

pv = (1 + r)**-T
right_side = {}
for j in range(1, J):
    for e in range(1, K[(j)]):
        right_side[(j, e)] = pv * F_j_e[(j, e)] * I_j_e[(j, e)]



prob += lp.lpSum(coef_left[(j, t)] * X_j_t[(j, t)] + right_side[(j, e)]
                 for j in range(1, J)
                 for t in range(1, T)
                 for e in range(1, E))


    prob.writeLP(r'8.2.1.lp')
    # Solve 
    prob.solve()

I don't think this is correct or that I'm using the right methodology to code this complex objective an constraint.

it returns the following:

\* Foobar_Village_Highway_Problem *\
Maximize
OBJ: 3.92156862745 X_(1,_1) + 7.8431372549 X_(2,_1)
Subject To
Bounds
X_(1,_1) free
X_(2,_1) free
End
1
  • Your objective is very different from the text. I would suggest staying closer to the formulas in the text. Also, do print(prob) instead of writing an LP file (so you don't lose any constant terms). Commented Oct 30, 2020 at 15:49

1 Answer 1

0

We cannot reproduce much because the fragment is incomplete, but there seems an issue with the loop structure:

prob += lp.lpSum(coef_left[(j, t)] * X_j_t[(j, t)]
                 for j in range(1, J)
                 for i in range(1, T))  <----- for t
Sign up to request clarification or add additional context in comments.

1 Comment

I updated my code with the whole objective function + output from it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.