-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization.py
More file actions
54 lines (39 loc) · 1.49 KB
/
optimization.py
File metadata and controls
54 lines (39 loc) · 1.49 KB
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
# Giulia Chiucchi e Gustavo Gomes
from pulp import *
import time
import json
def optimize(items, maxVolume):
print(items)
# number of items
itemCount = len(items)
# Initialize the problem and specify the type
model = LpProblem(name="Maximizar Lucro", sense=LpMaximize)
# Initialize the decision variables
x = LpVariable.dicts('Quantidade de produtos', range(itemCount), lowBound = 0, cat=LpInteger)
# Add objective function
model += lpSum([ x[i] * items[i]["lucro"] for i in range(itemCount) ]) # "Objetivo: Maximizar lucro"
# Add constraints to model
for i in range(itemCount):
model += x[i] <= items[i]["quantidade"] # "Quantidade de produtos <= quantidade disponível"
model += (lpSum([ x[i] * items[i]["volume"] for i in range(itemCount) ]) <= maxVolume, "VolumeProds")
model.solve()
# The optimised objective function value is printed to the screen
print(value(model.objective))
for var in model.variables():
print(f"{var.name}: {var.value()}")
used_items = []
for i in range(itemCount):
item = {
"nome": items[i]["nome"],
"quantidade": x[i].varValue,
"volume": items[i]["volume"],
"lucro": items[i]["lucro"]
}
used_items.append(item)
# Return the solution
return json.dumps({
"status": LpStatus[model.status],
"lucro": value(model.objective),
"max_volume": maxVolume,
"items": used_items
})