| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace MultiAgentSystemPCL
- {
- public class WasteAgent : ObjectInWorld
- {
- protected const double STEP = 3;
- protected const double CHANGE_DIRECTION_PROB = 0.05;
- protected Waste load = null;
- protected double speedX;
- protected double speedY;
- protected WasteEnvironment env;
- protected bool busy = false;
- public WasteAgent(double _posX, double _posY, WasteEnvironment _env)
- {
- PosX = _posX;
- PosY = _posY;
- env = _env;
- speedX = env.randomGenerator.NextDouble() - 0.5;
- speedY = env.randomGenerator.NextDouble() - 0.5;
- Normalize();
- }
- private void Normalize()
- {
- double length = Math.Sqrt(speedX * speedX + speedY * speedY);
- speedX = speedX / length;
- speedY = speedY / length;
- }
- public void UpdatePosition(double _maxWidth, double _maxHeight) {
- PosX += STEP * speedX;
- PosY += STEP * speedY;
- if (PosX < 0)
- {
- PosX = 0;
- }
- if (PosY < 0)
- {
- PosY = 0;
- }
- if (PosX > _maxWidth)
- {
- PosX = _maxWidth;
- }
- if (PosY > _maxHeight)
- {
- PosY = _maxHeight;
- }
- }
- internal void UpdateDirection(List<Waste> _wasteList)
- {
- // Où aller ?
- List<Waste> inZone = _wasteList.Where(x => DistanceTo(x) < x.Zone).OrderBy(x => DistanceTo(x)).ToList();
- Waste goal;
- if (load == null)
- {
- goal = inZone.FirstOrDefault();
- }
- else
- {
- goal = inZone.Where(x => x.Type == load.Type).FirstOrDefault();
- }
- // Avons-nous un but ?
- if (goal == null || busy)
- {
- // Pas de but, se déplacer aléatoirement
- if (env.randomGenerator.NextDouble() < CHANGE_DIRECTION_PROB)
- {
- speedX = env.randomGenerator.NextDouble() - 0.5;
- speedY = env.randomGenerator.NextDouble() - 0.5;
- }
- if (busy && goal == null)
- {
- busy = false;
- }
- }
- else
- {
- // Aller au but
- speedX = goal.PosX - PosX;
- speedY = goal.PosY - PosY;
- // But atteint ? Prendre ou déposer une charge
- if (DistanceTo(goal) < STEP)
- {
- if (load == null)
- {
- if (env.randomGenerator.NextDouble() < goal.ProbaToTake())
- {
- load = env.TakeWaste(goal);
- }
- }
- else
- {
- env.SetWaste(goal);
- load = null;
- }
- busy = true;
- }
- }
- Normalize();
- }
- public bool isLoaded()
- {
- return load != null;
- }
- }
- }
|