public void Consume(CompletedSimulation sim) { RoverStats stats = sim.Stats; // Write CSV _csv.WriteField(stats.MovesLeft.ToString()); _csv.WriteField(stats.Power.ToString()); _csv.WriteField(stats.SamplesCollected.ToString()); _csv.WriteField(stats.SamplesProcessed.ToString()); _csv.WriteField(stats.SamplesTransmitted.ToString()); _csv.WriteField(stats.MoveCount.ToString()); _csv.WriteField(stats.MoveCallCount.ToString()); _csv.WriteField(stats.CollectPowerCallCount.ToString()); _csv.WriteField(stats.CollectSampleCallCount.ToString()); _csv.WriteField(stats.ProcessSamplesCallCount.ToString()); _csv.WriteField(stats.TransmitCallCount.ToString()); _csv.WriteField(sim.ProtoLevel.Seed); _csv.NextRecord(); // Update stats _moves += stats.MovesLeft; _power += stats.Power; _count++; Double delta = stats.SamplesTransmitted - _meanSamples; _meanSamples += delta / _count; Double delta2 = stats.SamplesTransmitted - _meanSamples; _mean2Samples += delta * delta2; // Update Worst Sim WorstSim = ChooseWorst(WorstSim, sim); _progress.Report(1); }
private void UpdateTimer_Tick(Object sender, EventArgs e) { Int32 delay; do { if (!_actionEnumerator.MoveNext() || !_rover.Perform(_actionEnumerator.Current, out Update update)) { UpdateTimer.Stop(); UpdateStats(); Render(); beginRender.Enabled = true; _actionEnumerator.Dispose(); return; } _stats = _stats.Add(_actionEnumerator.Current, update); _state.Apply(update); delay = _actionEnumerator.Current.Instruction switch { Instruction.Move => 75, Instruction.CollectSample => 50, _ => 0 }; }while (delay == 0); UpdateTimer.Interval = delay; UpdateStats(); Render(); }
private void BeginRender_Click(object sender, EventArgs e) { IAi ai = DemoAi.Create(DemoResult.Parameters); Level level = DemoResult.ProtoLevel.Generate(); _rover = new Rover(level, DemoResult.Parameters); _stats = RoverStats.Create(DemoResult.Parameters); _state = VisibleState.GenerateBlank(level.BottomRight, _rover.Position); _actionEnumerator = ai.Simulate(_rover.Accessor).GetEnumerator(); beginRender.Enabled = false; UpdateTimer.Start(); }
public RenderViewModel(IAiFactory aiFactory, CompletedSimulation simulation) { Ai = aiFactory ?? throw new ArgumentNullException(nameof(aiFactory)); Simulation = simulation ?? throw new ArgumentNullException(nameof(simulation)); Start = ReactiveCommand.CreateFromObservable(() => { IAi ai = Ai.Create(Simulation.Parameters); Level level = Simulation.ProtoLevel.Generate(); Rover rover = new Rover(level, Simulation.Parameters); return(Observable.Create <(RoverAction action, Update update)>(obs => { return Task.Run(async() => { var actionEnumerator = ai.Simulate(rover.Accessor).GetEnumerator(); while (actionEnumerator.MoveNext() && rover.Perform(actionEnumerator.Current, out Update update)) { obs.OnNext((actionEnumerator.Current, update)); Int32 delay = actionEnumerator.Current.Instruction switch { Instruction.Move => 75, Instruction.CollectSample => 50, _ => 0 }; if (delay != 0) { await Task.Delay(delay); } } }); })); }); Stats = Start.Aggregate(RoverStats.Create(Simulation.Parameters), (s, update) => s.Add(update.action, update.update)); State = this .WhenAnyObservable(m => m.Start) .Select(update => update.update) .Scan(VisibleState.GenerateBlank(Simulation.Parameters), (state, update) => { state.Apply(update); return(state); }); }