Tag Archives: scilab

Scilab BogoSort implementation

// bogosort algorithm

clear;
function bogoSort()
    // generate a vector of random numbers
    clear;
    bogoList = []
    for i=1:100
        bogoList(i) = round(rand()*10)
    end

    // We have now created a list of randomly generated numbers (0-100)
    // We check to see if the numbers are sorted - if not - we jumble them up!
    isSorted = checkSort(bogoList)

    while(~isSorted)
        disp('Try again')
        bogoList = mixList(bogoList) 
        isSorted = checkSort(bogoList)
        disp(bogoList)
        c = [1:length(bogoList)]
        clf();plot2d(c,bogoList)
        input('space to shuffleeeeee!')
    end

endfunction

function y=mixList(unsortedList)
    for i=1:length(unsortedList)
        randomPos = round(1+rand()*(length(unsortedList)-1))
        currentNum = unsortedList(i)
        unsortedList(i) = unsortedList(randomPos)
        unsortedList(randomPos)=currentNum;
    end
    y=unsortedList
endfunction

function x=checkSort(sortedList)
    x=%T
    for i=1:length(sortedList)-1
       if(sortedList(i)>sortedList(i+1)) 
            // If the previous number is larger than the current one
        x=%F
        break;         
        end
    end
endfunction

Scilab: 4-state Cellular Automata V2.0

UPDATES:
-User input
-Variable rules (Bx/Sy)
- Refactoring of old code
- User can now create the initial field


// The value for a life cell is 2
// The value for a dead cell is 1


// Rules

// live cell surrounded by less than 2 = starvation (die)
// 2 or 3 live neighbours = survive
// exactly 3 = spawn (or stay alive)
// more than 3 = overcrowded (die)

// sieripinski triangle: B1/S12 (applied to one)
function life(rowSize, columnSize)
    
    // alter stacksize for really big fields!
    // default stacksize = 10^8
    
    // create a flexible born / die rule
    born = input('B:') //B2
    survive = input('S:')
    
    
    
    if(rowSize*columnSize>1000000)
        X = input('over 1 million cells! Continue?  Y/N ')
        if(X == 'Y')
            stacksize(168435454)    
        else
           rowSize = input('Enter new rowsize:  ') 
           columnSize = input('Enter new columnsize:   ')
        end
    end
    
    while(rowSize*columnSize > 2500000)
        disp('Too much cells, please stay below 2.5 million cells!')
        rowSize = input('Enter new rowsize:  ') 
        columnSize = input('Enter new columnsize:   ')
    end
    
    // ask if the field should be randomly generated, otherwise, testarea
    
    RGF = input('Generate random field?')
    if(RGF)
        field = generateField(rowSize, columnSize);
    else
        field = enterCustomField(rowSize,columnSize)
    end
isLaunched = %T;

liveCount = 1;
changeCount = 1
arrayOfAlive = [];
arrayOfChange = [];
generation = 0


while(isLaunched) // infinite loop
    for row = 1:rowSize 
        for column = 1:columnSize
            
            count = 0 // set count to zero for every 'cell'
            
            
            
            if(row<rowSize & column < columnSize & row > 1 & column > 1) // check boundary conditions
               if(field(row,column+1) == 2 | field(row,column+1) == 10)
                    count = count + 1
               end 
               
               if(field(row,column-1) == 2 | field(row,column-1) == 10)
                    count = count + 1
               end
               
               if(field(row-1,column-1) == 2 | field(row-1,column-1) == 10)
                    count = count + 1    
               end
               
               if(field(row-1,column)==2 | field(row-1,column) == 10)
                   count = count + 1    
               end
               
               if(field(row-1,column+1)==2 |field(row-1,column+1) == 10)
                    count = count + 1;    
               end
               
               if(field(row+1,column)==2 | field(row+1,column) == 10)
                    count = count + 1
               end
               
               if(field(row+1,column-1)==2 | field(row+1,column-1) == 10)
                   count = count + 1
               end
               
               if(field(row+1,column+1)==2 | field(row+1,column+1) == 10) // 10 signals that it was ALIVE (2) in this generation, but will DIE in the next generation!!!!!!
                   count = count + 1
               end
               
               
               
          else // WHAT IF THE ALL BOUNDARY CHECK IS FALSE
              
          end // end the -ALL BOUNDARY- check
          
          
          

  
                // Now we see what the count equals, and decide what the cell has to do accordingly
                
                // I'll have to flag them for change. 
                // Probably Von Neumans way.
                if(count==born)
                   if(field(row,column) == 1)
                        field(row,column) = 20 // flagged to be alive
                   end
                else // Check if they survive - if they don't, they'll die.
                    
                    willSurvive=%F // OUTSIDE THE IF, OTHERWISE IF 2 = %T, RESET TO %F FOR 3 & STAY AT %F.
                    for inVector=1:length(survive)
                        if(count == survive(inVector)) 
                            willSurvive=%T
                        end
                    end
                    if(~willSurvive)
                       if(field(row,column)==2)
                          field(row,column)=10 
                       end
                    end
                end
                
            
            
        end // end the for column
    end // end the for row
       
    generation = generation +1 // we have gone through one generation
   // disp(generation)
   

   changing = 0
   
    // Update the graph matrix by changing those that have to be changed!
       for rows=1:rowSize
        for columns = 1:columnSize
            if(field(rows,columns) == 10)
               field(rows,columns)=1
               changing = changing + 1
            end
                      
            if(field(rows,columns) == 20)
                field(rows,columns)=2
                changing = changing + 1
            end
        end
    end // end for ROWS
    
    //clf
    
    
    // COUNT THE LIVE ONES
       
      liveOnes = 0
      
       for rows=1:rowSize
        for columns = 1:columnSize
            if(field(rows,columns)==2)
                liveOnes = liveOnes + 1;
            end
        end
    end    
    
    arrayOfAlive(liveCount) = liveOnes;
    arrayOfChange(changeCount) = (changing)
    liveCount = liveCount + 1;
    changeCount = changeCount + 1
    
    
    if(modulo(generation,10)==0)
       subplot(222)

       plot(arrayOfAlive)
       livePlot = gca()
       xtitle('Generation vs Live Cells')
       
       // also create another graph with percentages?
       subplot(223)
       plot(arrayOfChange)
       AOC = gca()
       xtitle('Change in Life')
    end
    
    graph = gca()
    subplot(221)

    Matplot(field)
    xtitle('generation: ' + string(generation))
end


endfunction



function M=generateField(rows,columns)
        M=[]
    for i=1:rows
        for j=1:columns
            M(i,j) = 1+round(rand())
        end
    end
    
   
endfunction

function NRF=enterCustomField(rows,columns)
    NRF = [] // non Random field
    for i=1:rows
        for j=1:columns
            NRF(i,j) = 1
        end
    end
    
    // ask user for input on NRF creation. 
    
    userInput = 'input'
    while(~(userInput=='EOI'))//end of input
        userInput = input('Enter a location to be set alive:')
        if(userInput=='EOI')
            break
        end
        disp(userInput(1))
        NRF(userInput(1),userInput(2))=2
    end
endfunction




Scilab: 4-state Cellular Automata v1.1

Added: - Warnings when cell count exceeds 1 million
- Altering stacksize when cell count > 1 million

// The value for a life cell is 2
// The value for a dead cell is 1


// Rules

// live cell surrounded by less than 2 = starvation (die)
// 2 or 3 live neighbours = survive
// exactly 3 = spawn (or stay alive)
// more than 3 = overcrowded (die)
function life(rowSize, columnSize)
    
    // alter stacksize for really big fields!
    // default stacksize = 10^8
    
    if(rowSize*columnSize>1000000)
        x = input('You re creating a big field. Continue: Y / N:   ')
        if(x=='Y')
        stacksize(168435454)    // arbitrary limit working on most sizes and enough to calculate reasonably big (2.5*10^7) cells - though plotting can become 'unstable'
        else
            rowSize = 10; columnSize = 10;
        end
    end
field = generateField(rowSize, columnSize);
isLaunched = %T;

liveCount = 1;
changeCount = 1
arrayOfAlive = [];
arrayOfChange = [];
generation = 0


while(isLaunched) // infinite loop
    for row = 1:rowSize 
        for column = 1:columnSize
            
            count = 0 // set count to zero for every 'cell'
            if(row<rowSize & column < columnSize & row > 1 & column > 1) // check boundary conditions
               if(field(row,column+1) == 2 | field(row,column+1) == 10)
                    count = count + 1
               end 
               
               if(field(row,column-1) == 2 | field(row,column-1) == 10)
                    count = count + 1
               end
               
               if(field(row-1,column-1) == 2 | field(row-1,column-1) == 10)
                    count = count + 1    
               end
               
               if(field(row-1,column)==2 | field(row-1,column) == 10)
                   count = count + 1    
               end
               
               if(field(row-1,column+1)==2 |field(row-1,column+1) == 10)
                    count = count + 1;    
               end
               
               if(field(row+1,column)==2 | field(row+1,column) == 10)
                    count = count + 1
               end
               
               if(field(row+1,column-1)==2 | field(row+1,column-1) == 10)
                   count = count + 1
               end
               
               if(field(row+1,column+1)==2 | field(row+1,column+1) == 10) // 10 signals that it was ALIVE (2) in this generation, but will DIE in the next generation!!!!!!
                   count = count + 1
               end
               
               

  
                // Now we see what the count equals, and decide what the cell has to do accordingly
                
                // I'll have to flag them for change. 
                // Probably Von Neumans way.
                if(count==3)
                   if(field(row,column) == 1)
                        field(row,column) = 20 // flagged to be alive
                   end
                elseif(count<2)
                    if(field(row,column) == 2)
                        field(row,column)= 10 // flagged to be dead
                     end
                elseif(count>3)
                    if(field(row,column)==2)
                        field(row,column)= 10 // flagged to be dead
                    end
                end
                
            end // end the big if
            
        end // end the for column
    end // end the for row
       
    generation = generation +1 // we have gone through one generation
   // disp(generation)
   

   changing = 0
   
    // Update the graph matrix by changing those that have to be changed!
       for rows=1:rowSize
        for columns = 1:columnSize
            if(field(rows,columns) == 10)
               field(rows,columns)=1
               changing = changing + 1
            end
                      
            if(field(rows,columns) == 20)
                field(rows,columns)=2
                changing = changing + 1
            end
        end
    end // end for ROWS
    
    //clf
    
    
    // COUNT THE LIVE ONES
       
      liveOnes = 0
      
       for rows=1:rowSize
        for columns = 1:columnSize
            if(field(rows,columns)==2)
                liveOnes = liveOnes + 1;
            end
        end
    end    
    
    arrayOfAlive(liveCount) = liveOnes;
    arrayOfChange(changeCount) = (changing)
    liveCount = liveCount + 1;
    changeCount = changeCount + 1
    
    
    if(modulo(generation,10)==0)
       subplot(222)

       plot(arrayOfAlive)
       livePlot = gca()
       xtitle('Generation vs Live Cells')
       
       // also create another graph with percentages?
       subplot(223)
       plot(arrayOfChange)
       AOC = gca()
       xtitle('Change in Life')
    end
    
    graph = gca()
    subplot(221)

    Matplot(field)
    xtitle('generation: ' + string(generation))
end


endfunction



function M=generateField(rows,columns)
        M=[]
    for i=1:rows
        for j=1:columns
            M(i,j) = 1+round(rand())
        end
    end
    
   
endfunction

Scilab: 4-state Cellular Automaton

This code has some "optimalisation" issues at first glance, but this is due to the fact I've written a 4-state CA when a 3-state CA would reach the same outcome. I have chosen to do this, because I plan to implement "Barricelli's parasites", as first done by Barricelli in 1953 on the MANIAC, based on theories by himself and John Von Neumann. My primary goal is to illustrate that parasites, giving the power they had in 1953, will always dominate the universe.

Flaws in this 'beta' version are:
- limited universe, causing the CA to hit 'the edge of the universe'.
- A time-complexity of n² (which is not in itself a flaw, but hashlife is significantly better)

Features:
- A working CA with rules on Conway's game of life (B3/S23)
- A plot showing the current count of live cells
- A plot showing the change in life (the 3th and 4th state count)

// The value for a life cell is 2
// The value for a dead cell is 1


// Rules

// live cell surrounded by less than 2 = starvation (die)
// 2 or 3 live neighbours = survive
// exactly 3 = spawn (or stay alive)
// more than 3 = overcrowded (die)
function life(rowSize, columnSize)
field = generateField(rowSize, columnSize);
isLaunched = %T;

liveCount = 1;
changeCount = 1
arrayOfAlive = [];
arrayOfChange = [];
generation = 0


while(isLaunched) // infinite loop
    for row = 1:rowSize 
        for column = 1:columnSize
            
            count = 0 // set count to zero for every 'cell'
            if(row<rowSize & column < columnSize & row > 1 & column > 1) // check boundary conditions
               if(field(row,column+1) == 2 | field(row,column+1) == 10)
                    count = count + 1
               end 
               
               if(field(row,column-1) == 2 | field(row,column-1) == 10)
                    count = count + 1
               end
               
               if(field(row-1,column-1) == 2 | field(row-1,column-1) == 10)
                    count = count + 1    
               end
               
               if(field(row-1,column)==2 | field(row-1,column) == 10)
                   count = count + 1    
               end
               
               if(field(row-1,column+1)==2 |field(row-1,column+1) == 10)
                    count = count + 1;    
               end
               
               if(field(row+1,column)==2 | field(row+1,column) == 10)
                    count = count + 1
               end
               
               if(field(row+1,column-1)==2 | field(row+1,column-1) == 10)
                   count = count + 1
               end
               
               if(field(row+1,column+1)==2 | field(row+1,column+1) == 10) // 10 signals that it was ALIVE (2) in this generation, but will DIE in the next generation!!!!!!
                   count = count + 1
               end
               
               

  
                // Now we see what the count equals, and decide what the cell has to do accordingly
                
                // I'll have to flag them for change. 
                // Probably Von Neumans way.
                if(count==3)
                   if(field(row,column) == 1)
                        field(row,column) = 20 // flagged to be alive
                   end
                elseif(count<2)
                    if(field(row,column) == 2)
                        field(row,column)= 10 // flagged to be dead
                     end
                elseif(count>3)
                    if(field(row,column)==2)
                        field(row,column)= 10 // flagged to be dead
                    end
                end
                
            end // end the big if
            
        end // end the for column
    end // end the for row
       
    generation = generation +1 // we have gone through one generation
   // disp(generation)
   

   changing = 0
   
    // Update the graph matrix by changing those that have to be changed!
       for rows=1:rowSize
        for columns = 1:columnSize
            if(field(rows,columns) == 10)
               field(rows,columns)=1
               changing = changing + 1
            end
                      
            if(field(rows,columns) == 20)
                field(rows,columns)=2
                changing = changing + 1
            end
        end
    end // end for ROWS
    
    //clf
    
    
    // COUNT THE LIVE ONES
       
      liveOnes = 0
      
       for rows=1:rowSize
        for columns = 1:columnSize
            if(field(rows,columns)==2)
                liveOnes = liveOnes + 1;
            end
        end
    end    
    
    arrayOfAlive(liveCount) = liveOnes;
    arrayOfChange(changeCount) = (changing)
    liveCount = liveCount + 1;
    changeCount = changeCount + 1
    
    
    if(modulo(generation,10)==0)
       subplot(222)

       plot(arrayOfAlive)
       livePlot = gca()
       xtitle('Generation vs Live Cells')
       
       // also create another graph with percentages?
       subplot(223)
       plot(arrayOfChange)
       AOC = gca()
       xtitle('Change in Life')
    end
    
    graph = gca()
    subplot(221)

    Matplot(field)
    xtitle('generation: ' + string(generation))
end


endfunction



function M=generateField(rows,columns)
        M=[]
    for i=1:rows
        for j=1:columns
            M(i,j) = 1+round(rand())
        end
    end
    
   
endfunction