public void AddNeighbours(NeighbourCount neihghboursCount, CellStatus cellStatus) { for (int i = 0; i < (int)neihghboursCount; i++) { AddNeighbour(CellFactory(cellStatus).AddNeighbour(this)); } }
public void AddAliveNeighbours(NeighbourCount neihghboursCount) { AddNeighbours(neihghboursCount, CellStatus.Alive); }
private void Update(EvaluationContext context) { var wasUpdate = false; var neighbourCount = NeighbourCount.GetValue(context).Clamp(1, 9); var stateCount = StateCount.GetValue(context).Clamp(1, 100); var seed = RandomSeed.GetValue(context); var lambda = Lambda.GetValue(context); var isotropic = Isotropic.GetValue(context); if (neighbourCount != _neighbourCount || stateCount != _stateCount || seed != _randomSeed || Math.Abs(lambda - _lambda) > 0.001f || isotropic != _isotropic) { wasUpdate = true; _neighbourCount = neighbourCount; _stateCount = stateCount; _randomSeed = seed; _lambda = lambda; _isotropic = isotropic; _requiredBitCount = (int)Math.Ceiling(Math.Log(_stateCount, 2)); _ruleTableLength = 1 << (_requiredBitCount * neighbourCount); if (_cellBuffer.Length != _ruleTableLength) { _cellBuffer = new Cell[_ruleTableLength]; } var rand = new Random(seed); var countPositives = 0f; //_lambda = 0.49f; for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex++) { var choice = rand.NextDouble(); var nextState = (choice > _lambda || ruleIndex == 0) ? 0 : (rand.Next(stateCount - 1) + 1); if (nextState == 0) { countPositives++; } _cellBuffer[ruleIndex] = new Cell((uint)nextState); _cellBuffer[FlipLookupIndex(ruleIndex)] = new Cell((uint)nextState); } // Initialize with random // for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex++) // { // var nextState = rand.Next(stateCount); // _cellBuffer[ruleIndex] = new Cell((uint)nextState); // } // Flip to approach lambda //var countPositives = 0f; // int step = _ruleTableLength/6; // for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex+= step) // { // var currentL = MeasureLambda(); // if (currentL < _lambda) // { // _cellBuffer[ruleIndex] = new Cell((uint)rand.Next(stateCount-1)+1); // _cellBuffer[FlipLookupIndex(ruleIndex)] = new Cell((uint)rand.Next(stateCount-1)+1); // } // else // { // _cellBuffer[ruleIndex] = new Cell(0); // _cellBuffer[FlipLookupIndex(ruleIndex)] = new Cell(0); // } // //countPositives++; // } Log.Debug($" Lambda: {MeasureLambda()}"); const int stride = 4; var resourceManager = ResourceManager.Instance(); _bufferWithViews.Buffer = _buffer; resourceManager.SetupStructuredBuffer(_cellBuffer, stride * _cellBuffer.Length, stride, ref _buffer); resourceManager.CreateStructuredBufferSrv(_buffer, ref _bufferWithViews.Srv); resourceManager.CreateStructuredBufferUav(_buffer, UnorderedAccessViewBufferFlags.None, ref _bufferWithViews.Uav); } OutBuffer.Value = _bufferWithViews; TableLength.Value = _ruleTableLength; WasUpdated.Value = wasUpdate; }