zsc2_environment

//**************************************************************
// File: zcs_environment_preparation_27may10.sce
// Authors: Gerd Doeben-Henisch
// Version Start: May-18, 2010
//---------------------------------
// Update May-19-2010-1459
// Update May-19-10-2343
// Update May-20-10-0637
// Update May-27-10-1833
//**********************************************************
// diary('H:\fh\scilab\histories\Date.txt')
//diary('/home/gerd/public_html/uffmm/science-technology/single/themes/computer-science/personal-sites/doeben-henisch/KNOW/GBBLT/SCILAB/HISTORIES/May20-10-1032.txt')
//******************************************************************
// CONTENT: 
// Code necessary to provide an environment
//
//***************************************************


//**********************************
// GRID 'WOOD1' 
//
// Y = r=1...15, X = c=1...55
// The Y-axis is from above (north) to bottom (south), the X-axis is from left (west) to right (east)
// '.' := No Food; encoded '00'
// 'O' := Object (Rock); encoded '10'
// Attention: scilab assumes GRID(Y,X) columns first and then rows!


//********************************************************************
// DYNAMIC GRID GENERATION
// Using the wood1-structure as building blocks
//
// YYMAX := Max number of rows multiplied by 5
// XMAX := Max number of columnsXNtiplied by 5


function[GRID]=gridgen(YYMAX,XXMAX)
  
  if (YYMAX < 1) | (XXMAX < 1) then printf("gridgen:ERROR WITH YYMAX, XXMAX\n\n"), end
  
  for k=1:5:5*YYMAX
  i=k
    for j=1:5*XXMAX, GRID(i+0,j)='.',end
  i=k+1
  for j=1:5:5*XXMAX, 
    GRID(i,j+0)='.'
    GRID(i,j+1)='O'
    GRID(i,j+2)='O'
    GRID(i,j+3)='F'
    GRID(i,j+4)='.'
  end
  i=k+2
  for j=1:5:5*XXMAX, 
    GRID(i,j+0)='.'
    GRID(i,j+1)='O'
    GRID(i,j+2)='O'
    GRID(i,j+3)='O'
    GRID(i,j+4)='.'
  end
  
  i=k+3
  for j=1:5:5*XXMAX, 
    GRID(i,j+0)='.'
    GRID(i,j+1)='O'
    GRID(i,j+2)='O'
    GRID(i,j+3)='O'
    GRID(i,j+4)='.'
  end
  i=k+4
  for j=1:5*XXMAX, GRID(i+0,j)='.',end
  end
  
  disp(GRID)
  
endfunction

//********************************************************************
// DYNAMIC GRID GENERATION
// Using theextended  wood1-structure as building block
//
// Allows less food cells in the world
//
// YYMAX := Max number of rows multiplied by 5
// XMAX := Max number of columnsXNtiplied by 5
// S := Inverse scaling to lower the density of food


function [GRID]=gridgen2(YYMAX,XXMAX,S)
  
  if (YYMAX < 1) | (XXMAX < 1) then printf("gridgen:ERROR WITH YYMAX, XXMAX\n\n"), end
  
  r=0
  for k=1:5:5*YYMAX
  i=k
    for j=1:5*XXMAX, GRID(i+0,j)='.',end
  i=k+1
  for j=1:5:5*XXMAX, 
    GRID(i,j+0)='.'
    GRID(i,j+1)='O'
    GRID(i,j+2)='O'
    
    if (S>0) & (r==S) then  GRID(i,j+3)='F', r=0, else  GRID(i,j+3)='O', r=r+1,end
    
    GRID(i,j+4)='.'
  end
  i=k+2
  for j=1:5:5*XXMAX, 
    GRID(i,j+0)='.'
    GRID(i,j+1)='O'
    GRID(i,j+2)='O'
    GRID(i,j+3)='O'
    GRID(i,j+4)='.'
  end
  
  i=k+3
  for j=1:5:5*XXMAX, 
    GRID(i,j+0)='.'
    GRID(i,j+1)='O'
    GRID(i,j+2)='O'
    GRID(i,j+3)='O'
    GRID(i,j+4)='.'
  end
  i=k+4
  for j=1:5*XXMAX, GRID(i+0,j)='.',end
  end
  
   
  disp(GRID)
 endfunction
 
 
//********************************************************************
// TESTGRIDS WITH OBJECTS
// 
//

GD3O2F1=['.' '.' '.'; 'O' 'O' '.'; '.' 'F' '.' ]

GD3O2F0=['.' '.' '.'; 'O' 'O' '.'; '.' '.' '.' ]


GD3O2F12=['.' '.' '.'; '.' 'O' 'O'; '.' 'F' '.' ]

GD3O2F13=['.' '.' '.'; '.' 'O' '.'; '.' 'F' '.' ]
   
//*************************************************
// AUTOMATIC GENERATION OF TRANSITION TABLE
// Takes every cell of a given GRID and computes the transition probabilities
// with regard to every possible direction 1 ... 8
//


function [TGRID]=transprob(GRID)
  
[r,c]=size(GRID)
TGRID=zeros()
N=r*c

MAX=9
Y=0
  while(Y<N)
    for y=1:r,
      
      for x=1:c, Y=Y+1
        
      
     // printf("\n---------------Y = %d----------------------\n\n",Y)
   
      m=1
      
      //Catch wrong indices and evaluate cases
        
        if  (y-1)<1 then m=m+1, elseif GRID(y-1, x) == 'O' then TGRID(Y,(y-1)*c+x)=0, m=m+1,else  TGRID(Y,(y-1-1)*c+x)=1/MAX, end
        if  (y-1)<1|(x+1)>c then m=m+1,elseif GRID(y-1, x+1) == 'O' then  TGRID(Y,(y-1)*c+x)=0,m=m+1,else  TGRID(Y,(y-1-1)*c+x+1)=1/MAX, end
        if  (x+1)>c then m=m+1,elseif GRID(y, x+1) == 'O' then  TGRID(Y,(y-1)*c+x)=0,m=m+1,else  TGRID(Y,(y-1)*c+x+1)=1/MAX, end
        if  (y+1)>r|(x+1)>c then m=m+1,elseif GRID(y+1, x+1) == 'O' then  TGRID(Y,(y-1)*c+x)=0,m=m+1,else  TGRID(Y,(y-1+1)*c+x+1)=1/MAX, end
        if  (y+1)>r then m=m+1,elseif GRID(y+1, x) == 'O' then  TGRID(Y,(y-1)*c+x)=0,m=m+1,else  TGRID(Y,(y-1+1)*c+x)=1/MAX,  end
        if  (y+1)>r|(x-1)<1 then m=m+1,elseif GRID(y+1, x-1) == 'O' then  TGRID(Y,(y-1)*c+x)=0,m=m+1,else  TGRID(Y,(y-1+1)*c+x-1)=1/MAX, end
        if   (x-1)<1 then m=m+1,elseif GRID(y, x-1) == 'O' then  TGRID(Y,(y-1)*c+x)=0,m=m+1,else  TGRID(Y,(y-1)*c+x-1)=1/MAX, end
        if  (y-1)<1|(x-1)<1 then m=m+1,elseif GRID(y-1, x-1) == 'O' then  TGRID(Y,(y-1)*c+x)=0,m=m+1,else  TGRID(Y,(y-1-1)*c+x-1)=1/MAX, end
        
        if GRID(y, x)=='O' then  for z=1:N,TGRID(Y,z)=0,end
            else TGRID(Y,Y)=m/MAX, end
          
     
    
    end //x
  end //y
  end //Y
 
endfunction

//*************************************************
// AUTOMATIC GENERATION OF TRANSITION TABLE
// Takes every cell of a given GRID and computes the transition probabilities
// with regard to every possible direction 1 ... 8
//


function [TGRID]=transprob_tst(GRID)
  
[r,c]=size(GRID)
TGRID=zeros()
N=r*c

MAX=9
Y=0
  while(Y<N)
    for y=1:r,
      
      for x=1:c, Y=Y+1
        
      
      printf("\n---------------Y = %d----------------------\n\n",Y)
   
      m=1
      
      //Catch wrong indices and evaluate cases
        
        if  (y-1)<1 then printf(" __BORDER__"), m=m+1, elseif GRID(y-1, x) == 'O' then printf(" __OBJECT__"),TGRID(Y,(y-1)*c+x)=0, m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1-1)*c+x)=1/MAX, printf("(y,x) = (%d,%d) with m = %d TG(Y,Y) = (%d,%d) = %f\n",y,x,m,Y,(y-1-1)*c+x, TGRID(Y,(y-1-1)*c+x)), end
        if  (y-1)<1|(x+1)>c then printf(" __BORDER__"), m=m+1,elseif GRID(y-1, x+1) == 'O' then printf(" __OBJECT__"), TGRID(Y,(y-1)*c+x)=0,m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1-1)*c+x+1)=1/MAX, printf("(y,x) = (%d,%d) with m = %d TG(Y,Y) = (%d,%d) = %f\n",y,x,m,Y,(y-1-1)*c+x+1,TGRID(Y,(y-1-1)*c+x+1)), end
        if  (x+1)>c then printf(" __BORDER__"), m=m+1,elseif GRID(y, x+1) == 'O' then printf(" __OBJECT__"), TGRID(Y,(y-1)*c+x)=0,m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1)*c+x+1)=1/MAX, printf("(y,x) = (%d,%d) with m = %d TG(Y,Y) = (%d,%d) = %f\n",y,x,m,Y,(y-1)*c+x+1,TGRID(Y,(y-1)*c+x+1)), end
        if  (y+1)>r|(x+1)>c then printf(" __BORDER__"), m=m+1,elseif GRID(y+1, x+1) == 'O' then printf(" __OBJECT__"), TGRID(Y,(y-1)*c+x)=0,m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1+1)*c+x+1)=1/MAX, printf("(y,x) = (%d,%d) with m = %d TG(Y,Y)  = (%d,%d) = %f\n",y,x,m,Y,(y-1+1)*c+x+1,TGRID(Y,(y-1+1)*c+x+1)), end
        if  (y+1)>r then printf(" __BORDER__"), m=m+1,elseif GRID(y+1, x) == 'O' then printf(" __OBJECT__"), TGRID(Y,(y-1)*c+x)=0,m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1+1)*c+x)=1/MAX,  printf("(y,x) = (%d,%d) with m = %d TG(Y,Y) = (%d,%d) = %f\n",y,x,m,Y,(y-1+1)*c+x,TGRID(Y,(y-1+1)*c+x)), end
        if  (y+1)>r|(x-1)<1 then printf(" __BORDER__"), m=m+1,elseif GRID(y+1, x-1) == 'O' then printf(" __OBJECT__"), TGRID(Y,(y-1)*c+x)=0,m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1+1)*c+x-1)=1/MAX, printf("(y,x) = (%d,%d) with m = %d TG(Y,Y)  = (%d,%d) = %f\n",y,x,m,Y,(y-1+1)*c+x-1,TGRID(Y,(y-1+1)*c+x-1)), end
        if   (x-1)<1 then printf(" __BORDER__"), m=m+1,elseif GRID(y, x-1) == 'O' then printf(" __OBJECT__"), TGRID(Y,(y-1)*c+x)=0,m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1)*c+x-1)=1/MAX, printf("(y,x) = (%d,%d) with m = %d TG(Y,Y)  = (%d,%d) = %f\n",y,x,m,Y,(y-1)*c+x-1,TGRID(Y,(y-1)*c+x-1)), end
        if  (y-1)<1|(x-1)<1 then printf(" __BORDER__"), m=m+1,elseif GRID(y-1, x-1) == 'O' then printf(" __OBJECT__"), TGRID(Y,(y-1)*c+x)=0,m=m+1,else  printf(" __MOVE__"),TGRID(Y,(y-1-1)*c+x-1)=1/MAX, printf("(y,x) = (%d,%d) with m = %d TG(Y,Y)  = (%d,%d) = %f\n",y,x,m,Y,(y-1-1)*c+x-1,TGRID(Y,(y-1-1)*c+x-1)), end
        
        if GRID(y, x)=='O' then  for z=1:N,TGRID(Y,z)=0,end
            else TGRID(Y,Y)=m/MAX, end
          
     
    
    end //x
  end //y
  end //Y
 
endfunction

//**********************************
// RANDOM TEST WORLD FOR BENCHMARKING 
//
// This world has one food cell with 'F' in the center and
// has a fixed diameter d according to the distance which
// shall be explored.


function[GRID]=testgrid(DISTANCE)
  
  D=DISTANCE
  //Generate normal empty space '.'
  for j=1:(2*D)+1,
    for i=1:(2*D)+1 
      GRID(i,j)='.',
    end
  end
  
  //Locate the food in the center
  GRID(D+1,D+1)='F'

endfunction


//***************************************************
// SIMPLE AUTOMATED TEST FRAMEWORK FOR ZCS
//
// Automatic testing for a certain GRID with given numers of runs
//
// ANIMAT := Structure OF ANIMAT system
// HISTORY := Protocol of movements and cumulated rewards
// GRID := Content of environment
// MODE := random (=0), non-random (=1)
// YYMAX := Number of rows times 5
// XXMAX := Number of columns times 5
// RUNS := How many cycles the simulation shall run. 1 cycle means one action
// S := scaling factor in the sparse food case
// GRID := Fixed GRID from external source
// DISTANCE := General distance from food in test field

//Prepare some parameters for liveR()

function [GRID]=liveR1prep(YYMAX, XXMAX, S,GRID,DISTANCE,SHOW)
  
    
  //GENERATE FIXED TEST GRID
  if (S==-1) then [GRID]=testgrid(DISTANCE),
  
  //GENERATE dynamic GRID
  elseif (S == 0) then [GRID]=gridgen(YYMAX, XXMAX),
  
  //GENERATE dynamic GRID with sparse food
elseif (S == 1) then [GRID]=gridgen2(YYMAX,XXMAX,S),

  // USE external GRID
else S =2,
  
end
  
endfunction



Gerd Doeben-Henisch 2012-03-31