-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear-programming-model.mos
151 lines (121 loc) · 4.29 KB
/
linear-programming-model.mos
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
model "linear-programming-transeuro"
!----------------------------------------------------------------------------------------
! Linear Programming Model - Transeuro
! Sergio García Prado - garciparedes.me
! March 2018
!----------------------------------------------------------------------------------------
uses "mmxprs";
declarations
n: integer
p: integer
end-declarations
initializations from "data.dat"
n p
end-initializations
declarations
vertices = 1..n
products = 1..p
demand: array(vertices, products) of real
cost: array(vertices, vertices) of real
capacity: array(vertices) of real
end-declarations
initializations from "data.dat"
demand
cost
capacity
end-initializations
n_origins := sum(v in vertices | sum(k in products) demand(v, k) > 0) 1
n_destinations := sum(v in vertices | sum(k in products) demand(v, k) < 0 ) 1
n_warehouses := sum(v in vertices | sum(k in products) demand(v, k) = 0 ) 1
declarations
origins = 1..n_origins
destinations = 1..n_destinations
warehouses = 1..n_warehouses
c: array(origins, products) of real
d: array(destinations, products) of real
cap: array(warehouses) of real
cst: array(origins, destinations, warehouses) of real
x: array(vertices, vertices, vertices, products) of mpvar
end-declarations
forall(k in products) do
i_origins := 1
i_destinations := 1
i_warehouses := 1
forall(v in vertices) do
if(demand(v, k) > 0) then
c(i_origins, k) := demand(v, k)
i_origins := i_origins + 1;
elif(demand(v, k) < 0) then
d(i_destinations, k) := - demand(v, k)
i_destinations := i_destinations + 1
end-if
end-do
end-do
k1:=1
i_origins := 1
forall(i in vertices) do
j_destinations := 1
origins_change := FALSE
forall(j in vertices) do
w_warehouses := 1
destinations_change := FALSE
forall(w in vertices) do
if(demand(i, k1) > 0 and demand(j, k1) < 0 and demand(w, k1) = 0) then
cst(i_origins, j_destinations, w_warehouses) := cost(i, w) + cost(w, j)
origins_change := TRUE
destinations_change := TRUE
w_warehouses := w_warehouses + 1
end-if
end-do
if (destinations_change = TRUE) then
j_destinations := j_destinations + 1
end-if
end-do
if (origins_change = TRUE) then
i_origins := i_origins + 1
end-if
end-do
i_warehouses := 1
forall(v in vertices | capacity(v) > 0) do
cap(i_warehouses) := capacity(v)
i_warehouses := i_warehouses + 1
end-do
!----------------------------------------------------------------------------------------
! Model
!----------------------------------------------------------------------------------------
forall(i in origins, k in products) do
res_in(i, k) := sum(j in destinations, w in warehouses) x(i, j, w, k) <= c(i, k)
end-do
forall(w in warehouses) do
res_max(w) := sum(i in origins, j in destinations, k in products) x(i, j, w, k) <= cap(w)
end-do
forall(j in destinations, k in products) do
res_out(j, k) := sum(i in origins, w in warehouses) x(i, j, w, k) >= d(j, k)
end-do
objetive := sum(i in origins, j in destinations, w in warehouses, k in products) x(i, j, w, k)*(cst(i, j, w))
minimize(objetive)
!----------------------------------------------------------------------------------------
forall(k in products) do
writeln
writeln("k = ", k)
forall(i in origins) do
writeln
writeln("\ti = ", i)
write("\t\t\t")
forall(j in destinations) do
write(j, "\t")
end-do
forall(w in warehouses) do
writeln
write("\t\t",w, "\t")
forall(j in destinations) do
write(getsol(x(i, j, w, k)), "\t")
end-do
end-do
writeln
end-do
writeln
end-do
writeln
writeln("objetive = ", getobjval)
end-model