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
dassouki
  • 6,286
  • 7
  • 51
  • 81
  • 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). – Erwin Kalvelagen Oct 30 '20 at 15:49

1 Answers1

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
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39