-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (74 loc) · 3.07 KB
/
main.py
File metadata and controls
92 lines (74 loc) · 3.07 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
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
import threading, random, os, time, multiprocessing, sysv_ipc
import argparse
from home import Home
from weather import Weather
from market import Market
from sharing_platform import SharingPlatform
from multiprocessing import Value
# ipcrm -Q 123; ipcrm -Q 321; python main.py
if __name__ == "__main__":
#définition des arguments dans la ligne de commande
parser = argparse.ArgumentParser(usage='main.py -n NB_HOUSES -d NB_DAYS -t TEMPERATURE -p NB_POLICY_1 NB_POLICY_2 NB_POLICY_3')
parser.add_argument('-n', "--houses", type=int, metavar='NB_HOUSES', help="number of houses to create", required=True)
parser.add_argument('-d', "--days", type=int, metavar='NB_DAYS', help="number of days to perform", required=True)
parser.add_argument('-t', "--temperature", type=int, metavar='TEMPERATURE', help="initial temperature", required=True)
parser.add_argument('-p', "--policies", nargs="+", help="number of houses for each policy")
args = parser.parse_args()
#attribution des arguments aux variables
nb_days = int(args.days)
nb_houses = int(args.houses)
temperature = int(args.temperature)
if args.policies:
sum = 0
for i in args.policies:
sum += int(i)
if sum != nb_houses:
print("Number of houses doesn't match the policies. Exiting the cool program.")
exit(1)
stop = Value('b', False)
barrier = multiprocessing.Barrier(4 + nb_houses) # 1 main + 1 weather + 1 external + 1 sharing platform + N home
#Création des threads/processes
weather = Weather(barrier, stop)
shrd_temp = weather.getSharedMem()
shrd_temp.value = temperature
weather_thread = threading.Thread(target= weather.genTemperature, args=())
weather_thread.start()
market = Market(shrd_temp, barrier, stop)
market.start()
sharing_platform = SharingPlatform(args.houses, barrier, stop)
sharing_platform.start()
homes = []
if not args.policies:
for i in range(args.houses):
homes.append(Home(random.randint(1,3), shrd_temp, barrier, stop))
else:
policy = 1
for i in args.policies:
i = int(i)
for j in range(i):
homes.append(Home(policy, shrd_temp, barrier, stop))
policy += 1
for home in homes:
home.start()
#Début des journée
print("\n--- Journée 1 ---\n")
barrier.wait()
t0 = time.time()
for cpt in range(1,nb_days): #itération sur le nombre de journée voulu
t1 = time.time()
if t1-t0 > 3: #1 journée toutes les 3s
print("\n--- Journée {} ---\n".format(cpt + 1))
barrier.wait()
t0 = t1
while barrier.n_waiting < barrier.parties - 1: #attend que tout le monde ait fini sa journée
pass
print("\n")
stop.value = True
barrier.wait() #fin des journées
#fermeture des processes/threads
for home in homes:
home.join()
market.join()
sharing_platform.join()
weather_thread.join()
print("\nYour favorite Energy Market Simulator is over....")