In this chapter we present an agent that acts as an intelligent manager of a shared resource: a traffic light (semaphore).
The goal is to allow entities to pass from different origins to different destinations, but only one direction at a time, in a controlled manner. This simulates an intersection where the semaphore changes direction periodically, enabling one of four possible paths.
General structure:
- An INITIAL block defines the tramos table, which contains the configuration for each direction (positions, restrooms, colors, graphics, etc.).
- Entities wait in a Restroom if their direction is not currently enabled and proceed when it becomes active.
- The semaphoreAgent.main agent:
* Switches the current semaphore to yellow and then red.
* Selects the next open direction randomly.
* Updates colors and on-screen text.
* Wakes the entities waiting for the newly opened direction.
Key learning:
The agent behaves as both a visual and operational scheduler, showing how a permanent control loop can govern multiple entity flows simply by deciding when to allow progress. This pattern is useful for exclusive zones, turn-based access, shared resources, or alternating flows.
Example
/*
Resource manager
*/
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
INITIAL tramos, [
{ tiempo: 10, color: "#009900", nInicio: "InicioN", nFin: "FinN", nCentro: "CentroN", nRestroom: "RestroomN", nSemaforo: "SemaforoN", nTexto: "TextN" },
{ tiempo: 20, color: "#FF0099", nInicio: "InicioS", nFin: "FinS", nCentro: "CentroS", nRestroom: "RestroomS", nSemaforo: "SemaforoS", nTexto: "TextS" },
{ tiempo: 30, color: "#000099", nInicio: "InicioE", nFin: "FinE", nCentro: "CentroE", nRestroom: "RestroomE", nSemaforo: "SemaforoE", nTexto: "TextE" },
{ tiempo: 40, color: "#0099FF", nInicio: "InicioO", nFin: "FinO", nCentro: "CentroO", nRestroom: "RestroomO", nSemaforo: "SemaforoO", nTexto: "TextO" }
]
POSITION {NAME:InicioN,X:368,Y:541}
POSITION {NAME:InicioS,X:393,Y:30}
POSITION {NAME:InicioE,X:756,Y:270}
POSITION {NAME:InicioO,X:41,Y:269}
POSITION {NAME:FinN,X:398,Y:540}
POSITION {NAME:FinS,X:416,Y:31}
POSITION {NAME:FinE,X:754,Y:298}
POSITION {NAME:FinO,X:40,Y:301}
POSITION {NAME:CentroN,X:381,Y:347}
POSITION {NAME:CentroS,X:393,Y:249}
POSITION {NAME:CentroE,X:447,Y:294}
POSITION {NAME:CentroO,X:325,Y:290}
Graphic {NAME:SemaforoN,TYPE:ARC,X:413,Y:346,FCOLOR:"red",RADIUS:16,START_ANGLE:0,END_ANGLE:360,CLOSE:1}
Graphic {NAME:SemaforoS,TYPE:ARC,X:363,Y:246,FCOLOR:"red",RADIUS:16,START_ANGLE:0,END_ANGLE:360,CLOSE:1}
Graphic {NAME:SemaforoE,TYPE:ARC,X:453,Y:263,FCOLOR:"red",RADIUS:16,START_ANGLE:0,END_ANGLE:360,CLOSE:1}
Graphic {NAME:SemaforoO,TYPE:ARC,X:326,Y:320,FCOLOR:"red",RADIUS:16,START_ANGLE:0,END_ANGLE:360,CLOSE:1}
Restroom {NAME:RestroomN,X:713,Y:570,on_rest:actualiza}
Restroom {NAME:RestroomS,X:713,Y:473,on_rest:actualiza}
Restroom {NAME:RestroomE,X:713,Y:521,on_rest:actualiza}
Restroom {NAME:RestroomO,X:713,Y:422,on_rest:actualiza}
Graphic {NAME:TextN,Type:TEXT,X:412,Y:347,Text:"N",font:"18px",color:#ffffff}
Graphic {NAME:TextS,Type:TEXT,X:363,Y:247,Text:"S",font:"18px",color:#ffffff}
Graphic {NAME:TextE,Type:TEXT,X:453,Y:263,Text:"E",font:"18px",color:#ffffff}
Graphic {NAME:TextO,Type:TEXT,X:326,Y:320,Text:"O",font:"18px",color:#ffffff}
Graphic {NAME:textSem,Type:TEXT,X:165,Y:381,Text:"Semaphore",font:"18px",color:#000000}
START 3000
;----------------------------- MAIN MOVEMENT PROCEDURE
procedure toSemaphore ; P$PARAM_A = origin, P$PARAM_B = destination
ADVANCE X$(tramos.P$(PARAM_A).tiempo),10 {from:X$(tramos.P$(PARAM_A).nInicio), to:X$(tramos.P$PARAM_A.nCentro)}
if (X$openSemaphore!=P$PARAM_A)
rest X$(tramos.P$(PARAM_A).nRestroom)
endif
ADVANCE 10,3 {to:X$(tramos.P$PARAM_B.nCentro)}
ADVANCE X$(tramos.P$(PARAM_B).tiempo),10 {to:X$(tramos.P$(PARAM_B).nFin)}
endprocedure
;----------------------------- SEMAPHORE AGENT
procedure semaphoreAgent.main
savevalue openSemaphore,0
while (1==1)
savevalue prevOpenSemaphore, X$openSemaphore
move {name:X$(tramos.X$(openSemaphore).nSemaforo),FCOLOR:yellow}
savevalue openSemaphore,-1
advance 50
call calculateDestination,X$prevOpenSemaphore
move {name:X$(tramos.X$(prevOpenSemaphore).nSemaforo),FCOLOR:red}
savevalue openSemaphore,P$calculateDestination
move {name:textSem,text:"Open semaphore: X$openSemaphore"}
move {name:X$(tramos.X$(openSemaphore).nSemaforo),FCOLOR:green}
wake X$(tramos.X$(openSemaphore).nRestroom)
advance 200
endwhile
endprocedure
;----------------------------- DESTINATION SELECTION PROCEDURE
procedure calculateDestination ; P$PARAM_A origin
assign tmp, (P$PARAM_A + 1 + floor(random*3)) % 4
endprocedure P$tmp
;----------------------------- UI UPDATE PROCEDURE
procedure actualiza
move {name:X$(tramos.0.nTexto),text:"R$(X$(tramos.0.nRestroom),IN)"}
move {name:X$(tramos.1.nTexto),text:"R$(X$(tramos.1.nRestroom),IN)"}
move {name:X$(tramos.2.nTexto),text:"R$(X$(tramos.2.nRestroom),IN)"}
move {name:X$(tramos.3.nTexto),text:"R$(X$(tramos.3.nRestroom),IN)"}
terminate
endprocedure
;----------------------------- ENTITY GENERATOR
GENERATE 10,5 {NAME:GEN,X:577,Y:575,ERADIO:10}
assign origin,floor(random*4)
mod {color:X$(tramos.P$origin.color)}
call calculateDestination,P$origin
call toSemaphore,P$origin,P$calculateDestination
ENDGENERATE 1
;----------------------------- AGENT STARTUP
PROCEDURE PRE_RUN
TIMEOUT semaphoreAgent.main, 0
TERMINATE_VE
ENDPROCEDURE