def loadScenario(self): with open(self.path) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 center = Vector2D(0, 0) for row in csv_reader: if line_count == 0: print(f' {", ".join(row)}') line_count += 1 else: event = [float(row[0]), float(row[1]) / self.factor, float(row[2]) / self.factor, float(row[3]) / self.factor, float(row[4]) / self.factor, float(row[5]), float(row[6]), float(row[7])] self.scheduling.append(event) self.center.x = self.center.x + event[1] self.center.y = self.center.y + event[2] line_count += 1 self.center.x = (self.center.x / line_count) self.center.y = (self.center.y / line_count) self.environment.center = self.center self.upLeft = Vector2D(0, 0) self.downRight = Vector2D(0, 0) if len(self.scheduling) >= 1: self.upLeft.x = self.scheduling[0][1] self.upLeft.y = self.scheduling[0][2] self.downRight.x = self.scheduling[0][1] self.downRight.y = self.scheduling[0][2] for event in self.scheduling: event[1] = event[1] event[2] = event[2] print(f'\t{event[0]} s ; [{event[1]},{event[2]}] to [{event[3]},{event[4]}] - {event[7]} passagers.') if self.upLeft.x > event[1]: self.upLeft.x = event[1] if self.upLeft.y > event[2]: self.upLeft.y = event[2] if self.downRight.x < event[1]: self.downRight.x = event[1] if self.downRight.y < event[2]: self.downRight.y = event[2] print( "c: " + self.center.toString() + " upLeft :" + self.upLeft.toString() + " downRight :" + self.downRight.toString()) self.environment.boardW = (self.downRight.x - self.upLeft.x) self.environment.boardH = (self.downRight.y - self.upLeft.y) print(self.environment.boardW) print(self.environment.boardH) self.ready = True
def __init__(self): self.id = util.id_generator(10, "1234567890") self.mass = 1 self.location = Vector2D(util.randomInt(400), util.randomInt(400)) self.velocity = Vector2D(util.randomInt(50), util.randomInt(50)) self.fustrum = CircularFustrum(20) self.orientation = 0 self.vitesseMax = 15 self.vitesseMin = 1.0 self.accelerationMax = 15
def computeMove(self, v): m = Vector2D(v.x, v.y) if m.getLength() <= 0: m = Vector2D(0, 0) return m if m.getLength() > self.vitesseMax: m = m.getNormalized() m = m.scale(self.vitesseMax) return m
def __init__(self, path): threading.Thread.__init__(self) self.limitSimulation = 6000 self.environment = EnvironmentTaxis() self.path = path self.ready = False self.obsManager = ObserverManager("./res") self.scheduling = [] self.factor = 10 self.nbTaxi = 25 self.center = Vector2D(0, 0) self.upLeft = Vector2D(0, 0) self.downRight = Vector2D(0, 0) self.Gui = None
def run(self): if self.ready: print("START") iterator = 0; startTime = int(time.time()) while iterator < len(self.scheduling) - 1 or self.environment.getRandomAgent("Client") is not None: elapseTime = int(time.time()) - startTime if elapseTime > self.limitSimulation: iterator = len(self.scheduling) elif elapseTime > self.scheduling[iterator][0]: iterator = iterator + 1 for i in range(0, int(self.scheduling[iterator][7])): d = self.environment.getRandomObject("Destination") a = Client() a.addDestination(d) x = self.scheduling[iterator][1] - self.environment.center.x + self.environment.boardW / 2 y = self.scheduling[iterator][2] - self.environment.center.y + self.environment.boardH / 2 a.body.location = Vector2D(x, y) print( "start agent " + str(iterator) + "/" + str(len(self.scheduling)) + " t:" + str(elapseTime)) self.environment.addAgent(a) self.obsManager.addObservation(a.observer) print("Fin de simulation") self.Gui.stop2() else: print("Erreur de simulation")
def __init__(self, id, type): self.id = id self.type = type self.dernierePosition = Vector2D(0, 0) self.distance=0 self.distanceTheorique = 0.0 self.timeout=0
def updateMarche(self, location): if self.dernierePositionM is None: self.dernierePositionM = location self.distanceMarche =0 else: self.distanceMarche = self.distanceMarche + abs(self.dernierePositionM.distance(location)) self.dernierePositionM = Vector2D(location)
def update(self): inf = AnimateAction(None, None, None) nearby_boids, other_b, attractors = self.filtrePerception() cohesion_vector = self.average_position(nearby_boids) alignment_vector = self.average_velocity(nearby_boids) attractor_vector = self.attraction(attractors) boid_avoidance_vector = self.avoid_collisions(nearby_boids) obstacle_avoidance_vector = self.avoid_collisions(other_b) self.change_vectors = [(self.cohesionFactor, cohesion_vector), (self.allignFactor, alignment_vector), (self.attractorFactor, attractor_vector), (self.avoidanceFactor, boid_avoidance_vector), (self.obstacleFactor, obstacle_avoidance_vector) ] for factor, vec in self.change_vectors: self.velocity[0] += factor * vec[0] self.velocity[1] += factor * vec[1] self.velocity = util.limit_magnitude(self.velocity, self.body.vitesseMax, self.body.vitesseMin) inf.move = Vector2D(self.velocity[0], self.velocity[1]) self.body.velocity = inf.move return inf
def signedAngle(v1, v2): a = Vector2D(v1.x, v1.y) if a.getLength() == 0: return None b = Vector2D(v2.x, v2.y) if b.getLength() == 0: return None a = a.getNormalized() b = b.getNormalized() cos = a.x * b.x + a.y * b.y sin = a.x * b.y - a.y * b.x angle = math.atan2(sin, cos) return angle
def getNextByDistance(source, destinations): v = Vector2D(0, 0) mini = 100000000 for d in destinations: if source.distance(d.body.location) < mini: mini = source.distance(d.body.location) v = d return v.body
def loadTaxi(self): x = (self.downRight.x + self.upLeft.x) / 2 y = (self.downRight.y + self.upLeft.y) / 2 for i in range(0, self.nbTaxi): t = Taxi(self.obsManager) t.body.location = Vector2D(x - self.center.x + self.environment.boardW / 2, y - self.center.y + self.environment.boardH / 2) print("Taxi :" + t.body.location.toString()) self.environment.addAgent(t)
def __init__(self): threading.Thread.__init__(self) self.center = Vector2D(800 / 2, 600 / 2) self.boardW = 1280 self.boardH = 720 self.running = 1 self.clock = 0 self.agents = [] self.objects = [] self.perceptionList = {} self.influenceList = {}
def update(self): influence = AnimateAction(None, None, None) influence.move = Vector2D(0, 0) if self.onboard == -1: if time.time() - self.observer.HCommande > self.timeout: self.observer.timeout = 1 self.stat = -1 return influence # kill me if self.stat == 2: self.stat = -1 return influence if self.onboard == 1: return influence else: if self.destination.location.distance(self.body.location) < 2: self.stat = 2 return influence if self.stat == 0: influence.move = self.moveRandom() if self.stat == 1: nearby_client = self.filtrePerception() influence.move = Vector2D(0.0, 0.0) if len(nearby_client) > 0: if self.policy == ClientsPolicy.COHESION: cohesion_vector = self.average_position(nearby_client) alignment_vector = self.average_velocity(nearby_client) influence.move = self.moveTo(cohesion_vector) self.observer.updateMarche(self.body.location) self.body.velocity = influence.move return influence
def applyInfluence(self, dt): for k, influence in self.influenceList.items(): if influence is None: continue agentBody = self.getAgentBody(k) if agentBody is not None: move = Vector2D(influence.move.x, influence.move.y) rotation = 0 move = agentBody.computeMove(move) move = move.scale(dt) agentBody.move(move)
def __init__(self, f): Agent.__init__(self) self.body = BoidsBody() self.type = "StandardAgent" self.famille = f self.body.mass = 80 self.body.fustrum.radius = 100 self.body.vitesseMax = 150.0 self.body.vitesseMin = 20.0 self.velocity = [ random.uniform(-50.0, 50.0), random.uniform(-50.0, 50.0) ] self.avoidanceFactor = 7.5 self.obstacleFactor = 500 self.target = Vector2D(0, 0)
from helper.util import signedAngle from helper.vector2D import Vector2D v =Vector2D(0,10) v1 =Vector2D(0,-10) a = signedAngle(v,v1) print(a)
def __init__(self): self.body = None self.move = Vector2D(0.0, 0.0) self.velocity = Vector2D(0.0, 0.0) self.rotatoin = Vector2D(0.0, 0.0)
def toOrientationVector(angle): return Vector2D(math.cos(angle), math.sin(angle))
def moveTo(self, d): return Vector2D(d.location.x - self.body.location.x, d.location.y - self.body.location.y)
def removeClient(self, c): self.clients.remove(c) self.occupation = self.occupation - 1 c.onboard = 0 c.body.location = Vector2D(self.body.location.x, self.body.location.y)
def moveTo(self, d): return Vector2D(d[0] - self.body.location.x, d[1] - self.body.location.y)
def update(self, location,type): self.distance = self.distance + abs(self.dernierePosition.distance(location)) self.dernierePosition = Vector2D(location)
def on_mouse_release(x, y, button, modifiers): nonlocal mouse_location o = self.environment.getFirstObjectByName("Attractor") if o is not None: o.location = Vector2D(-1000, -1000)
def on_mouse_drag(x, y, dx, dy, buttons, modifiers): o = self.environment.getFirstObjectByName("Attractor") if o is not None: o.location = Vector2D(x, y)
def on_mouse_leave(x, y): o = self.environment.getFirstObjectByName("Attractor") print("leave") if o is not None: o.location = Vector2D(-1000, -1000)
def __init__(self): Object.__init__(self) self.location = Vector2D(30, 30) self.orientation = 0 self.type = "EnvironmentalObject"
def __init__(self, x, y): EnvironmentalObject.__init__(self) self.location = Vector2D(x, y) self.type = "Attractor"
def __init__(self, x, y): EnvironmentalObject.__init__(self) self.location = Vector2D(x, y) self.orientation = 0 self.type = "Destination"
def moveRandom(self): x = int(random.uniform(-2, 2)) y = int(random.uniform(-2, 2)) return Vector2D(x, y)