-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drone.py
142 lines (122 loc) · 4.08 KB
/
Drone.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
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
'''
Created on 11 fevr. 2016
@author: capitaine slip
'''
import Warehouse
import numpy as np
class Drone(object):
'''
classdocs
'''
def __init__(self, location, payloadWeight, weights):
'''
Constructor
'''
self.pos = location
self.payload = {}
self.payloadWeight = payloadWeight
self.currentPayloadWeight = 0
self.busy = False
self.commandType = 'W'
self.command = []
self.weights = weights
self.timeleft = 0
def goto(self, location):
distance = np.sqrt((self.pos[0]-location[0])*(self.pos[0]-location[0]) + (self.pos[1]-location[1])*(self.pos[1]-location[1]))
self.pos = location
return np.floor(distance)
def gotoWH(self,warehouse):
return self.goto(warehouse.location)
def load(self,objet,n,warehouse):
weight = self.weights[objet]
if weight*n + self.currentPayloadWeight <= self.payloadWeight:
self.currentPayloadWeight += weight*n
if objet in self.payload :
self.payload[objet] += n
else:
self.payload[objet] = n
warehouse.take(objet,n)
else:
raise ValueError
return 1
def takeCommand(self,param):
self.command = param[1:]
self.commandType = param[0]
if self.commandType == 'D':
self.timeleft = self.goto(param[0]) + 1
self.busy = True
if self.commandType == 'W':
self.timeleft = param[0]
self.busy = True
if self.commandType == 'L' or self.commandType == 'U':
self.timeleft = self.gotoWH(param[0])
self.busy =True
return 0
def step(self):
if self.timeleft>1:
self.timeleft -=1
else:
self.timeleft = 0
out = self.execute()
if out <> False:
return out
def execute(self):
self.busy =True
if self.commandType == 'D':
return self.deliver(self.command[2],self.command[3])
if self.commandType == 'L':
self.load(self.command[2],self.command[3],self.command[1])
return False
if self.commandType == 'U':
self.unload(self.command[2],self.command[3],self.command[1])
return False
def unload(self, objet,n, warehouse):
unloaded = 0
weight = self.weights[objet]
if objet not in self.payload:
return unloaded
if self.payload[objet] >= n:
self.currentPayloadWeight -= n*weight
self.payload[objet] -= n
warehouse.put(objet,n)
unloaded = n
else:
self.currentPayloadWeight -= self.payload[objet]*weight
unloaded = self.payload[objet]
warehouse.put(objet,self.payload[objet])
self.payload[objet] = 0
return unloaded
def deliver(self,objet,n):
weight = self.weights[objet]
delivered = False
if objet not in self.payload:
return delivered
if self.payload[objet] >= n:
self.currentPayloadWeight -= n*weight
self.payload[objet] -= n
delivered = n
else:
self.currentPayloadWeight -= self.payload[objet]
delivered = self.payload[objet]
self.payload[objet] = 0
return self.command([1].deliver(objet,n))
def __str__(self):
return "Drone , maxPayload : " + str(self.payloadWeight) +"\n"
+ "payload [" + str(self.payload) + "]\n"
+ "current weight " + str(self.currentPayloadWeight)
if __name__ == "__main__":
D = Drone((10,10),30,[1,1,1,1])
W = Warehouse.Warehouse((2,5), {0:5,2:6,4:7})
print D
print D.goto((24,34))
print D.gotoWH(W)
print D.load(0, 4, W)
print D.unload(0, 1, W)
D.takeCommand('W', [4])
print D.busy
D.step()
D.step()
D.step()
D.step()
print D.busy
print "BONGEOURRE"