示例#1
0
        public Grid2d <ICell> BuildCellularGrid()
        {
            //create the grid
            cellularGrid = CreateCellularGrid();

            //add cells to the grid:
            PopulateGrid();

            //initialize cell state:

            ICAConfig initialConfiguration = GetInitialConfiguration();

            if (initialConfiguration != null)
            {
                foreach (ICell cell in cellularGrid.GetObjects())
                {
                    cell.SetState(initialConfiguration.GetCellState(cell));
                }
            }


            //build neighborhoods:
            foreach (ICell cell in cellularGrid.GetObjects())
            {
                ((ICell)cell).SetNeighbors(GetNeighborhoodStrategy().BuildNeighborhood(cell));
            }

            //return the grid
            return(cellularGrid);
        }
        /**
         * Does the computation
         */
        protected override void SolveRabbitInstance(IGH_DataAccess DA)
        {
            //Get the INPUT
            //GH_ObjectWrapper CAWrapper = null;
            //DA.GetData<GH_ObjectWrapper>(1, ref CAWrapper);//param index, place holder
            //CA CA = (CA)CAWrapper.Value;

            GH_ObjectWrapper CAConfigurationWrapper = null;

            DA.GetData <GH_ObjectWrapper>(0, ref CAConfigurationWrapper);//param index, place holder
            ICAConfig CAConfiguration = (ICAConfig)CAConfigurationWrapper.Value;

            int time = CAConfiguration.GetAssociatedTime();// CA.GetMemory().GetTime(CAConfiguration);
            IEnumerable <ICell> cells = null;


            //Filter the cells, if filter is specified
            IGH_Goo filterStateValue = null;

            DA.GetData <IGH_Goo>(1, ref filterStateValue);
            if (filterStateValue == null)           //no filter specified
            {
                cells = CAConfiguration.GetCells(); //CA.GetGrid().GetObjects();
            }
            else
            {
                //filter cells with the specified state
                CellState filterCS = new GH_CellState(filterStateValue);
                //CellState filterCS = new CellState(true);
                cells = filterCells(CAConfiguration, CAConfiguration.GetCells(), filterCS);
            }


            //OUTPUT
            ArrayList cellTimes   = new ArrayList();
            ArrayList cellIndexes = new ArrayList();
            ArrayList cellStates  = new ArrayList();

            foreach (ICell cell in cells)
            {
                cellTimes.Add(time);
                //cellTimes.Add(CAConfiguration.GetCellState(cell).Equals(filterCS));
                //cellTimes.Add(new GH_Boolean(true).Value.Equals(new GH_Boolean(true).Value));
                //cellTimes.Add(new GH_Colour(255,0,0,0).Value.Equals(new GH_Colour(255,0,0,0).Value));
                //cellIndexes.Add(cell.GetId());
                cellIndexes.Add(cell.GetAttachedObject());
                //the value of the state is a GH_Goo instance
                cellStates.Add(CAConfiguration.GetCellState(cell).GetValue());//.GetValue().GetType());
            }

            //set the output parameters
            DA.SetDataList(0, cellIndexes);
            DA.SetDataList(1, cellStates);
            DA.SetDataList(2, cellTimes);
        }
示例#3
0
        private int currentTime = 0;//TODO: get that from the timer!!


        /**
         * Constructor. Creates a cellular automata
         */
        public CA(IProjection <ICell> grid)
        {
            this.grid    = grid;
            this._memory = new TimedMemory <ICAConfig>();
            ICAConfig initialConfig = BuildConfiguration(grid);

            _memory.Save(0, initialConfig);
            this.currentConfiguration = initialConfig;

            this.grid = grid;

            //listen for events on the global clock:
            DiscreteTimer.Instance.TickTackEvent += new DiscreteTimer.TickTackEventHandler(OnTickTack);
        }
        private IList <ICell> filterCells(ICAConfig CAConfiguration, IEnumerable <ICell> cells, CellState filterCS)
        {
            //TODO: use an utility class
            IList <ICell> filteredCells = new List <ICell>();

            foreach (ICell cell in cells)
            {
                if (CAConfiguration.GetCellState(cell).Equals(filterCS))
                {
                    filteredCells.Add(cell);
                }
            }

            return(filteredCells);
        }
示例#5
0
        /**
         * <returns> the CA configuration for t=discreteTime
         *
         */
        public ICAConfig Update(int discreteTime)
        {
            ICAConfig configuration = _memory.GetObject(discreteTime);
            int       timeGap       = discreteTime - _memory.GetObjects().Count;

            if (configuration == null)
            {
                for (int t = 0; t <= timeGap; t++)
                {
                    configuration = Update();
                    _memory.Save(currentTime, configuration);
                }
                return(configuration);
            }
            else
            {
                return(configuration);//_memory.GetState(discreteTime);
            }
        }
示例#6
0
 protected override ICAConfig GetInitialConfiguration()
 {
     if (initialConfiguration == null)
     {
         if (random)
         {
             initialConfiguration = randomConfig;
         }
         else if (custom)
         {
             initialConfiguration = CreateICAConfig(stateConfig);
         }
         else
         {
             initialConfiguration = null; //no configuration defined
         }
     }
     return(initialConfiguration);
 }
示例#7
0
        /**
         * Does the computation
         */
        protected override void SolveRabbitInstance(IGH_DataAccess DA)
        {
            GH_ObjectWrapper CAWrapper = null;

            //get the input parameters
            DA.GetData <GH_ObjectWrapper>(1, ref CAWrapper);//param index, place holder

            //unwrap the CA object
            CA CA = (CA)CAWrapper.Value;

            //get the time
            int time = 0;

            DA.GetData <int>(0, ref time);

            DiscreteTimer.Instance.SetTime(time);

            ICAConfig configuration = CA.GetCurrentConfiguration();//CalculateConfiguration(time);

            //set the output parameters
            DA.SetData(0, configuration);
            DA.SetDataList(1, CA.GetMemory().GetStates(DiscreteTimer.Instance.GetTime()));
            //DA.SetData(2, CA);
        }
示例#8
0
 private void OnTickTack(Object sender, EventArgs args)
 {
     currentConfiguration = Update(DiscreteTimer.Instance.GetTime());
     //notify all listeners that the CA has changed
     FireEvent(EventArgs.Empty);
 }