Is there a way to record when (and how often) Transporters get within a certain distance of each other? - anylogic

I have an AnyLogic simulation model using trucks and forklifts as agents (Transporter type), and among other things would like to identify each time one of them becomes within a certain distance of another one (example within 5 meters). I will record this count as a variable within the main space of the model. I am using path-guided navigation.
I have seen a method "agentsInRange" which will probably be able to do the trick, but am not sure where to call this from. I assume I should be able to use the AL functionality of "Min distance to obstacle" (TransporterFleet) and "Collision detection timeout" (TransporterControl)?
Thanks in advance!

Since there don't seem to be pre-built functions for this, afaik, the easiest way is to:
add an int variable to your transporter agent type counter
add an event to your transporter type checkCollision that triggers every second or so
in the event, loop across the entire population of transporters and count the number that are closer than X meters (use distanceTo(otherTransporter) and write your own custom code)
add that number to counter
Note that this might be very inefficient computationally as it is quite brute-force. But might be good enough :)

Related

AnyLogic how to set attractor choice to both free and random

Is there a way to make attractor choice both free & random at the same time?
The problem I have with either on it's own is:
When the choice is set to Free - agents are using attractors in very predictable order based on attractor creation.
When the choice is set to Random - more than one agent is using an attractor at the same time, which I don't want.
Solution I found, but don't know how to implement correctly is in the following thread:
AnyLogic Attractor weird behavior
I tried to create an agent type (instead of a class) 'myAttractor' with a boolean variable inside (occupied or not occupied attractor), but I don't know how to assign that agent type to actual attractors within a node - if that is even possible?
Maybe there are other solutions to customise the attractor choice to achieve complete randomness with only one agent per attractor?
Many thanks in advance,
Peter
That is a good question and often a problem on the animation side of things.
One option is to create a collection, simple ArrayList will do, of all the attractors
Then in the Process Modelling Block (PML) where you setup the attractor you have a function that returns an Attractor. I supply the agent here so that we can keep track of what agent is sent to which attractor so that we can put the attractor back into the available pile once the agent leaves the attractor location.
Here is the getAttractor function
It gets a random available attractor and then also saves the agent that is taking it to a map
Here is the setup for the map mapAgentPerAttractor
If you want to free the attractor you can simple call this at any point that the attractor is freed
attractorsAvailable.add(mapAgentPerAttractor.get(agent));
mapAgentPerAttractor.remove(agent);
Here is the final result as well as a comparison where we are replciating the problem you described
One can see in the bottom node there are only 8 dots available as some agents are on the same attractor...

Custom defined transporters colliding

I've created a simple transporter (think of it like a box on wheels) and I have two separate TransporterFleets that each contain one of these simple transporter agents. In the TransporterFleet blocks I specify a "Min distance to obstacle" = 0.5 meters; however when the simulation runs, the two separate transporters go right over each other from time-to-time.
Note that I have two separate TransporterFleet blocks because each transporter has certain paths that it cannot take.
What can I modify/add so that these transporters will never collide?
i had the same issue and suggestion from AL support was to increase transporters deceleration. If you have default value (1 m/s2) than it could be possible that transportesr cant break fast enough...

Measure intersection delay time

Using the Road Traffic Library, I have created a 4-way intersection that is stop-controlled and I need to measure the average delay for each approach. Currently I am using timeMeasureStart and timeMeasureEnd blocks, showing the time taken as the car enters the road/model, until it exits the intersection.
Instead, I want to measure from the time the car slows to 40km/h, until it exits the intersection. Any suggestions?
The initial speed of all cars entering the model is 60 km/hr.
Sure, there is no pre-defined way but this custom approach should work:
make sure your cars are a custom agent type, not the default cars. Lets name it My Car
add a variable myTimeBelow40 into MyCar of type double
check the car speed regularly (every 0.1 sec?!) in an event in MyCar.checkSpeed. Use the getSpeed() function. If it is below 40 KPH, you log the current time into myTimeBelow40
log the departure time of your car: your time since 40 KPH is the difference
Finally, add a statistic across your car population or log your individual car duration into main
#Benjamin thank you for pointing me in the right direction.
Here is my solution, guided by the suggestion. I'm sure it could be refined but in the end it is what worked for me and my limited knowledge of AnyLogic.
For a 4-way intersection, I wanted the delay for each approach, so I created 4 custom car agents, with each populations starting out empty.
In each agent, I had 2 variable blocks - var_Start and var_Slow, an one event block set to Timeout, Cyclic, first read at time(), and proceeding in intervals of 0.1s. In the event action, i specidied the following:
if(getSpeed(KPH) <= 40) {
var_Slow=time();
var_Slow.suspend();
}
In main I used Histogram Data, labeled as dataDelay, and a chart with the mean showing, to see the results. I had one for each intersection.
Back in the car agent, in actions on startup:
var_Start=time();
and on destroy:
if(var_Slow = 0)
main.dataDelay.add(time()-var_Start);
else
main.dataDelay.add(time()-var_Slow);
At the car source block in main, I kept the initial and prefered speed at 60, however if there were cars backed up then new cars were often initiated at a slower speed, sometimes already below 40kph, hence the if,else code on destroy.
I had everything labeled according to its corresponding approach direction, unlike the simplified version I have here.

Set NetLogo Display rate Constant

I have a NetLogo model where the number of agents changes/reduces with time. I notice the model runs faster as the agents are less(ticks increment quickly).
How can keep it constant, probably set to upperlimit.
There are two ways of doing that. One is to lower the speed using the speed slider at the top of model view. This essentially sets an upper limit.
Another, programmatic, way is to use the every (click for link) keyword. That gives you more exact control over how your model executes, but keep in mind there is a CPU overhead using that.

Implementing reinforcement learning in NetLogo (Learning in multi-agent models)

I am thinking to implement a learning strategy for different types of agents in my model. To be honest, I still do not know what kind of questions should I ask first or where to start.
I have two types of agents which I want them to learn by experience, they have a pool of actions which each has different reward based on specific situations that might happen.
I am new to reinforcement Learning methods, therefore any suggestions on what kind of questions should I ask myself is welcomed :)
Here is how I am going forward to formulate my problem:
Agents have a lifetime and they keep track of a few things that matter for them and these indicators are different for different agents, for example, one agent wants to increase A another wants B more than A.
States are points in an agent's lifetime which they
Have more than one option (I do not have a clear definition for
States as they might happen a few times or not happen at all because
Agents move around and they might never face a situation)
The reward is the an increase or decrease in an indicator that agents can get from an action in a specific State, and agent do not know what would be the gain if he chose another action.
The gain is not constant, the states are not well defined and there is no formal transition of one state into another,
For example agent can decide to share with one of the co-located agent (Action 1) or with all of the agents at the same location(Action 2) If certain conditions hold true Action A will be more rewarding for that agent, while in other conditions Action 2 will have higher reward; my problem is I did not see any example with unknown rewards since sharing in this scenario also depends on the other agent's characteristics (which affects the conditions of reward system) and in different states it will be different.
In my model there is no relationship between the action and the following state,and that makes me wonder if its ok to think about RL in this situation at all.
What I am looking to optimize here is the ability for my agents to reason about current situation in a better way and not only respond to their need which is triggered by their internal states. They have a few personalities which can define their long term goal and can affect their decision making in different situations, but I want them to remember what action in a situation helped them to increase their preferred long term goal.
In my model there is no relationship between the action and the following state,and that makes me wonder if its ok to think about RL in this situation at all.
This seems strange. What do actions do if not change state? Note that agents don't have to necessarily know how their actions will change their state. Similarly, actions could change the state imperfectly (a robots treads could skid out so it doesn't actually move when it tries to). In fact, some algorithms are specifically designed for this uncertainty.
In any case, even if the agents are moving around the state space without having any control, it can still learn the rewards for the different states. Indeed, many RL algorithms involve moving around the state space semi-randomly to figure out what the rewards are.
I do not have a clear definition for States as they might happen a few times or not happen at all because Agents move around and they might never face a situation
You might consider expanding what goes into what you consider to be a "state". For instance, the position seems like it should definitely go into the variables identifying a state. Not all states need to have rewards (although good RL algorithms typically infer a measure of goodness of neutral states).
I would recommend clearly defining the variables that determine an agent's state. For instance, the state space could be current-patch X internal-variable-value X other-agents-present. In the simplest case, the agent can observe all of the variables that make up their state. However, there are algorithms that don't require this. An agent should always be in a state, even if the state has no reward value.
Now, concerning unknown reward. That's actually totally okay. Reward can be a random variable. In that case, a simple way to apply standard RL algorithms would be to use the expected value of the variable when making decisions. If the distribution is unknown, then the algorithm could just use the mean of the rewards observed so far.
Alternatively, you could include the variables that determine the reward in the definition of the state. That way, if the reward changes, then it is literally in a different state. For example, suppose a robot is on top of a building. It needs to get to the top of the building in front of it. If it just moves forward, it falls to ground. Thus, that state has a very low reward. However, if it first places a plank that goes from one building to the other, and then moves forward, the reward changes. To represent this, we could include plank-in-place as a variable so that putting the board in place actually changes the robot's current state and the state that would result from moving forward. Thus, the reward itself has not changed; it's just in a different state.
Hopefully this helps!
UPDATE 2/7/2018: A recent upvote reminded me of the existence of this question. In the years since it was asked, I've actually dived into RL in NetLogo to a much greater extent. In particular, I've made a python extension for NetLogo, primarily to make it easier to integrate machine learning algorithms in with model. One of the demos of the extension trains a collection of agents using deep Q-learning as the model runs.