What is the best architecture for this simulator?

I need to make a Java simulator that will simulate driving a car on a highway. There should be 3 lanes on the highway, in each lane - cars with a constant speed. There is one agent on this highway who must drive and not crash into any other car. A detailed description can be found in this article in section 2.5 and figure 5.

This image is from the mentioned article and shows the view of the highway:

enter image description here

My goal is to write only a simulator (and GUI), not an agent logic. Now I would like to design the architecture of this simulator, and here I need help.

My idea is what the agent API looks like:

public abstract class BaseAgent { public abstract void run() public abstract void onCrash(); } 
The agent (car) on the highway must be a descendant of this class. At each step, the function of calling the simulator run() , where the agent logic is located. In this function, an agent can call functions such as:
 goLeft(); goRight(); getNearestCarInLane(int lane_no); getMySpeed(); 

So, at each step, the agent can decide whether he will remain in the current lane, or if he turns left or right. And all the agent can do.

So these are API agents, but I don’t know how to develop other simulators. My first attempt at modeling a simulator 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. What class should goLeft() , goRight() and getNearestCarInLane() methods be? Because these methods must be inside the BaseAgent class, but must know the position of each car on the highway. So in 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's awful and ugly.

So I need a little help from smart people here. Can someone give me a link to a book, article, regardless of simulators / architecture? Or explain what I'm doing wrong?

I am not a programmer, and this project is part of an additional course at my faculty of software development.

+6
source share
2 answers

My recommendation would be to develop an agent interface with the formal concept of an intelligent agent in mind : from the perspective of modeling, this is a black box that receives perception from its environment (for example, sensor data) and then decides on a specific action (for example, to drive a car left or right).

Based on this definition and assuming simple discrete step-by-step modeling , your agent class might look like this:

 public abstract class BaseAgent { public AgentAction act(HighwayPerception hwyPerception); } 

where AgentAction will be a type representing actions that the agent can decide to take in one step (in the simplest case, it will be an enumeration with the values STEER_LEFT , STEER_RIGHT , etc. - for more complex problems, you can define a whole class hierarchy with AgentAction as a superclass / interface). The simulator's task is to interpret the AgentAction objects returned by the agent and accordingly change the state of its environment (i.e., the Highway object).

The HighwayPerception parameter, on the other hand, represents everything that the agent is able to perceive at the current time step: for example, how fast the car ( getMySpeed() ) or the distance to the next car ( getNearestCarInLane(int laneNumber) ). This avoids binding the agent directly to its environment (i.e., Highway ) --- which is important because it shares the problems : agents perceive only their environment as a solution to actions, and not interact with it directly. This is again the task of the simulator to create perception for the agent, given the current state of his environment.

Finally, this design also simplifies agent management . The HighwayPercept class must be designed so that it can only be used to read data that the agent must perceive, and not to directly affect the environment. On the contrary, the agent in your original project has access to the Highway object as such and may therefore try to deceive , for example, to see cars several miles ahead and plan your route accordingly, or simply change the position of other cars on the highway. This should never be possible , even if you do not care about security, because such things can also happen unintentionally and can be difficult to debug.

Depending on your requirements, your architecture can, of course, be much more complex . Additional information should be easily accessible from the literature on multi-agent simulation systems (this is a generalization of your problem, i.e. several agents can be simulated to drive along your highway). There is a lot of research in this area, and there are several tools for modeling several agents that you might want to see (for example, Repast ).

+6
source

Personally, I would encapsulate the Highway so that it is contained in the Simulator. If there are more than 1 Highway in the simulator, I would assume that the Agent will be located at any of the Highway objects, so the agent can belong to the Simulator and is connected to the Highway. But the simulator must be a Facade template, so all you have to do is create a Simulator and possibly optionally pass it the configuration so you don't have to deal with them. Each agent must have a run () method, and there must be a thread in the simulator that calls run () for each agent that it contains, or you can have a manual simulation step that calls run () once for each agent (for example step by step simulator).

0
source

Source: https://habr.com/ru/post/898968/


All Articles