I have to make a simulator in Java, which will simulate car riding on highway. There should be 3 lanes on highway, in every lane there are cars with constant speed. On this highway, there is one agent, which has to drive through and not crash into any other car. Detailed description can be found in this paper at section 2.5 and picture 5.
This image is from mentioned paper and shows the look of highway:
My goal is to write only a simulator (and GUI), not logic of agent. Now, I would like to design an architecture of this simulator and this is where I need help.
My idea, how the agent's API can look is:
public abstract class BaseAgent {
public abstract void run()
public abstract void onCrash();
}
Agent (car) on highway should be a descendant of this class. In every step, simulator call function run()
where is the agents logic. In this function, agent can call functions like:
goLeft();
goRight();
getNearestCarInLane(int lane_no);
getMySpeed();
So, in every step, agent can decide if he stay in current lane, or if he turns left or right. And that’s all, what can agent do.
So this is agents API, but I do not know, how to design the rest of simulator. My first attempt to simulator architecture was:
class Agent — descendant of BaseAgent, can ride on highway.
class Highway — stores position of all cars on highway.
class Simulator — creates instance of agent and highway; in every step, call agent’s `run()` and monitors any car crash.
This is not a good architecture. In which class should be methods goLeft()
, goRight()
and getNearestCarInLane()
? Because these methods has to be inside of BaseAgent
class, but has to know position of every car on highway. So at the end, I had something like this:
Simulator s = new Simulator();
Highway h = new Highway();
Agent a = new Agent();
s.setAgent(a);
s.setHighway(h);
a.setHighway(h);
h.setAgent(a);
And it is terrible and ugly.
So I need a little help from smarter people here. Can somebody give me a link to book, article, whatever about simulators/architecture? Or explain my what I am doing wrong?
I am not a programmer and this project is part of an optional course at my faculty named Software engineering.
My recommendation would be to design the agent's interface with the formal notion of an intelligent agent in mind: from a simulation perspective, it is a black box that receives percepts from its environment (e.g., sensor data) and then decides on a certain action (e.g., to steer the car left or right).
Based on this definition and assuming a simple discrete step-wise simulation, your agent class could look like this:
public abstract class BaseAgent {
public AgentAction act(HighwayPerception hwyPerception);
}
where AgentAction
would be the type representing the actions an agent could decide to do in a single step (in the most simple case, this would be an enumeration with values STEER_LEFT
, STEER_RIGHT
, etc. --- for more complex problems, you could define a whole class hierarchy with AgentAction
as super class / interface). It is the job of the simulator to interpret the AgentAction
objects returned by the agent and to change the state of its environment (i.e., the Highway
object) accordingly.
The parameter HighwayPerception
, on the other hand, represents all that the agent is able to perceive at the current time step: e.g., how fast is the car (getMySpeed()
) or the distance to the next car (getNearestCarInLane(int laneNumber)
). This avoids coupling the agent directly to its environment (i.e., Highway
) --- which is important, because it separates the concerns: agents only perceive their environment an decide on actions, instead of interacting with it directly. It is, again, the job of the simulator to create the percepts for the agent, given the current state of its environment.
Finally, this design also makes it easier to control agents.
The HighwayPercept
class has to be designed so that it can only be used to read the data the agent should be able to perceive, not to affect the surrounding environment directly. In contrast, the agent in your original design has access to a Highway
object as such, and could hence try to cheat, e.g., to see
cars several miles ahead and plan its route accordingly, or to just change the positions of other cars on the highway. This should never be possible, even if you do not care much about security, because such things may also happen unintentionally and may be tricky to debug.
Depending on your requirements, your architecture may of course need to be much more sophisticated. Further information should be easy to get from literature on multi-agent simulation systems (this is a generalization of your problem, i.e. several agents can be simulated to drive on your highway). There is a lot of research going on in this field and there are several tools for multi-agent simulation that you may want to look at (such as Repast).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With