-4

I want a decreasing function in AnyLogic software that starts from 100 and decreases with a slope of -0.2 in time plot. When it reaches 80, it should completely reach zero. After a few days, it should start again from 85 and then start decreasing with the slope.

i created a variable usestock_boundary3 which initial values is 100 and then i have function

usestock_boundary3 -= 0.2;

if (usestock_boundary3 <= 80) {
usestock_boundary3 = 0;
} else if (usestock_boundary3 <= 0) {
usestock_boundary3 = 90;
}

and event which call this function

mczandrea
  • 463
  • 3
  • 12
  • Doesn't look like a function to me. – pjs May 02 '23 at 19:23
  • Your code is incorrect, it will never set the variable back to 90/85, because you test first whether it's smaller than 80 to set it to 0. Switch your `if` and `else if` cases. What is the logic after you set it back to 85? Is it cycling 85->80->0->85->80->0->85->... or something different? – mczandrea May 03 '23 at 06:59
  • Let's imagine a car that starts running at 100% health, but over time its health decreases, and eventually it reaches 80%. After a few days, an accident occurs, and the car's health drops to 0%. Then, it requires repair. However, even after the repair, the car's health can only be restored to 85%, not 100%. This cycle repeats with the car's health fluctuating between 100% to 80%, then dropping to 0%, and after a few days of repairing, its health is restored to 85%. – Aditya Wani May 03 '23 at 21:04
  • Can i do with the help of variable, event, and function? – Aditya Wani May 03 '23 at 21:09

1 Answers1

0

I would do this with a Dynamic Event, because it's easier to set the occurrence of the next event if it's varying. Let's call it MyDynamicEvent, then you should use create_MyDynamicEvent(double _dt, TimeUnits _units). I don't fully understand your logic, but the body of the dynamic event (or the function body you're calling there) should look something like this:

usestock_boundary3 -= 0.2;

if (usestock_boundary3 <= 0) { //car needs repair
    usestock_boundary3 = 85;
    create_MyDynamicEvent(1, HOUR); //set the time when the car should start running again
    
}
else if (usestock_boundary3 <= 80) { //accident happens
    usestock_boundary3 = 0;
    create_MyDynamicEvent(3, DAY); //set the time of repair
} 
else { //normal usage
    create_MyDynamicEvent(2, HOUR); //set the normal event cycle time
}

And additionally to initialize the event, you need to call create_MyDynamicEvent(2, HOUR) function at the On startup: section of the agent that has this Dynamic Event. This way you can control when the next event should occur based on the value of you variable usestock_boundary3.

mczandrea
  • 463
  • 3
  • 12