Пример #1
0
 public void RemoveCellInstance(CellInstance ci)
 {
     if (this.instances.Contains(ci))
     {
         this.instances.Remove(ci);
     }
 }
Пример #2
0
 public void AddCellInstance(CellInstance ci)
 {
     if (!this.instances.Contains(ci))
     {
         this.instances.Add(ci);
     }
 }
Пример #3
0
    public static VirtualCell Copy(VirtualCell input_cell)
    {
        VirtualCell copied_cell = new VirtualCell(input_cell.location);

        foreach (CellInstance c in input_cell.instances)
        {
            copied_cell.AddCellInstance(CellInstance.Copy(c));
        }
        copied_cell.connectedCells     = input_cell.connectedCells; // They have the same connected cells TODO: maybe instead copy them alltogether?
        copied_cell.distance_from_root = input_cell.distance_from_root;
        return(copied_cell);
    }
Пример #4
0
        private Dictionary <string, HashSet <string> > _references;   // Store other cell references

        public Spreadsheet(int numberOfRows, int numberOfColumns)     // Spreadsheet constructor
        {
            _references = new Dictionary <string, HashSet <string> >();
            _cells      = new Cell[numberOfRows, numberOfColumns];
            for (int row = 0; row < numberOfRows; row++)
            {
                for (int col = 0; col < numberOfColumns; col++)
                {
                    CellInstance currentCell = new CellInstance(row, col);
                    currentCell.PropertyChanged += OnPropertyChanged; // subscribe to property changed
                    _cells[row, col]             = currentCell;
                }
            }
        }
        //constructor
        public Spreadsheet(int numRows, int numColumns)
        {
            _references = new Dictionary<string, HashSet<string>>();

            _cells = new Cell[numRows, numColumns];

            //initialize cell rowindexes and ColumnIndez
            for (int row = 0; row < numRows; row++)
            {
                for (int col = 0; col < numColumns; col++)
                {
                    CellInstance currentCell = new CellInstance(row, col);
                    //subscribe to property changed
                    currentCell.PropertyChanged += OnPropertyChanged;
                    _cells[row, col] = currentCell;
                }
            }
        }
Пример #6
0
        public void OnPropertyChanged(object sender, PropertyChangedEventArgs e) // For each fired event property changed
        {
            if (e.PropertyName == "Text")                                        // Text changed
            {
                CellInstance currentCell = sender as CellInstance;
                RemoveReferences(currentCell.Name);
                if (!string.IsNullOrEmpty(currentCell.Text) && currentCell.Text.StartsWith("=") && currentCell.Text.Length > 1)
                {
                    ExpTree expression = new ExpTree();
                    expression.ExpressionString = currentCell.Text.Substring(1);
                    AddReferences(currentCell.Name, expression.GetVar());
                }

                EvalCell(currentCell);
            }

            if (e.PropertyName == "BGColor") // Background Color changed
            {
                CellPropertyChanged(sender, new PropertyChangedEventArgs("BGColor"));
            }
        }
Пример #7
0
        /// <summary>
        /// Loops though each MainBlock and Cell adding a new instacne to any without a known solution.
        /// Copies relevant data from the original Instance on the stack.
        /// </summary>
        public void CloneInstancesAndData()
        {
            foreach (IMainBlock mainBlock in _solutionTracker.Grid.MainBlocks)
            {
                //No need to duplicate instance if it has a solution already.
                //Note: data will be added to the new instance when creating new Cell instances.
                if (mainBlock.Instances.Peek().SolutionIndex == -1)
                {
                    mainBlock.Instances.Push(new MainBlockInstance());
                }
            }

            foreach (ICell cell in _solutionTracker.Grid.Cells)
            {
                if (cell.Instances.Peek().OwnedBy == null)
                {
                    ICellInstance originalInstance = cell.Instances.Peek();
                    ICellInstance newInstance      = new CellInstance();
                    cell.Instances.Push(newInstance);
                    CopyData(originalInstance, newInstance);
                }
            }
            _solutionTracker.Grid.MaxStackHeight++;
        }
Пример #8
0
 public override void InitializeInEnvironment(CellInstance cell, ComponentWorldStateBase compData, StateSnapshot state)
 {
     cell.CellInstanceSpatialContext.Mass = mass;
     cell.CellInstanceSpatialContext.Radius = radius;
 }
Пример #9
0
 public override void DoTimeStep(CellInstance cell, ComponentWorldStateBase compData, StateSnapshot state, double time, double timeStep)
 {
     updateOutput(cell, state);
 }
Пример #10
0
 public virtual void DoTimeStep(CellInstance cell,ComponentWorldStateBase compData, StateSnapshot state, double time, double timeStep)
 {
 }
Пример #11
0
 public virtual void InitializeInEnvironment(CellInstance cell, ComponentWorldStateBase compData, StateSnapshot state)
 {
 }
Пример #12
0
 public override void InitializeInEnvironment(CellInstance cell, StateSnapshot state)
 {
     ComponentType.InitializeInEnvironment(cell, this, state);
 }
Пример #13
0
 public override void DoTimeStep(CellInstance cell, StateSnapshot state, double time, double timeStep)
 {
     ComponentType.DoTimeStep(cell, this, state, time, timeStep);
 }
Пример #14
0
        //update output (ie set values in simulator)
        private void updateOutput(CellInstance cell, StateSnapshot state)
        {
            SpeciesReference speciesRef = this.getSpeciesReference(0, ComponentLinkType.Output);
            NutrientField attractant = state.SimulationEnvironment.GetNutrientFieldObject(nutrientIndex);

            float amount = attractant.GetNutrientLevel(cell.CellInstanceSpatialContext.Position);

            cell.setSpeciesAmountInSimulation(speciesRef.species.ID,amount / 10 );
            cell.setSpeciesAmount(speciesRef.species.ID, amount / 10);
        }
Пример #15
0
 public override void InitializeInEnvironment(CellInstance cell, ComponentWorldStateBase compData, StateSnapshot state)
 {
     updateOutput(cell, state);
 }
Пример #16
0
        public void EvalCell(Cell cell) // Evaluate a cell
        {
            CellInstance instance = cell as CellInstance;

            if (string.IsNullOrEmpty(cell.Text))
            {
                instance.SetValue("");                                            //set to nothing for default
                CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); // if value changed
            }

            else if (cell.Text[0] == '=' && cell.Text.Length > 1) // for an equation
            {
                bool    error      = false;
                ExpTree expression = new ExpTree();
                expression.ExpressionString = cell.Text.Substring(1);
                List <string> expressionVar = expression.GetVar();

                foreach (string variable in expressionVar) // check for errors
                {
                    if (variable == cell.Name)             // self reference
                    {
                        instance.SetValue("!(self reference)");
                        error = true;
                        break;
                    }
                    if (GetCell(variable) == null) // cell does not exist
                    {
                        instance.SetValue("!(bad reference)");
                        error = true;
                        break;
                    }
                    if (IsCircularReference(variable, cell.Name)) // circular reference
                    {
                        instance.SetValue("!(circular reference)");
                        error = true;
                        break;
                    }

                    Cell   variableCell = GetCell(variable);
                    double variableValue;

                    if (string.IsNullOrEmpty(variableCell.Value)) // check for empty value
                    {
                        expression.SetVar(variable, 0);
                    }

                    else if (!double.TryParse(variableCell.Value, out variableValue))
                    {
                        expression.SetVar(variable, 0);
                    }

                    else
                    {
                        expression.SetVar(variable, variableValue);
                    }
                }

                if (error) // for errors
                {
                    CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));
                    return;
                }
                //at this point, the variables are set
                instance.SetValue(expression.Eval().ToString()); // Evaluate expression and set value of Cell
                CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));
            }

            else // if not an expression
            {
                instance.SetValue(cell.Text); // chage text of cell
                CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));
            }

            if (_references.ContainsKey(instance.Name)) // Evaluate referenced cells
            {
                foreach (string cellName in _references[instance.Name])
                {
                    EvalCell(GetCell(cellName));
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Execute a timestep, manipulating data accordingly
        /// </summary>
        /// <param name="cell">The cell to which this component belongs</param>
        /// <param name="state">The current environment state</param>
        /// <param name="time">Simulation time</param>
        /// <param name="timeStep">Simulation time step</param>
        public override void DoTimeStep(CellInstance cell, ComponentWorldStateBase compData,StateSnapshot state, double time, double timeStep)
        {
            FlagellaWorldState flage = (FlagellaWorldState)compData;

            //NutrientField attractant = state.SimulationEnvironment.GetNutrientFieldObject(attractantIndex);
            //NutrientField repellent = state.SimulationEnvironment.GetNutrientFieldObject(repellentIndex);
            float valueFromCircuit = (float)cell.getLocalSimulationSpeciesAmount(getSpeciesReference(0, ComponentLinkType.Input).species.ID);

            if (flage.TumbleState)
            {
                flage.TumbleCounter += (float)timeStep;

                //end of tumble
                if (flage.TumbleCounter > tumbleDuration)
                {
                    cell.CellInstanceSpatialContext.Reorientate(new Vector3((float)(cell.GetRandomObject().NextDouble() * 2 - 1),
                                                             (float)(cell.GetRandomObject().NextDouble() * 2 - 1),
                                                             (float)(cell.GetRandomObject().NextDouble() * 2 - 1)));

                    flage.TumbleState = false;
                    //up to 50% varience in next tumble duration
                    flage.TumbleCounter = 0.0f + tumbleDuration*0.5f*(float)cell.GetRandomObject().NextDouble();

                }

            }
            else
            {

                flage.TumbleUpdateCounter += (float)timeStep;

                //Randomly decide whether or not to tumble according to some likelihood
                if (flage.TumbleUpdateCounter > 1.0f / TumbleUpdateFrequency)
                {
                    float val = flage.TumbleLikelihood - (float)cell.GetRandomObject().NextDouble();

                    if (flage.TumbleLikelihood - (float)cell.GetRandomObject().NextDouble() > 0)
                    {
                        //tumble
                        flage.TumbleState = true;
                    }

                    flage.TumbleUpdateCounter = 0.0f;
                }

                flage.SampleCounter += (float)timeStep;

                //propel cell forward
                cell.CellInstanceSpatialContext.Accelerate(new Vector3(
                    cell.CellInstanceSpatialContext.Orientation.x * motiveStength * (float)timeStep,
                    cell.CellInstanceSpatialContext.Orientation.y * motiveStength * (float)timeStep,
                    cell.CellInstanceSpatialContext.Orientation.z * motiveStength * (float)timeStep));

                if (flage.SampleCounter > 0.4f)
                {
                    /*
                    if (!flage.FirstSampleTaken)
                    {
                        //flage.FirstSample = attractant.GetNutrientLevel(cell.CellInstanceSpatialContext.Position);
                        flage.FirstSample = valueFromCircuit;
                        flage.FirstSampleTaken = true;
                    }*/
                }

                if (flage.SampleCounter > 0.8f)
                {

                   // float SecondSample = attractant.GetNutrientLevel(cell.CellInstanceSpatialContext.Position);
                    /*
                    float SecondSample = valueFromCircuit;

                    if (SecondSample > flage.FirstSample)
                    {
                        //continue going straight
                        flage.TumbleLikelihood = 0.001f;

                    }
                    else
                    {
                        //tumble
                        flage.TumbleLikelihood = 0.995f;

                    }*/

                    if (valueFromCircuit > 1.0f)
                    {
                        flage.TumbleLikelihood = 0.90f;
                    }
                    else
                    {
                        flage.TumbleLikelihood = 0.1f;
                    }

                    flage.SampleCounter = 0.0f;
                   // flage.FirstSampleTaken = false;

                }

            }
        }
Пример #18
0
    public static CellInstance Copy(CellInstance other)
    {
        CellInstance new_ci = new CellInstance(other.type, other.dir);

        return(new_ci);
    }
Пример #19
0
 public override void DoTimeStep(CellInstance cell, ComponentWorldStateBase compData, StateSnapshot state, double time, double timeStep)
 {
     //nothing to do there
 }