Inteligentna_smieciarka/garbage_truck.py
2023-06-16 12:20:46 +02:00

109 lines
3.9 KiB
Python

from heuristicfn import heuristicfn
FIELDWIDTH = 50
TURN_FUEL_COST = 10
MOVE_FUEL_COST = 200
MAX_FUEL = 20000
MAX_SPACE = 5
MAX_WEIGHT = 400
MAX_WEIGHT_GLASS = 100
MAX_WEIGHT_MIXED = 100
MAX_WEIGHT_PAPER = 100
MAX_WEIGHT_PLASTIC = 100
class GarbageTruck:
garbage_types = {'bio': 0, 'electronics': 1, 'mixed': 2, 'recyclable': 3}
def __init__(self, dump_x, dump_y, rect, orientation, request_list: list, clf):
self.dump_x = dump_x
self.dump_y = dump_y
self.fuel = MAX_FUEL
self.free_space = MAX_SPACE
self.weight_capacity = MAX_WEIGHT
self.weight_capacity_glass = MAX_WEIGHT_GLASS
self.weight_capacity_mixed = MAX_WEIGHT_MIXED
self.weight_capacity_paper = MAX_WEIGHT_PAPER
self.weight_capacity_plastic = MAX_WEIGHT_PLASTIC
self.rect = rect
self.orientation = orientation
self.request_list = request_list #lista domów do odwiedzenia
self.clf = clf
def turn_left(self):
self.orientation = (self.orientation - 1) % 4
self.fuel -= TURN_FUEL_COST
def turn_right(self):
self.orientation = (self.orientation + 1) % 4
self.fuel -= TURN_FUEL_COST
def forward(self):
self.fuel -= MOVE_FUEL_COST
if self.orientation == 0:
self.rect.x += FIELDWIDTH
elif self.orientation == 1:
self.rect.y += FIELDWIDTH
elif self.orientation == 2:
self.rect.x -= FIELDWIDTH
else:
self.rect.y -= FIELDWIDTH
def next_destination(self):
for i in range(len(self.request_list)):
if(self.request_list==[]):
break
request = self.request_list[i]
#nie ma miejsca w zbiorniku lub za ciężkie śmieci
if request.volume > self.free_space or request.weight > self.weight_capacity:
continue
#nie straczy paliwa na dojechanie i powrót na wysypisko
if heuristicfn(request.x_pos, request.y_pos, self.dump_x, self.dump_y) // 50 * 200 > self.fuel:
continue
self.request_list.pop(i)
self.free_space -= request.volume
self.weight_capacity -= request.weight
return request.x_pos, request.y_pos
return self.dump_x, self.dump_y
def collect(self, garbage_type):
if self.rect.x == self.dump_x and self.rect.y == self.dump_y:
self.fuel = MAX_FUEL
self.free_space = MAX_SPACE
self.weight_capacity = MAX_WEIGHT
self.weight_capacity_plastic = MAX_WEIGHT_PLASTIC
self.weight_capacity_mixed = MAX_WEIGHT_MIXED
self.weight_capacity_glass = MAX_WEIGHT_GLASS
self.weight_capacity_paper = MAX_WEIGHT_PAPER
if self.request_list==[]:
return 1
else:
request = self.request_list[0]
if garbage_type == "glass":
if request.weight > self.weight_capacity_glass:
return 1
self.weight_capacity_glass -= request.weight
elif garbage_type == "mixed":
if request.weight > self.weight_capacity_mixed:
return 1
self.weight_capacity_mixed -= request.weight
elif garbage_type == "paper":
if request.weight > self.weight_capacity_paper:
return 1
self.weight_capacity_paper -= request.weight
elif garbage_type == "plastic":
if request.weight > self.weight_capacity_plastic:
return 1
self.weight_capacity_plastic -= request.weight
print(f'agent at ({self.rect.x}, {self.rect.y}); fuel: {self.fuel}; free space: {self.free_space}; weight capacity: {self.weight_capacity}, glass_capacity: {self.weight_capacity_glass}, mixed_capacity: {self.weight_capacity_mixed}, paper_capacity: {self.weight_capacity_paper}, plastic_capacity: {self.weight_capacity_plastic}')
return 0
pass