Skip to content

Commit 7dbf98a

Browse files
committed
First commit
0 parents  commit 7dbf98a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+108475
-0
lines changed

Apress-AI-master/Makefile

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
PYTHON = python3
2+
TEST:
3+
${PYTHON} test_classification.py gen 111
4+
${PYTHON} test_classification.py margins 111
5+
${PYTHON} test_blend_multi.py content 122
6+
${PYTHON} test_blend_multi.py target 122
7+
${PYTHON} test_blend_multi.py cost 122
8+
${PYTHON} test_blend_multi.py inventory 122
9+
${PYTHON} test_blend_multi.py run 122
10+
${PYTHON} test_gas_blend.py raw 103
11+
${PYTHON} test_gas_blend.py ref 103
12+
${PYTHON} test_gas_blend.py run 103
13+
${PYTHON} test_classification.py gen 111
14+
${PYTHON} test_classification.py plane 111
15+
${PYTHON} test_classification.py run 111
16+
${PYTHON} test_curve_fit.py data 115
17+
${PYTHON} test_curve_fit.py run 115
18+
${PYTHON} test_cutting_stock.py data 11
19+
${PYTHON} test_cutting_stock.py run 11
20+
${PYTHON} test_cutting_stock.py large 11
21+
${PYTHON} test_facility_location.py dcost 112
22+
${PYTHON} test_facility_location.py fcost 112
23+
${PYTHON} test_facility_location.py run 112
24+
${PYTHON} test_coexistence.py
25+
${PYTHON} test_job_shop.py data 129
26+
${PYTHON} test_job_shop.py run 129
27+
${PYTHON} test_maxflow.py data 117
28+
${PYTHON} test_maxflow.py run0 117
29+
${PYTHON} test_maxflow.py run1 117
30+
${PYTHON} test_mincost.py data 112
31+
${PYTHON} test_mincost.py run 112
32+
${PYTHON} test_diet_problem.py run 12
33+
${PYTHON} test_multi_commodity_flow.py data 112
34+
${PYTHON} test_multi_commodity_flow.py run 112
35+
${PYTHON} test_multi_commodity_flow.py pairs 112
36+
${PYTHON} test_piecewise.py data 110 250 False
37+
${PYTHON} test_piecewise.py run 110 250 False
38+
${PYTHON} test_tricks.py
39+
${PYTHON} test_piecewise.py ncvx 110 250 False
40+
${PYTHON} test_tricks_0.py
41+
${PYTHON} test_tricks_5.py
42+
${PYTHON} test_tricks_2.py
43+
${PYTHON} test_tricks_1.py
44+
${PYTHON} test_tricks_3.py
45+
${PYTHON} test_piecewise.py data 112 250 True
46+
${PYTHON} test_piecewise.py run 112 250 True
47+
${PYTHON} test_piecewise.py run 112 310 True
48+
${PYTHON} test_piecewise.py run 112 1 True
49+
${PYTHON} test_piecewise.py non 112 250 True
50+
${PYTHON} test_piecewise.py data 110 250 False
51+
${PYTHON} test_piecewise.py run 110 250 False
52+
${PYTHON} test_project_management.py data 13
53+
${PYTHON} test_project_management.py run 13
54+
${PYTHON} test_project_management.py runclp 13
55+
${PYTHON} test_set_cover.py data 129
56+
${PYTHON} test_set_cover.py run 129
57+
${PYTHON} test_set_packing.py data 103
58+
${PYTHON} test_set_packing.py run 103
59+
${PYTHON} test_shortest_path.py data 112
60+
${PYTHON} test_shortest_path.py run 112
61+
${PYTHON} test_shortest_path.py pm 112
62+
${PYTHON} test_shortest_path.py tree 112
63+
${PYTHON} test_shortest_path.py all 112
64+
${PYTHON} test_sports_timetabling.py data 12
65+
${PYTHON} test_sports_timetabling.py run 12
66+
${PYTHON} test_staff_scheduling.py section 11
67+
${PYTHON} test_staff_scheduling.py instructor 11
68+
${PYTHON} test_staff_scheduling.py sets 11
69+
${PYTHON} test_staff_scheduling.py pairs 11
70+
${PYTHON} test_staff_scheduling.py run 11
71+
${PYTHON} test_staffing.py data 112
72+
${PYTHON} test_staffing.py run 112
73+
${PYTHON} test_staffing.py runo 112
74+
${PYTHON} test_transship_dist.py data 117
75+
${PYTHON} test_transship_dist.py run 117
76+
${PYTHON} test_tsp.py data 118
77+
${PYTHON} test_tsp.py display 118
78+
${PYTHON} test_tsp.py path 118
79+
${PYTHON} test_tsp.py star 118

Apress-AI-master/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Apress-AI
2+
3+
This is the code described in the book, along with generators of
4+
random instances and test drivers.
5+
6+
7+
First, there is a `Makefile` that tests every model in the book. The
8+
reader should run a `make` before making any change to the code to
9+
verify that everything is working.
10+
11+
The only change that may be
12+
*required* is to change the value of the `PYTHONPATH` variable to
13+
indicate where is located Google's or-tools. This variable is set on
14+
the first line of the `Makefile`.
15+
16+
17+
For each section of the book, the structure is identical. Consider,
18+
for instance the chapter *Staffing*. The code in the book can be found
19+
in the file `staffing.py`; in the same file a generator of problem is
20+
included. And in the file `test_staffing.py` there is a driver that
21+
generates a problem and solves it. The reader can use this as an example
22+
on how to use the models.
23+

Apress-AI-master/bin_packing.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
from random import randint,uniform
3+
from math import ceil
4+
def gen_data(n):
5+
R,T=[],0
6+
for i in range(n):
7+
RR=[randint(6,10),randint(200,500)]
8+
T+=RR[0]*RR[1]
9+
R.append(RR)
10+
return R,randint(1200, 1500)
11+
12+
from my_or_tools import newSolver, ObjVal, SolVal
13+
14+
def solve_model(D,W,symmetry_break=False,knapsack=True):
15+
s = newSolver('Bin Packing',True)
16+
nbC,nbP = len(D),sum([P[0] for P in D])
17+
w = [e for sub in [[d[1]]*d[0] for d in D] for e in sub]
18+
nbT,nbTmin = bound_trucks(w,W)
19+
x = [[[s.IntVar(0,1,'') for _ in range(nbT)] \
20+
for _ in range(d[0])] for d in D]
21+
y = [s.IntVar(0,1,'') for _ in range(nbT)]
22+
for k in range(nbT):
23+
sxk = sum(D[i][1]*x[i][j][k] \
24+
for i in range(nbC) for j in range(D[i][0]))
25+
s.Add(sxk <= W*y[k])
26+
for i in range(nbC):
27+
for j in range(D[i][0]):
28+
s.Add(sum([x[i][j][k] for k in range(nbT)]) == 1)
29+
if symmetry_break:
30+
for k in range(nbT-1):
31+
s.Add(y[k] >= y[k+1])
32+
for i in range(nbC):
33+
for j in range(D[i][0]):
34+
for k in range(nbT):
35+
for jj in range(max(0,j-1),j):
36+
s.Add(sum(x[i][jj][kk] \
37+
for kk in range(k+1)) >= x[i][j][k])
38+
for jj in range(j+1,min(j+2,D[i][0])):
39+
s.Add(sum(x[i][jj][kk] \
40+
for kk in range(k,nbT))>=x[i][j][k])
41+
if knapsack:
42+
s.Add(sum(W*y[i] for i in range(nbT)) >= sum(w))
43+
s.Add(sum(y[k] for k in range(nbT)) >= nbTmin)
44+
s.Minimize(sum(y[k] for k in range(nbT)))
45+
rc = s.Solve()
46+
P2T=[[D[i][1], [k for j in range(D[i][0]) for k in range(nbT)
47+
if SolVal(x[i][j][k])>0]] for i in range(nbC) ]
48+
T2P=[[k, [(i,j,D[i][1]) \
49+
for i in range(nbC) for j in range(D[i][0])\
50+
if SolVal(x[i][j][k])>0]] for k in range(nbT)]
51+
return rc,ObjVal(s),P2T,T2P
52+
53+
def bound_trucks(w,W):
54+
nb,tot = 1,0
55+
for i in range(len(w)):
56+
if tot+w[i] < W:
57+
tot += w[i]
58+
else:
59+
tot = w[i]
60+
nb = nb+1
61+
return nb,ceil(sum(w)/W)

Apress-AI-master/blend_multi.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
from random import randint, uniform
3+
def gen_data_content(m,n):
4+
# Oils down, acids accross (more oils than acids m > n)
5+
R=[]
6+
for i in range(m):
7+
RR=[]
8+
P = 100
9+
for j in range(n-1):
10+
if P>1:
11+
acid = randint(1,min(70,P))*randint(0,1)
12+
else:
13+
acid = 0
14+
RR.append(acid)
15+
P -= acid
16+
RR.append(P)
17+
R.append(RR)
18+
return R
19+
def gen_data_target(C):
20+
F=[]
21+
R0,R1=[],[]
22+
m,n=len(C),len(C[0])
23+
P=100
24+
R=[0 for j in range(n)]
25+
for i in range(m-1):
26+
if P:
27+
f=randint(1,min(20,P))
28+
else:
29+
f=0
30+
F.append(f)
31+
P-=f
32+
for j in range(n):
33+
acid = f*C[i][j]
34+
R[j] += acid
35+
f=P
36+
F.append(f)
37+
for j in range(n):
38+
acid = f*C[m-1][j]
39+
R[j] += acid
40+
for j in range(n):
41+
R0.append((0.95*R[j]/100.0))
42+
R1.append((1.05*R[j]/100.0))
43+
return [R0,R1]
44+
def gen_data_cost(m,k):
45+
# Oils down, months accross
46+
R=[]
47+
for i in range(m):
48+
RR=[]
49+
for j in range(k):
50+
cost = randint(100,200)
51+
RR.append(cost)
52+
R.append(RR)
53+
return R
54+
def gen_data_inventory(m):
55+
# Oils down
56+
R=[]
57+
for i in range(m):
58+
cost = [randint(0,200)]
59+
R.append(cost)
60+
return R
61+
62+
from my_or_tools import SolVal, ObjVal, newSolver
63+
64+
def solve_model(Part,Target,Cost,Inventory,D,SC,SL):
65+
s = newSolver('Multi-period soap blending problem')
66+
Oils= range(len(Part))
67+
Periods, Acids = range(len(Cost[0])), range(len(Part[0]))
68+
Buy = [[s.NumVar(0,D,'') for _ in Periods] for _ in Oils]
69+
Blnd = [[s.NumVar(0,D,'') for _ in Periods] for _ in Oils]
70+
Hold = [[s.NumVar(0,D,'') for _ in Periods] for _ in Oils]
71+
Prod = [s.NumVar(0,D,'') for _ in Periods]
72+
CostP= [s.NumVar(0,D*1000,'') for _ in Periods]
73+
CostS= [s.NumVar(0,D*1000,'') for _ in Periods]
74+
Acid = [[s.NumVar(0,D*D,'') for _ in Periods] for _ in Acids]
75+
for i in Oils:
76+
s.Add(Hold[i][0] == Inventory[i][0])
77+
for j in Periods:
78+
s.Add(Prod[j] == sum(Blnd[i][j] for i in Oils))
79+
s.Add(Prod[j] >= D)
80+
if j < Periods[-1]:
81+
for i in Oils:
82+
s.Add(Hold[i][j]+Buy[i][j]-Blnd[i][j] == Hold[i][j+1])
83+
s.Add(sum(Hold[i][j] for i in Oils) >= SL[0])
84+
s.Add(sum(Hold[i][j] for i in Oils) <= SL[1])
85+
for k in Acids:
86+
s.Add(Acid[k][j]==sum(Blnd[i][j]*Part[i][k] for i in Oils))
87+
s.Add(Acid[k][j] >= Target[0][k] * Prod[j])
88+
s.Add(Acid[k][j] <= Target[1][k] * Prod[j])
89+
s.Add(CostP[j] == sum(Buy[i][j] * Cost[i][j] for i in Oils))
90+
s.Add(CostS[j] == sum(Hold[i][j] * SC for i in Oils))
91+
Cost_product = s.Sum(CostP[j] for j in Periods)
92+
Cost_storage = s.Sum(CostS[j] for j in Periods)
93+
s.Minimize(Cost_product+Cost_storage)
94+
rc = s.Solve()
95+
B,L,H,A = SolVal(Buy),SolVal(Blnd),SolVal(Hold),SolVal(Acid)
96+
CP,CS,P = SolVal(CostP),SolVal(CostS),SolVal(Prod)
97+
return rc,ObjVal(s),B,L,H,P,A,CP,CS

Apress-AI-master/coexistence.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
from ortools.linear_solver import pywraplp
3+
def solve_coexistence():
4+
t = 'Amphibian coexistence'
5+
s = pywraplp.Solver(t,pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
6+
x = [s.NumVar(0, 1000,'x[%i]' % i) for i in range(3)]
7+
pop = s.NumVar(0,3000,'pop')
8+
s.Add(2*x[0] + x[1] + x[2] <= 1500)
9+
s.Add(x[0] + 3*x[1] + 2*x[2] <= 3000)
10+
s.Add(x[0] + 2*x[1] + 3*x[2] <= 4000)
11+
s.Add(pop == x[0] + x[1] + x[2])
12+
s.Maximize(pop)
13+
s.Solve()
14+
return pop.SolutionValue(),[e.SolutionValue() for e in x]

Apress-AI-master/curve_fit.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
from random import randint, uniform
3+
def gen_data(myfunc,n):
4+
R=[]
5+
for i in range(n):
6+
RR=[]
7+
t = i+uniform(-0.2,0.2)
8+
RR.append(t)
9+
RR.append(myfunc(t)*uniform(0.8,1.2))
10+
R.append(RR)
11+
return R
12+
13+
from my_or_tools import ObjVal, SolVal, newSolver
14+
15+
def solve_model(D,deg=1,objective=0):
16+
s,n = newSolver('Polynomial fitting'),len(D)
17+
b = s.infinity()
18+
a = [s.NumVar(-b,b,'a[%i]' % i) for i in range(1+deg)]
19+
u = [s.NumVar(0,b,'u[%i]' % i) for i in range(n)]
20+
v = [s.NumVar(0,b,'v[%i]' % i) for i in range(n)]
21+
e = s.NumVar(0,b,'e')
22+
for i in range(n):
23+
s.Add(D[i][1]==u[i]-v[i]+sum(a[j]*D[i][0]**j \
24+
for j in range(1+deg)))
25+
for i in range(n):
26+
s.Add(u[i] <= e)
27+
s.Add(v[i] <= e)
28+
if objective:
29+
Cost = e
30+
else:
31+
Cost = sum(u[i]+v[i] for i in range(n))
32+
s.Minimize(Cost)
33+
rc = s.Solve()
34+
return rc,ObjVal(s),SolVal(a)

0 commit comments

Comments
 (0)