Skip to content
Snippets Groups Projects
Commit f3329ba7 authored by Hailong Zhu's avatar Hailong Zhu
Browse files

first upload

parents
No related branches found
No related tags found
No related merge requests found
from abc import ABC
from shapely.geometry import Point, LineString
from shapely.geometry.polygon import Polygon
import numpy as np
def vector_between_two_points(point1, point2):
# returns a vector, created from two coordinates
return point1.x-point2.x, point1.y - point2.y
class Line(LineString):
def __init__(self, model, points, index=None):
self.model = model
self.index = index
self.direction = vector_between_two_points(Point(points[0]), Point(points[1]))
super().__init__(points)
class Loadpoint(Point):
def __init__(self, model, coordinates, index, capacity):
self.model = model
self.env = self.model.env
super().__init__((coordinates[0], coordinates[1]))
self.index = index
self.capacity = capacity
self.management = BottleneckManagement(self, self.env, model, index, capacity)
self.stop_distance = self.model.arguments['loadpoint_stop_distance'] # distance to the bottleneck, where a vehicle without clearance should stop
self.request_distance = self.model.arguments['loadpoint_request_distance']
def __str__(self):
return 'Loadpoint{}_({},{})'.format(self.index, self.x, self.y)
class OneWay(Polygon, ABC):
def __init__(self, model, points, index, start_points_index):
self.model = model
self.index = index
self.ring = LineString(list(self.exterior.coords))
end_points = list()
start_points = list()
for i in range(0, 4):
if i not in start_points_index:
end_points.append(points[i])
else:
start_points.append(points[i])
def vector_between_two_points(p1, p2):
return p2[0] - p1[0], p2[1] - p1[1]
self.borders = list()
#self.borders.append(OneWayBorder(self.model, self, (end_points[0], end_points[1]), str(index) + '_1'))
vector = vector_between_two_points(start_points[0], end_points[0])
if np.array_equal(vector, vector_between_two_points(start_points[1], end_points[1])):
self.direction = vector
#self.borders.append(OneWayBorder(self.model, self, (start_points[0], end_points[0]), str(index) + '_2'))
#self.borders.append(OneWayBorder(self.model, self, (start_points[1], end_points[1]), str(index) + '_3'))
else:
self.direction = vector_between_two_points(start_points[1], end_points[0])
#self.borders.append(OneWayBorder(self.model, self, (start_points[0], end_points[1]), str(index) + '_2'))
# self.borders.append(OneWayBorder(self.model, self, (start_points[1], end_points[0]), str(index) + '_3'))
self.start_point = ((start_points[0][0] + start_points[1][0]) / 2, (start_points[0][1] + start_points[1][1]) / 2)
self.end_point = ((end_points[0][0] + end_points[1][0]) / 2, (end_points[0][1] + end_points[1][1]) / 2)
self.start_point1 = start_points[0]
self.end_point1 = tuple(map(lambda x, y: x+y, start_points[0], self.direction))
self.start_point2 = start_points[1]
self.end_point2 = tuple(map(lambda x, y: x+y, start_points[1], self.direction))
super().__init__([self.start_point1, self.end_point1, self.end_point2, self.start_point2])
def get_side_borders(self):
return LineString([self.start_point1, self.end_point1]), LineString([self.start_point2, self.end_point2])
def point_in_oneway(self, point):
if self.intersects(point):
return True
if self.contains(point):
return True
return False
class OneWayBorder(Line):
instances = list()
def __init__(self, model, one_way, coords, index):
Line.__init__(self, model, coords)
self.oneway = one_way
self.index = index
OneWayBorder.instances.append(self)
def __str__(self):
return 'one_way_border{}_({},{}),({},{})'.format(self.index, self.coord1[0], self.coord1[1], self.coord2[0], self.coord2[1])
class Obstacle(Polygon):
def __init__(self, model, points, index):
self.model = model
self.index = index
super().__init__(points)
def __str__(self):
return 'obstacle{}'.format(self.index)
def contains_line(self, line):
if self.contains(line):
return True
p1 = Point(line.bounds[:2])
p2 = Point(line.bounds[2:])
a = self.distance(p2)
if self.distance(p2)<0.01 and self.distance(p1)<0.01:
return True
return False
def contains_point(self, point):
if self.intersects(point):
return True
return False
class Wall(Line):
def __init__(self, model, points, index):
self.model = model
self.index = index
super().__init__(model, points, index)
def contains_point(self, point): #todo: das nochmal genau überpüfen, ob das überall so referenziert wird? Oder wird irgendwo mit contains die sub Klasse angesprochen?
if self.distance(point) < self.model.arguments['tolerance']:
return True
else:
return False
def __str__(self):
return 'Wall{}'.format(self.index)
class BottleneckManagement:
def __init__(self, point, env, model, index, capacity):
self.env = env
self.model = model
self.index = index
self.capacity = capacity
self.point = point
self.priority_rule = 'FIFO'
self.arriving = dict()
self.waiting = dict()
self.vehicle_with_allowance = list()
self.env.process(self.acting())
def check_vehicle_permission(self, vehicle):
if vehicle in self.vehicle_with_allowance:
return True
else:
return False
def add_to_waiting(self, vehicle):
try:
del self.arriving[vehicle]
except:
self.model.print_to_console('Warning: Vehicle not in arriving list')
self.waiting[vehicle] = self.env.now
def request_for_service(self, vehicle):
if vehicle not in self.arriving:
self.arriving[vehicle] = self.env.now
def has_capacity(self):
if len(self.vehicle_with_allowance) >= self.capacity:
return False
else:
return True
def next_vehicle(self):
if self.priority_rule == 'FIFO':
if len(self.waiting) > 0:
vehicle = min(self.waiting.keys(), key=(lambda k: self.waiting[k]))
return vehicle
elif len(self.arriving) > 0:
vehicle = min(self.arriving.keys(), key=(lambda k: self.arriving[k]))
return vehicle
else:
return None
else:
pass
def acting(self):
while True:
if (len(self.waiting) == 0 and len(self.arriving) == 0) or self.has_capacity()==False:
yield(self.env.timeout(0.1))
else:
vehicle = self.next_vehicle()
self.vehicle_with_allowance.append(vehicle)
try:
del self.waiting[vehicle]
vehicle.action.interrupt()
except:
pass
try:
del self.arriving[vehicle]
except:
pass
def remove_from_loadpoint(self, vehicle):
self.vehicle_with_allowance.remove(vehicle)
\ No newline at end of file
node.py 0 → 100644
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 30 00:10:26 2022
@author: s3340992
"""
class node:
def __init__(self, node_id, x, y):
self.node_id = node_id
self.x = x
self.y = y
def on_node(self, x, y):
if (self.x == x and self.y == y):
return True
else:
return False
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 29 23:00:36 2022
@author: s3340992
"""
import math
from node import node
class vehicle:
def __init__(self, number, manufacturer, pos_x, pos_y, battery_state, theta, last_node_id):
self.number = number
self.manufacturer = manufacturer
self.pos_x = pos_x/100
self.pos_y = pos_y/100
self.battery_state = battery_state
self.theta = theta
self.last_node_id =last_node_id
self.last_msg_time = 0
self.send_msg = False
def update_pos(self, pos_x, pos_y, time, node_list):
self.last_pos_x = self.pos_x
self.last_pos_y = self.pos_y
self.pos_x = pos_x/100
self.pos_y = pos_y/100
#self.theta = math.pi/2
self.theta = math.atan((self.pos_y-self.last_pos_y)/(self.pos_x-self.last_pos_x))
self.last_update_time = time
#print(time)
if ((time-self.last_msg_time) >= 1):
self.send_msg = True
for i in range(len(node_list)):
if (node_list[i].on_node(self.pos_x,self.pos_y)):
self.send_msg = True
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment