-
Notifications
You must be signed in to change notification settings - Fork 0
/
donnees.py
91 lines (75 loc) · 2.27 KB
/
donnees.py
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
import json
from dataclasses import dataclass, field
@dataclass
class Article:
id: int
nombre: int
prix: float
indice: int
poids: float = 0
def set_poids(self, poids):
self.poids = poids
def __hash__(self):
return hash(str(self))
def __eq__(self,other):
return self.id == other.id
@dataclass
class donnees:
e_min: int
e_max: int
L_max: int
cd: float
r_min: int
capacites: list
Articles: list[Article]
def borne_trivial(self):
C = sum(self.capacites)
Score = D = 0
for article in sorted(
self.Articles, key=lambda x: x.prix, reverse=True):
if D >= C:
break
n = min(article.nombre, C)
Score += n * (article.prix - self.cd)
D += n
return Score
def import_donnees_from_json(chemin):
json_data = json.load(open(chemin))
return donnees(
e_min=json_data["e_min"],
e_max=json_data["e_max"],
L_max=json_data["L_max"],
cd=json_data["cd"],
r_min=json_data["r_min"],
capacites=json_data["capacites"],
Articles=[
Article(
id=article["id"],
nombre=article["nombre"],
prix=article["prix"],
indice=article["indice"],
)
for article in json_data["Articles"]
],
)
def print_donnees(donnees):
print("e_min : ", donnees.e_min)
print("e_max : ", donnees.e_max)
print("L_max : ", donnees.L_max)
print("cd : ", donnees.cd)
print("r_min : ", donnees.r_min)
print("capacites : ", donnees.capacites)
print("Article : ")
print("id : ", donnees.Articles[0].id)
print("nombre : ", donnees.Articles[0].nombre)
print("prix : ", donnees.Articles[0].prix)
print("indice : ", donnees.Articles[0].indice)
def Articles_to_csv(donnees, name_file):
with open(name_file +".csv", "w") as f:
f.write("id,nombre,prix,indice \n")
for article in donnees.Articles:
f.write(f"{article.id},{article.nombre},{article.prix},{article.indice} \n")
if __name__ == "__main__":
instance = "instances_test/test3.json"
D = import_donnees_from_json(instance)
Articles_to_csv(D, instance)