how to control cars speed using anylogic - anylogic

i have to simulate a traffic on a highway using anylogic 8 learning edition, what I want to do is the control the car speed on road basis for ex if my car move from road1 to road 2 thru CarMoveTo I want to change the speed when it enters the road2... I tried to use "on enter" and "on exit" of the CarMoveTo but with no success and I even tried to use the Car API with no success too. I think I missed where is the suitable location to write the following code:
if (getRoad().equals("Road2"))
setPreferredSpeed(0, MPH);
Any Help?????

First, I think your getRoad().equals( "Road2" ) may be problematic. getRoad returns a road object according to intelliSense, not a string. Try taking out your quotation marks.
To set the speed at a certain road, try one of the following:
1) Use a stop line and on crossing the line, call your code to set the speed. No need to figure out what road you are on, because the stop line will inherently be on the road of interest.
2) Use a road network descriptor, and call your code "On Enter Road"
If move to only applies to road2, you could also set it there. However, if your move to block gives the car an overall destination that just happens to go through road2, then this would not be the right place, as it would get called just the first time the car enters the move to block.

Related

Anylogic detect that agent stopped on conveyor

is there any easy way to detect that agent stopped on a roller conveyor because there are other agents ahead? I tried to use dynamic variable and conditional event but it is consuming too much performence. I donĀ“t want to go for dynamically checking condition (with cyclic event) because I have some bad experience with it.
Thanks!
Try this:
Create a LinkedList<YourAgentType> agentsOnConveyor.
Whenever an agent enters the conveyor, call agentsOnConveyor.addLast(agent). When it leaves, call agentsOnConveyor.removeFirst(agent).
Now you have a linked representation of the agents on the conveyor.
Last, you use the "on stopped" field in the conveyor and pass the message "upstream" through the linked list, i.e. use a for-loop from the first to the last entry and send them a msg "conveyor stopped".
You may still need to check if the individual agent is moving itself or has also stopped, but it might put you on a working path

Anylogic Message Animation

I am trying to force agents of a population to exchange messages in AnyLogic. I would like each time agent A sends a message to B the icon of the message to move from A to B. How can I implement this?
The code Emile sent you works to move an agent from one place to another one. I understand you don't want to move your two agents, but instead you want to move only a "message icon" from one to the other. For that you can create an agent (let's call it agent "Message"), create it and locate it in the agentA, and tell it (as Emile said) to move to agentB: messageAB.moveTo(agentB.getPosition()); this way you'll get the effect you want.
You could also:
use a timer to move from one place to another, or
use an event and change the position of the icon dinamically depending on how much time you have remaining on that event
use a source/delay/sink for the same as in point 2
There are basically two ways to move an agent:
Jump to agent B: Instantly appears near agent B
Move to agent A at a certain speed
For each one the code is respectively as follows:
agentA.jumpTo( agentB.getXYZ() );
agentA.moveTo( agentB );
Where agentA and agentB refer to the agents which you might call differently depending where you are in the model.

How can I simulate a border of a parking place?

I am currently working on a parking place simulation. Before entering the parking place, a car has to cross a border. To simulate that, I added a "carMoveToBorder" block, where the car moves to a stopline. Then I added a service block to simulate the time getting served by the borderService. Now that I am having a car network, I dont really know how to specify the location of the delay or the queue inside the service block. I tried specifying the location of the delay by entering the name of the stopLine but I got an error message saying: Type mismatch: cannot convert from Agent to AnimationStaticLocationProvider
Pictures are below.
Help is appreciated.
Thank you for your time.
borderServicePicture1
borderServicePicture2
why dont you try to use Delay instead of Service. One of the difference between them is that Service has embedded queue. But maybe in your case the car will never use that queue, i mean if its right in front of border but not quite there, it means its still in CarMoveToBorder block. And i guess in Delay you just dont have to set any agent location. (btw, what happens if you leave agent location blank in Service?)

Anylogic: How to conditionally close/open a valve

I am very new at Anylogic. I have a simple model, using the Fluid dynamics library: two tanks and a valve between them. The valve have to open at a rate, say X, only when the amount in the first tank, say tank_1, were twice of the amount of the second tank, say tank_2
Could you please help me with that?
Regards
You probably have more conditions on what to use with the valve depending on different things. But it's a bad idea to make something occur when then tank_1 is exactly 2 times larger than tank_2... Instead, create a boolean variable that tells you if the current situation is that tank_1 is over or below 2*tank_2. Let's call it "belowTank2". I will assume tank_1 is below 2*tank_2 in the beginning of the simulation. so belowTank2 is true.
Then you create an event that runs cyclically every second or even more often if you want and you use the following code:
if(belowTank2){
if(tank_1.amount()>2*tank_2.amount()){
valve.set_openRate(0.1);
belowTank2=false;
}
}else{
if(tank_1.amount()<2*tank_2.amount()){
valve.set_openRate(0.3);
belowTank2=true;
}
}
So this means that whenever tank_1 surpases 2*tank_2, it will trigger the rate change on the valve. And it will trigger again a change when it's below 2*tank_2

MIT-Scratch : Sequential cloning without delay

I am just starting to play with this as an educational tool for a youngster and encounter strange behavior whilst attempting to clone sprites.
I setup a global variable for position x,y in sprite_1 and clone a sprite_2 object. This object immediately copies the global x,y to local x,y and exits. Later sprite_2 renders using the stored local x,y.
sprite_1:
sprite_2:
I expect the four sprites to clone diagonally up/right on the screen according to this small reproduce-able example. Instead I appear to get four sprite_2 objects all on top of each other:
If I add a delay of 1 second onto the end of the clone(x,y) function however all is well:
As all four sprite_2 objects appear to be where the last clone was placed, I have a suspicion that the clones are not created immediately but instead created as a batch all at once, at some time and therefore are all taking the last coordinates from the globals _clone_enemy_x/y.
Is this the case? is there are way to circumvent this behavior or what is the solution?
I have 2 possible solutions to this problem:
Go to the "define clone()()" block, right click it, open up the advanced dropdown, and tick "run without screen refresh".
Get rid of the custom block all together, but use the original source for that block in the actual code.
I hope this helps!