Skip to content

Commit 333083a

Browse files
committed
up
1 parent 7970697 commit 333083a

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

.ipynb_checkpoints/INF421 P 2021 _ Matching under constraints-checkpoint.ipynb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,26 @@
475475
"source": [
476476
"## Test Instance 3:"
477477
]
478+
},
479+
{
480+
"cell_type": "markdown",
481+
"metadata": {},
482+
"source": [
483+
"# Task 11:"
484+
]
485+
},
486+
{
487+
"cell_type": "markdown",
488+
"metadata": {},
489+
"source": [
490+
"Here we use an Integer Linear Programing algorithm. Since no algorithm has been proposed in the course and the implementation of the usual algorithms is complicated, we have chosen to use the library ```pulp``` of Python that implements the ILP.\n",
491+
"\n",
492+
"The problem corresponds to the following model :\n",
493+
"\n",
494+
"\\begin{center}\n",
495+
"Maximize c^{T} x\n",
496+
"\\end{center}"
497+
]
478498
}
479499
],
480500
"metadata": {

INF421 P 2021 _ Matching under constraints.ipynb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,11 @@
489489
"source": [
490490
"Here we use an Integer Linear Programing algorithm. Since no algorithm has been proposed in the course and the implementation of the usual algorithms is complicated, we have chosen to use the library ```pulp``` of Python that implements the ILP.\n",
491491
"\n",
492-
"The problem correspond"
492+
"The problem corresponds to the following model :\n",
493+
"\n",
494+
"\\begin{center}\n",
495+
"Maximize $c^{T} x$\n",
496+
"\\end{center}"
493497
]
494498
}
495499
],

Task11.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import numpy as np
22
from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable
33

4-
def optimize(w, p, Ws):
4+
def optimize(w, n, Ws):
55
"""
66
w is the list of costs of the different groups.
7-
p is the list of the proportions of different groups.
7+
n is the number of candidate students from each group.
88
Ws is the budget of the school.
99
"""
10-
G = len(p)
10+
G = len(n)
11+
N = sum(n) # total number of all students
12+
p = np.array([n[i] / N for i in range(G)])
1113
model = LpProblem(name="Task_5.1", sense=LpMaximize)
1214
groups = np.array([[LpVariable(name="x" + str(i), lowBound=0, cat="Integer")] for i in range(G)]) # variables
1315
model += lpSum(groups) # objective function
14-
print(lpSum(groups))
1516

16-
# Constraints
17+
# #Constraints
18+
# Cost constraint
1719
cost = []
1820
for i in range(G):
1921
cost.append(w[i,0] * groups[i, 0])
2022
model += lpSum(cost) <= Ws
21-
print(lpSum(cost) <= Ws)
2223

24+
# Number constraint
25+
for i in range(G):
26+
model += 1 * groups[i,0] <= n[i,0]
27+
28+
#4/5- rule
2329
A1 = np.array([[6 / 5 * p[i, 0]] * G for i in range(G)])
2430
A2 = np.array([[-4 / 5 * p[i, 0]] * G for i in range(G)])
2531
for i in range(G):
@@ -31,16 +37,15 @@ def optimize(w, p, Ws):
3137

3238
for i in range(G):
3339
model += set1[i, 0] >= 0
34-
print(set1[i, 0] >= 0)
3540
model += set2[i, 0] >= 0
36-
print(set2[i, 0] >= 0)
3741

42+
# Solution
3843
status = model.solve()
3944

4045
for var in model.variables():
4146
print(f"{var.name}: {var.value()}")
4247

4348
w = np.array([[1], [10]])
44-
p = np.array([[3/4], [1/4]])
45-
Ws = 11
46-
optimize(w, p, Ws)
49+
n = np.array([[3], [1]])
50+
Ws = 1000
51+
optimize(w, n, Ws)

0 commit comments

Comments
 (0)