Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/MathOnco/HAL
Browse files Browse the repository at this point in the history
  • Loading branch information
torococo committed Apr 4, 2020
2 parents a32cc9f + 611f98f commit 0b26d40
Show file tree
Hide file tree
Showing 9 changed files with 815 additions and 1 deletion.
89 changes: 89 additions & 0 deletions Examples/BasicCellularAutomata/BasicCellularAutomata.java
@@ -0,0 +1,89 @@
package Examples.BasicCellularAutomata;
import HAL.GridsAndAgents.AgentGrid2D;
import HAL.GridsAndAgents.AgentSQ2Dunstackable;
import HAL.Gui.GifMaker;
import HAL.Gui.GridWindow;
import HAL.Util;

public class BasicCellularAutomata extends AgentGrid2D<BasicCell> {

public static final int RULE = 35; // must be between 0 and 255
public static final int DOMAIN_SIZE = 200;
public GridWindow vis;

public static void main(String[]args){

BasicCellularAutomata grid=new BasicCellularAutomata();
grid.Draw();

GifMaker gif = new GifMaker("Examples/BasicCellularAutomata/rule"+Integer.toString(RULE)+".gif",10,true);
gif.AddFrame(grid.vis);

for (int tick = 2; tick <= grid.yDim; tick++) {
grid.vis.TickPause(0); // pause for n milliseconds
grid.StepCells(tick);
grid.Draw();
gif.AddFrame(grid.vis);
}
gif.Close();
grid.vis.Close();
}

// constructor for the grid
public BasicCellularAutomata() {
super(DOMAIN_SIZE*2-1, DOMAIN_SIZE, BasicCell.class,false,false);

// begin with a single cell in top middle
this.NewAgentSQ((xDim-1)/2,yDim-1);

//used for visualization
int scale_factor = (xDim > 300) ? 1 : 3;
vis=new GridWindow(xDim,yDim,scale_factor);
}


// step through all cells in a given row, determine if they're dead/alive
public void StepCells(int row){
int middle_x = (xDim-1)/2; // only need to "check" the middle section
for (int x = middle_x-(row-1); x <= middle_x+(row-1); x++) {
// create new cell in row, column
BasicCell c = this.NewAgentSQ(x,yDim-row);

// delete the cell if the rule doesn't call for a cell in that location
if (getBit(RULE,c.DetermineNeighbors()) == 0) { c.Dispose(); }
}
}

public void Draw() {
for (int i = 0; i < vis.length; i++) {
BasicCell c = this.GetAgent(i);
this.vis.SetPix(i, (c!=null) ? Util.BLACK : Util.WHITE);
}
}

// function to determine ith bit of integer
public static int getBit(int integer, int i) {
return (integer >> (7-i)) & 1;
}
}

class BasicCell extends AgentSQ2Dunstackable<BasicCellularAutomata>{

// a function which returns the neighbors' (row above) states,
// returns an integer of the case explained by rule, for example:
// http://mathworld.wolfram.com/Rule30.html
public int DetermineNeighbors() {
BasicCell above_left_cell = (this.Xsq()-1 >=0) ? G.GetAgent(this.Xsq()-1,this.Ysq()+1) : null;
BasicCell above_center_cell = G.GetAgent(this.Xsq(),this.Ysq()+1);
BasicCell above_right_cell = (this.Xsq()+1 <= G.xDim-1) ? G.GetAgent(this.Xsq()+1,this.Ysq()+1) : null;

if ((above_left_cell != null) && (above_center_cell != null) && (above_right_cell != null)) { return 0; }
if ((above_left_cell != null) && (above_center_cell != null) && (above_right_cell == null)) { return 1; }
if ((above_left_cell != null) && (above_center_cell == null) && (above_right_cell != null)) { return 2; }
if ((above_left_cell != null) && (above_center_cell == null) && (above_right_cell == null)) { return 3; }
if ((above_left_cell == null) && (above_center_cell != null) && (above_right_cell != null)) { return 4; }
if ((above_left_cell == null) && (above_center_cell != null) && (above_right_cell == null)) { return 5; }
if ((above_left_cell == null) && (above_center_cell == null) && (above_right_cell != null)) { return 6; }
return 7; // triple null case
}
}
54 changes: 54 additions & 0 deletions Examples/Outbreak/MultiOutbreak.java
@@ -0,0 +1,54 @@
package Examples.Outbreak;
import HAL.Gui.UIWindow;
import HAL.Tools.MultiWellExperiment.MultiWellExperiment;
import HAL.Util;

import static HAL.Util.*;

public class MultiOutbreak {

// used for "StepFn" argument of MultiWellExperiment
// - arguments: model, well index
// - update the model argument for one timestep.
public static void StepModel(OutbreakWorld model,int iWell){
model.StepCells();
}

// used for "ColorFn" argument of MultiWellExperiment
// - arguments: model, x, and y
// - used to set one pixel of the visualization.
public static int DrawModel(OutbreakWorld model,int x,int y){
Person c = model.GetAgent(x,y);
return OutbreakWorld.ReturnColor(c.type);
}
public static void main(String[] args){


// how many model simulations would you like to run?
int x_dimension = 7;
int y_dimension = 3;

OutbreakWorld[] models=new OutbreakWorld[x_dimension*y_dimension];

// set up all models
for (int row = 0; row < x_dimension; row++) {
for (int col = 0; col < y_dimension; col++) {

OutbreakWorld model = new OutbreakWorld();

model.QUARANTINE_RATE_SYMPTOMATIC = ((double) row / 20.0);
model.QUARANTINE_RATE_ASYMPTOMATIC = ((double) row / 20.0);

models[row*y_dimension + col] = model;
}
}

MultiWellExperiment<OutbreakWorld> expt=new MultiWellExperiment<>(x_dimension,y_dimension,models,models[0].DIMENSION,models[0].DIMENSION, 1, WHITE, MultiOutbreak::StepModel, MultiOutbreak::DrawModel);
expt.RunGIF(80,"./Examples/Outbreak/multi_world_outbreak.gif",4,false);

}
}




0 comments on commit 0b26d40

Please sign in to comment.