File: test4.py

package info (click to toggle)
python-pulp 1.6.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 14,596 kB
  • sloc: python: 6,006; sh: 12; makefile: 5
file content (59 lines) | stat: -rw-r--r-- 1,506 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# @(#) $Jeannot: test4.py,v 1.5 2004/03/20 17:06:54 js Exp $

# A two stage stochastic planification problem

# Example taken from:
# "On Optimal Allocation of Indivisibles under Incertainty"
# Vladimir I. Norkin, Yuri M. Ermoliev, Andrzej Ruszczynski
# IIASA, WP-94-021, April 1994 (revised October 1995).

from pulp import *
from random import *

C = 50
B = 500 # Resources available for the two years
s = 20 # Number of scenarios
n = 10 # Number of projects

N = list(range(n))
S = list(range(s))

# First year costs
c = [randint(0,C) for i in N]
# First year resources
d = [randint(0,C) for i in N]
# a=debut, b=taille
interval = [[(randint(0,C), randint(0,C)) for i in N] for j in S]
# Final earnings
q = [[randint(ai, ai+bi) for ai,bi in ab] for ab in interval]
# Second year resources
delta = [[randint(ai, ai+bi) for ai,bi in ab] for ab in interval]

# Variables
# x : Whether or not to start a project
x = LpVariable.matrix("x", (N,), 0, 1, LpInteger)
# y : Whether or not to finish it, in each scenario
y = LpVariable.matrix("y", (S, N), 0, 1, LpInteger)

# Problem
lp = LpProblem("Planification", LpMinimize)

# Objective: expected earnings
lp += lpDot(x, c) - lpDot(q, y)/float(s)

# Resources constraints for each scenario
for j in S:
	lp += lpDot(d, x) + lpDot(delta[j], y[j]) <= B

# We can only finish a project that was started
for i in N:
	for j in S:
		lp += y[j][i] <= x[i]

# Resolution
lp.solve()

# Solution printing
for i in N:
	print(x[i], "=", x[i].value())