Rehydration

Updated: 2026-01-27

An advanced and complex concept as the system grows is rehydration.

Rehydration means loading the system state not from the beginning, but from a later point in time, rebuilding the simulation exactly as it was at that instant.
In the logistics example this is fundamental: it is not the same to “start simulating packages” as it is to load the real state from an ERP and continue the simulation from there.

In this approach, the modeler becomes responsible for storing everything they want to be able to restore later.

 

This process implies solving three deep problems:

 

  1. The global simulated time (AC1) must advance to the saved instant.
  2. Entities must reschedule their internal times, which must always be later than the new AC1.
  3. The way of modeling changes radically: it is no longer enough to describe processes; now you must reconstruct them.

At this stage, more than modeling, we are programming. Rehydration is a technical problem, not a declarative one.

In the example:

So suppose what we have stored is that 4 trucks were looping around a circuit with 4 positions. The last saved data are:

  • System time: 105
  • Truck 1: it went through phase F3 and would arrive at its destination at 110
  • Truck 2: it went through phase F1 and would arrive at its destination at 108
  • Truck 3: it went through phase F3 and would arrive at its destination at 109
  • Truck 4: it went through phase F2 and would arrive at its destination at 109

To rebuild (rehydrate) this state we need to solve the three problems above.

1. Advance global time

The FORWARD_AC1 block allows moving the simulated clock forward.
Typically you place it as the first instruction of PRE_RUN, so the model starts directly at the desired instant.

2. Reschedule entity times

Once AC1 has advanced, all entities must have times after the new AC1.
This is achieved with UPDATE, which reschedules the end of an ADVANCE. In this case, we also provide the start time so that visualization begins where it left off.

The only condition is critical:

UPDATE must be executed by a VE when the entity is already inside an ADVANCE.
That is why rehydration requires a small temporal choreography: first we put it into the corresponding queue and then we update its times through an immediate TIMEOUT.

3. Rebuild the program point

This is the most complex problem.
If there were a GOTO, it would be trivial: we would jump to the exact point where each entity was.
But it does not exist, and it must not exist.

The correct solution is:

Create a local finite-state machine (FSM) for each entity.

The FSM state represents which point in the program the entity was at when the system was saved.
The model logic is rewritten so that each entity advances according to its state.

This turns the program flow into an explicit automaton, perfectly restorable.

 

4. The key detail: every entity always ends up in a queue

In a DES engine, the “last state” of an entity is always one of these:

  • The event chain, if it is in an ADVANCE.
  • A resource queue, if it is in a SEIZE.
  • An explicit queue, if it is waiting.

Therefore:

 

The FSM states must correspond to these queues.

 

We do not need to reconstruct internal queues or manipulate hidden structures:
it is enough to reinsert entities at the correct program point, and the engine rebuilds the rest automatically.

 

Example

SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}

POSITION {NAME:Pos1,X:368,Y:429}
POSITION {NAME:Pos2,X:634,Y:452}
POSITION {NAME:Pos3,X:592,Y:164}
POSITION {NAME:Pos4,X:343,Y:217}

initial fsm_camion_logic, {
    STATES: [
        "F1", "F2", "F3", "F4"
    ],

    TRANSITIONS: [
        {FROM:"F1", INPUT:"tick", TO:"F2"},
        {FROM:"F2", INPUT:"tick", TO:"F3"},
        {FROM:"F3", INPUT:"tick", TO:"F4"},
        {FROM:"F4", INPUT:"tick", TO:"F1"}
    ],

    INITIAL: "F1"
}

FSM {NAME:FSM_CAMION, LOCAL:1, LOGIC:V$(fsm_camion_logic)}
FACILITY {NAME:Fac1,X:633,Y:385}
Graphic {NAME:Text1,Type:TEXT,X:324,Y:100}

START 100
;-------------------------------
PROCEDURE PRE_RUN 
  FORWARD_AC1 105
    assign params,{status:"F2",tiempoIni:90,tiempo:110,matricula:"1111"}
	new GEN1,0,V$params
    assign params,{status:"F3",tiempoIni:88,tiempo:108,matricula:"2222"}
	new GEN1,0,V$params
    assign params,{status:"F2",tiempoIni:89,tiempo:109,matricula:"3333"}
	new GEN1,0,V$params
    assign params,{status:"F2",tiempoIni:104,tiempo:124,matricula:"4444"}
	new GEN1,0,V$params
	TERMINATE_VE 
ENDPROCEDURE

;-------------------------------
PROCEDURE camion_update
	; PARAM_A -> Numero entidad
	; PARAM_B -> tiempo final
	; PARAM_C -> tiempo inicial
	move {name:Text1,text:"camion_update P$(FSM_CAMION,P$PARAM_A) P$PARAM_C"}
    if (D$(IN_ADVANCE,P$PARAM_A)==1)
    	update P$PARAM_A, P$PARAM_B, P$PARAM_C
    endif
    TERMINATE_VE
endprocedure 1
;-------------------------------

GENERATE 0,0,0,0 {NAME:GEN1,X:62,Y:396}
    assign params,V$PARAM_A
    assign FSM_CAMION,"P$(params.status)"
    mod {subtitle:"P$(params.matricula)"}
    ; debe ser una VE y que se ejecute una vez hayan arrancado los advance.
    timeout camion_update,0,D$N,P$(params.tiempo),P$(params.tiempoIni)
	
   ; move {name:Text1,text:"Camión inicializado P$PARAM_C R$(FSM_CAMION,STATE) P$FSM_CAMION"}
	while (1==1)
    	STATE FSM_CAMION,"tick"
        
    	switch "R$(FSM_CAMION,STATE)"
	    case ==,"F1"
	        advance 20 {from:Pos4, to:Pos1}
        endcase
	    case ==,"F2"
        	 advance 20 {from:Pos1, to:Pos2}
        endcase
	    case ==,"F3"
            seize Fac1
  	        advance 40 {from:Pos2, to:Pos3}
            release Fac1
        endcase
	    case ==,"F4"
	        advance 20 {from:Pos3, to:Pos4}
        endcase

        endswitch
    endwhile

endgenerate 1
;-------------------------------