Experimental Frameworks: A1_reactive_experiments.sci

//**************************************************************
// File: A1_reactive_experiments.sci
// Authors: Gerd Doeben-Henisch
// Version Start: May-18, 2010
//---------------------------------
// Last: Nov-26, 2012, 21:44h
//
//**********************************************************
// Diary:
//******************************************************************
// CONTENT: 
// environment_v5.sci := Framework to generate an environment
// A1_reactive_experiments.sci := Framework to start and stop experiments
// A1_reactive_19Nov2012.sci := Structure of an ANIMAT2
// A1_reactive_functions.sci := Collection of those functions which define the behavior of an ANIMAT
//
// APPLICATION
//
// (1) Generate an environment
// (2) Define an experiment
// (3) Run the experiment




//******************************************************************************
// liveR2()
//
//Running a simple experiment with a given number of RUNS

   function [ANIMAT,HISTORY,GRID,FOOD]=liveR2(ANIMAT,X,Y,GRID, RUNS,SHOW,ACTDEPTH, CLASSIF,FOODIDX, VTHRESHOLD) 
  
  if (RUNS<0) then error('RUNS is not specified'),end

  
  //SET GLOBAL VARIABLES AS START VALUES
  HISTORY=[]
  ANIMAT(1)=X
  ANIMAT(2)=Y
  ANIMAT(3)= FOODIDX //Initial Value FOODIDX
  ANIMAT(4)=0
  ANIMAT(5)= 0
  ANIMAT(6)=1  //At start time the ANIMAT is supposed to be active
  ANIMAT(7)=[]
  ANIMAT(9)=ACTDEPTH //length of oldactions
  ANIMAT(10)=FOODIDX
  ANIMAT(12)=VTHRESHOLD //Schwellwert
  ANIMAT(13)=[]
  ANIMAT(14)=[] 
  MATCHSET=[]
  ACTIONSET=[]
  
  //Set all Feedback Values of CLASSIF to '000'
  [ANIMAT]=zeroCLASSIFFEEDB(ANIMAT,CLASSIF)
  
  //Set OLDACTIONS to a predefined value
  [ANIMAT, OLDACTIONS]=oldActZero(ANIMAT,OLDACTIONS)
  
  f=1 //Index to store in which cycle food has been found
  FOOD=[] //Storage for differences
  FOLD=0
  FNEW=0
  
  
  [YMAX,XMAX]= size(GRID)
  //printf("DIMENSION OF GRID - ROWS = %d, COLUMNS = %d\n\n",ZMAX,XMAX)

  k=1
  while(k < RUNS+1),
    
   // if (SHOW==1) then
    printf("-----CYCLE %d---------------------------------------------------\n\n",k)
   // end
  
  
  // (1) Get actual position (XO,YO) from Animat 
  // Is given with argument ANIMAT(1) = rows and ANIMAT(2) = columns
  
  XO= ANIMAT(1)
  YO= ANIMAT(2)
  
  // Insert ANIMAT into GRID
  
  REPLACE=GRID(YO,XO)
  GRID(YO,XO)='*'
  
 // if (SHOW==1) then
  disp(GRID),
 // end

// Get SENSINPUT as string: PERC

 [PERC]=ainp(YO,XO,GRID,SHOW)


//Select MATCHSET for PERC and VITAL

MATCHSET=ANIMAT(13)
[ANIMAT,MATCHSET]=selectM(ANIMAT, CLASSIF,PERC,MATCHSET,SHOW)

// Generate candidates for ACTion out of MATCHSET
// CAND are the classifiers selected
// IDXM is a randomly generated index into CAND
// Select ACTion

VITAL=ANIMAT(6)
if VITAL==0 then  [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDfood(MATCHSET,SHOW)
else [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDpath(MATCHSET,SHOW)
end


//Delete ANIMAT in GRID

GRID(YO,XO)=REPLACE

//Generate new move
[YN, XN]=aout(ACT,SHOW, YO, XO)
ANIMAT(1)=XN, ANIMAT(2)=YN

if (SHOW==1) then
  printf("liveR2: NEW POSITION PROPOSED=(YN,XN)= (%d, %d)\n\n",YN,XN),
  end


  // Decode the content of new position
  
  [CELLVALUE]=decode(YN,XN,GRID,SHOW)
  
   if (SHOW==5) then,
     printf("CONTENT OF NEW POSITION= %s\n\n",CELLVALUE),
     end
     
     
     

// Impact of perception onto ENERGY

[ANIMAT]=impact(CELLVALUE,ANIMAT,SHOW)

printf("ENERGY = %d, VITAL = %d\n",ANIMAT(3),ANIMAT(6))

// Compute Feedback of REW for last action ACT

[ANIMAT,OLDACTIONS]=manageOldActs(IDXM,CAND,OLDACTIONS,ACTIONSET,ANIMAT,FOODIDX,MAXREWARD)

// UPDATE HISTORY

[HISTORY]=updateHIST(HISTORY,k, CELLVALUE,XN,YN,ANIMAT)  

  //Update while loop
  
  
  k=k+1,
  
end

disp(HISTORY)

[FOOD]=food(HISTORY,RUNS)

//if (SHOW==1) then
  printf("ENERGY = %d,\n MEAN NUMNER OF ACTIONS = %f\n DENSITY OF FOODDETECTION = %f \n",ANIMAT(3),FOOD(4), FOOD(3))
 // end


endfunction


//******************************************************************************
// liveR3()
//
//Running a simple experiment with a a defined end
// If the 'end' occurs then the experiment stops
// Here we use as an 'end' criterium the eating of food

   function [ANIMAT,HISTORY,GRID,FOOD,k]=liveR3(ANIMAT,X,Y,GRID,SHOW,ACTDEPTH, CLASSIF,FOODIDX, VTHRESHOLD) 
  

  
  //SET GLOBAL VARIABLES AS START VALUES
  HISTORY=[]
  ANIMAT(1)=X
  ANIMAT(2)=Y
  ANIMAT(3)= FOODIDX //Initial Value FOODIDX
  ANIMAT(4)=0
  ANIMAT(5)= 0
  ANIMAT(6)=1  //At start time the ANIMAT is supposed to be active
  ANIMAT(7)=[]
  ANIMAT(9)=ACTDEPTH //length of oldactions
  ANIMAT(10)=FOODIDX
  ANIMAT(12)=VTHRESHOLD //Schwellwert
  ANIMAT(13)=[]
  ANIMAT(14)=[] 
  MATCHSET=[]
  ACTIONSET=[]
  
  //Set all Feedback Values of CLASSIF to '000'
  [ANIMAT]=zeroCLASSIFFEEDB(ANIMAT,CLASSIF)
  
  //Set OLDACTIONS to a predefined value
  [ANIMAT, OLDACTIONS]=oldActZero(ANIMAT,OLDACTIONS)
  
  f=1 //Index to store in which cycle food has been found
  FOOD=[] //Storage for differences
  FOLD=0
  FNEW=0
  
  
  [YMAX,XMAX]= size(GRID)
  //printf("DIMENSION OF GRID - ROWS = %d, COLUMNS = %d\n\n",ZMAX,XMAX)

  ENDE=0
  k=1
  while(ENDE==0),
    
   // if (SHOW==1) then
    printf("-----CYCLE %d---------------------------------------------------\n\n",k)
   // end
  
  
  // (1) Get actual position (XO,YO) from Animat 
  // Is given with argument ANIMAT(1) = rows and ANIMAT(2) = columns
  
  XO= ANIMAT(1)
  YO= ANIMAT(2)
  
  // Insert ANIMAT into GRID
  
  REPLACE=GRID(YO,XO)
  GRID(YO,XO)='*'
  
 // if (SHOW==1) then
  disp(GRID),
 // end

// Get SENSINPUT as string: PERC

 [PERC]=ainp(YO,XO,GRID,SHOW)


//Select MATCHSET for PERC and VITAL

MATCHSET=ANIMAT(13)
[ANIMAT,MATCHSET]=selectM(ANIMAT, CLASSIF,PERC,MATCHSET,SHOW)

// Generate candidates for ACTion out of MATCHSET
// CAND are the classifiers selected
// IDXM is a randomly generated index into CAND
// Select ACTion

VITAL=ANIMAT(6)
if VITAL==0 then  [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDfood(MATCHSET,SHOW)
else [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDpath(MATCHSET,SHOW)
end


//Delete ANIMAT in GRID

GRID(YO,XO)=REPLACE

//Generate new move
[YN, XN]=aout(ACT,SHOW, YO, XO)
ANIMAT(1)=XN, ANIMAT(2)=YN

if (SHOW==1) then
  printf("liveR2: NEW POSITION PROPOSED=(YN,XN)= (%d, %d)\n\n",YN,XN),
  end


  // Decode the content of new position
  
  [CELLVALUE]=decode(YN,XN,GRID,SHOW)
  
   if (SHOW==5) then,
     printf("CONTENT OF NEW POSITION= %s\n\n",CELLVALUE),
     end
     
     
     

// Impact of perception onto ENERGY

[ANIMAT]=impact(CELLVALUE,ANIMAT,SHOW)

if ANIMAT(15)==1 then ENDE=1
end

printf("ENERGY = %d, VITAL = %d\n",ANIMAT(3),ANIMAT(6))

// Compute Feedback of REW for last action ACT

[ANIMAT,OLDACTIONS]=manageOldActs(IDXM,CAND,OLDACTIONS,ACTIONSET,ANIMAT,FOODIDX,MAXREWARD,SHOW)

// UPDATE HISTORY

[HISTORY]=updateHIST(HISTORY,k, CELLVALUE,XN,YN,ANIMAT)  

  //Update while loop
  
  
  k=k+1,
  
end

disp(HISTORY)

[FOOD]=food(HISTORY,k)

//if (SHOW==1) then
  printf("ENERGY = %d,\n MEAN NUMNER OF ACTIONS = %f\n DENSITY OF FOODDETECTION = %f \n",ANIMAT(3),FOOD(4), FOOD(3))
 // end


endfunction



//******************************************************************************
// liveR4()
//
//Running a simple experiment with a a defined end
// If the 'end' occurs then the experiment stops
// Here we use as an 'end' criterium the eating of food
// This version is for repetitions!

   function [ANIMAT,HISTORY,GRID,FOOD]=liveR4(ANIMAT,X,Y,GRID,SHOW,ACTDEPTH, CLASSIF,FOODIDX, VTHRESHOLD) 
  

  
  //SET GLOBAL VARIABLES AS START VALUES
  HISTORY=[]
  ANIMAT(1)=X
  ANIMAT(2)=Y
  ANIMAT(3)= FOODIDX //Initial Value FOODIDX
  ANIMAT(4)=0
  ANIMAT(5)= 0
  ANIMAT(6)=1  //At start time the ANIMAT is supposed to be active
  ANIMAT(7)=[]
  ANIMAT(9)=ACTDEPTH //length of oldactions
  ANIMAT(10)=FOODIDX
  ANIMAT(12)=VTHRESHOLD //Schwellwert
  ANIMAT(13)=[]
  ANIMAT(14)=[] 
  MATCHSET=[]
  
  if MATCHSET==[] then

    //Set OLDACTIONS to a predefined value
  [ANIMAT, OLDACTIONS]=oldActZero(ANIMAT,OLDACTIONS)
    end
  
  f=1 //Index to store in which cycle food has been found
  FOOD=[] //Storage for differences
  FOLD=0
  FNEW=0
  
  
  [YMAX,XMAX]= size(GRID)
  //printf("DIMENSION OF GRID - ROWS = %d, COLUMNS = %d\n\n",ZMAX,XMAX)

  ENDE=0
  k=1
  while(ENDE==0),
    
   // if (SHOW==1) then
    printf("-----CYCLE %d---------------------------------------------------\n\n",k)
   // end
  
  
  // (1) Get actual position (XO,YO) from Animat 
  // Is given with argument ANIMAT(1) = rows and ANIMAT(2) = columns
  
  XO= ANIMAT(1)
  YO= ANIMAT(2)
  
  // Insert ANIMAT into GRID
  
  REPLACE=GRID(YO,XO)
  GRID(YO,XO)='*'
  
 // if (SHOW==1) then
  disp(GRID),
 // end

// Get SENSINPUT as string: PERC

 [PERC]=ainp(YO,XO,GRID,SHOW)


//Select MATCHSET for PERC and VITAL

MATCHSET=ANIMAT(13)
[ANIMAT,MATCHSET]=selectM(ANIMAT, CLASSIF,PERC,MATCHSET,SHOW)

// Generate candidates for ACTion out of MATCHSET
// CAND are the classifiers selected
// IDXM is a randomly generated index into CAND
// Select ACTion

VITAL=ANIMAT(6)
if VITAL==0 then  [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDfood(MATCHSET,SHOW)
else [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDpath(MATCHSET,SHOW)
end


//Delete ANIMAT in GRID

GRID(YO,XO)=REPLACE

//Generate new move
[YN, XN]=aout(ACT,SHOW, YO, XO)
ANIMAT(1)=XN, ANIMAT(2)=YN

if (SHOW==1) then
  printf("liveR2: NEW POSITION PROPOSED=(YN,XN)= (%d, %d)\n\n",YN,XN),
  end


  // Decode the content of new position
  
  [CELLVALUE]=decode(YN,XN,GRID,SHOW)
  
   if (SHOW==5) then,
     printf("CONTENT OF NEW POSITION= %s\n\n",CELLVALUE),
     end
     
     
     

// Impact of perception onto ENERGY

[ANIMAT]=impact(CELLVALUE,ANIMAT,SHOW)

if ANIMAT(15)==1 then ENDE=1
end

printf("ENERGY = %d, VITAL = %d\n",ANIMAT(3),ANIMAT(6))

// Compute Feedback of REW for last action ACT

[ANIMAT,OLDACTIONS]=manageOldActs(IDXM,CAND,OLDACTIONS,ACTIONSET,ANIMAT,FOODIDX,MAXREWARD,SHOW)

// UPDATE HISTORY

[HISTORY]=updateHIST(HISTORY,k, CELLVALUE,XN,YN,ANIMAT)  

  //Update while loop
  
  
  k=k+1,
  
end

disp(HISTORY)

[FOOD]=food(HISTORY,RUNS)

//if (SHOW==1) then
  printf("ENERGY = %d,\n MEAN NUMNER OF ACTIONS = %f\n DENSITY OF FOODDETECTION = %f \n",ANIMAT(3),FOOD(4), FOOD(3))
 // end


endfunction




//******************************************************************************
// liveR4()
//
//Running a simple experiment with a a defined end
// If the 'end' occurs then the experiment stops
// Here we use as an 'end' criterium the eating of food
// This version is for repetitions!

   function [ANIMAT,HISTORY,GRID,FOOD,k]=liveR5(ANIMAT,X,Y,GRID,SHOW,ACTDEPTH, CLASSIF,FOODIDX, VTHRESHOLD) 
  

  
  //SET GLOBAL VARIABLES AS START VALUES
  HISTORY=[]
  ANIMAT(1)=X
  ANIMAT(2)=Y
  ANIMAT(3)= FOODIDX //Initial Value FOODIDX
  ANIMAT(4)=0
  ANIMAT(5)= 0
  ANIMAT(6)=1  //At start time the ANIMAT is supposed to be active
  ANIMAT(7)=[]
  ANIMAT(9)=ACTDEPTH //length of oldactions
  ANIMAT(10)=FOODIDX
  ANIMAT(12)=VTHRESHOLD //Schwellwert
  ANIMAT(13)=[]
  ANIMAT(14)=[] 
  MATCHSET=[]
  
  if MATCHSET==[] then

    //Set OLDACTIONS to a predefined value
  [ANIMAT, OLDACTIONS]=oldActZero(ANIMAT,OLDACTIONS)
    end
  
  f=1 //Index to store in which cycle food has been found
  FOOD=[] //Storage for differences
  FOLD=0
  FNEW=0
  
  
  [YMAX,XMAX]= size(GRID)
  //printf("DIMENSION OF GRID - ROWS = %d, COLUMNS = %d\n\n",ZMAX,XMAX)

  ENDE=0
  k=1
  while(ENDE==0),
    
   // if (SHOW==1) then
    printf("-----CYCLE %d---------------------------------------------------\n\n",k)
   // end
  
  
  // (1) Get actual position (XO,YO) from Animat 
  // Is given with argument ANIMAT(1) = rows and ANIMAT(2) = columns
  
  XO= ANIMAT(1)
  YO= ANIMAT(2)
  
  // Insert ANIMAT into GRID
  
  REPLACE=GRID(YO,XO)
  GRID(YO,XO)='*'
  
 // if (SHOW==1) then
  disp(GRID),
 // end

// Get SENSINPUT as string: PERC

 [PERC]=ainp(YO,XO,GRID,SHOW)


//Select MATCHSET for PERC and VITAL

MATCHSET=ANIMAT(13)
[ANIMAT,MATCHSET]=selectM(ANIMAT, CLASSIF,PERC,MATCHSET,SHOW)

// Generate candidates for ACTion out of MATCHSET
// CAND are the classifiers selected
// IDXM is a randomly generated index into CAND
// Select ACTion

VITAL=ANIMAT(6)
if VITAL==0 then  [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDfood(MATCHSET,SHOW)
else [IDXM,CAND, MATCHSET,ACTIONSET,ACT]=makeCANDpath(MATCHSET,SHOW)
end


//Delete ANIMAT in GRID

GRID(YO,XO)=REPLACE

//Generate new move
[YN, XN]=aout(ACT,SHOW, YO, XO)
ANIMAT(1)=XN, ANIMAT(2)=YN

if (SHOW==1) then
  printf("liveR2: NEW POSITION PROPOSED=(YN,XN)= (%d, %d)\n\n",YN,XN),
  end


  // Decode the content of new position
  
  [CELLVALUE]=decode(YN,XN,GRID,SHOW)
  
   if (SHOW==5) then,
     printf("CONTENT OF NEW POSITION= %s\n\n",CELLVALUE),
     end
     
     
     

// Impact of perception onto ENERGY

[ANIMAT]=impact(CELLVALUE,ANIMAT,SHOW)

if ANIMAT(15)==1 then ENDE=1
end

printf("ENERGY = %d, VITAL = %d\n",ANIMAT(3),ANIMAT(6))

// Compute Feedback of REW for last action ACT

[ANIMAT,OLDACTIONS]=manageOldActs(IDXM,CAND,OLDACTIONS,ACTIONSET,ANIMAT,FOODIDX,MAXREWARD,SHOW)

// UPDATE HISTORY

[HISTORY]=updateHIST(HISTORY,k, CELLVALUE,XN,YN,ANIMAT)  

  //Update while loop
  
  
  k=k+1,
  
end

disp(HISTORY)

[FOOD]=food(HISTORY,k)

//if (SHOW==1) then
  printf("ENERGY = %d,\n MEAN NUMNER OF ACTIONS = %f\n DENSITY OF FOODDETECTION = %f \n",ANIMAT(3),FOOD(4), FOOD(3))
 // end


endfunction



Gerd Doeben-Henisch 2013-01-14