private IBanditRepo<ConstantRewardAlternative> CreateRepo() { SimulatorRepository repo = new SimulatorRepository(); repo.AddAlternative(new ConstantRewardAlternative(0.5f)); //best repo.AddAlternative(new ConstantRewardAlternative(0.2f)); //worst return repo; }
private static Runner InitializeRuntime() { var runner = new Runner(); runner.Started += (o, e) => { _log.Trace($"Runner started."); }; runner.IterationPassed += (o, e) => { _log.Trace($"Runner passed iteration"); }; runner.Stopped += (o, e) => { _log.Trace($"Runner stopped."); }; // radio channel var radioSim = new AdaptedFriisSimulator(); radioSim.With((args) => { var radioArgs = args as AdaptedFriisArgs; radioArgs.RadioBox.Resolution = 0.2F; }); // energy var batterySim = new BatteryPackSimulator(); batterySim.With((args) => { var batteryArgs = args as BatteryArgs; var battery = batteryArgs.AddBattery(); batteryArgs.UpdateDischargeCurrent(battery.Uid, 100); }); // network var networkSim = new MeshNetworkSimulator(); BuildMeshNetwork(networkSim.Arguments as MeshNetworkArgs); // pack all simulators in a reop var simRepo = new SimulatorRepository(); simRepo.AddRange(new ISimulatable[] { radioSim, networkSim, batterySim }); runner.BindSimulators(simRepo); return(runner); }
public async Task RunBatterySimulation() { // arrange var runtime = new RuntimeController(new EnergyValidator()); // TODO: seem to have a bug when setting up a higher cutoff voltage --> test and fix it _battery.CutoffVoltage = 1.2F; var s = _battery.State; var batteryArgs = new BatteryArgs(); batteryArgs.Batteries.Add(_battery); var netArgs = new NetworkArgs(); var device = new SimpleDevice(); device.Parts.Add(_battery); netArgs.Network.Add(device); var batterySim = new BatteryPackSimulator(); batterySim .With(batteryArgs) .With(netArgs) .With(runtime.Arguments); var repo = new SimulatorRepository(); repo.Add(batterySim); if (!runtime.BindSimulators(repo).Validate()) { Assert.Fail("error on validating the simulation"); } // act await runtime.RunAsync((rt) => { // assert return(!_battery.State.IsDepleted); }); Assert.AreNotEqual(s.Initial.ElapsedTime, s.Now.ElapsedTime, "ElapsedTime should not be equal"); Assert.AreNotEqual(s.Initial.Charge, s.Now.Charge, "Charge should not be equal"); Assert.AreNotEqual(s.Initial.SoD, s.Now.SoD, "SoD should not be equal"); Assert.AreNotEqual(s.Initial.Voltage, s.Now.Voltage, "Voltage should not be equal"); Assert.IsTrue(_battery.State.IsDepleted, "battery should be empty"); }
private void SetupDemoRepo(RuntimeBase runtime) { var sceneArgs = new InvariantSceneArgs(); var radioArgs = base.GetRadioArgs(); var comArgs = new WirelessCommArgs(); var netArgs = new NetworkArgs(); var antennaArgs = new SimpleAntennaArgs(); _repo = new SimulatorRepository(); _repo.AddRange(new ISimulatable[] { new SceneSimulator(runtime) .With(sceneArgs), new AdaptedFriisSimulator(runtime) .With(radioArgs) .With(comArgs), new SimpleAntennaSimulator(runtime) .With(antennaArgs), new PeerToPeerNetworkSimulator(runtime) .With(netArgs), new LrwpanSimulator(runtime) .With(comArgs) }); }
public async Task RunNetwork() { // arrange var runtime = new RuntimeController(new BasicValidator()); var netSim = new PeerToPeerNetworkSimulator(runtime); var netArgs = new NetworkArgs(); netSim.OnExecuting += (o, e) => { _network.AssociationMatrix.Each(ApplyAssociations); }; netSim.With(netArgs); _network = netArgs.Network; _network.AddRange(ImportDevices().First()); var simRepo = new SimulatorRepository(); simRepo.Add(netSim); var pass = 0; var maxPasses = 3; runtime.IterationPassed += (o, e) => { pass++; if (pass == maxPasses) { var dev = netArgs.Network.Items[0]; dev.Controls.Off(); _log.Trace($"Device '{dev.Name}' was turned off."); } }; runtime.BindSimulators(simRepo) .Validate(); // act iterations until availability is below 100% await runtime.RunAsync((r) => { if (netArgs.Network.Availability() < 1) { return(false); } else { return(true); } }); // assert Assert.IsTrue(pass >= maxPasses, $"The iterations should be more than {maxPasses}."); netArgs.Network.DistanceMatrix.Each((r, c, v) => { Assert.IsTrue(v > 0, $"position at row '{r}' and col '{c}' should not be '{v}'"); _log.Trace($"{r}:{c} -> distance: {v}"); return(v); }); netArgs.Network.AngleMatrix.Each((r, c, v) => { Assert.IsTrue(!float.IsNaN(v.Azimuth), $"Azimuth at position at row '{r}' and col '{c}' should not be NaN"); Assert.IsTrue(!float.IsNaN(v.Elevation), $"Elevation at position at row '{r}' and col '{c}' should not be NaN"); _log.Trace($"{r}:{c} -> angle: {v}"); return(v); }); }
/// <summary> /// Takes the Simulator Repository instance with all ready-to-go simulators and sets the internal valid-state to invalid. /// This ensures that the concretion of this base class implements a validation method and runs it before the simulation. /// </summary> /// <param name="simulatorRepo">The repository instance</param> /// <returns>Returns the calling instance for method chaining</returns> public RuntimeBase BindSimulators(SimulatorRepository simulatorRepo) { _isValid = false; _simRepo = simulatorRepo ?? throw new ArgumentException("The simulation repository must not be null.", nameof(simulatorRepo)); return(this); }
/// <summary> /// Takes the Simulator Repository instance with all ready-to-go simulators and sets the internal valid-state to invalid. /// This ensures that the concretion of this base class implements a validation method and runs it before the simulation. /// </summary> /// <param name="simulatorRepo">The repository instance</param> /// <returns>Returns the calling instance for method chaining</returns> public virtual RuntimeBase BindSimulators(SimulatorRepository simulatorRepo) { _isValid = false; _simRepo = simulatorRepo; return(this); }
// -- constructor protected RuntimeBase() { _log = LoggingProvider.CreateLogger <RuntimeBase>(); _simRepo = new SimulatorRepository(); }
// -- constructor public RuntimeValidator(SimulatorRepository simulatorRepository) { _simulators = simulatorRepository; }
public async Task SimulateRuntimeAsync() { // -- arrange // radio channel var radioSim = new AdaptedFriisSimulator(); radioSim.With((args) => { var radioArgs = args as AdaptedFriisArgs; radioArgs.RadioBox.Resolution = 0.2F; }); // energy var batterySim = new BatteryPackSimulator(); batterySim.With((args) => { var batteryArgs = args as BatteryArgs; var battery = batteryArgs.AddBattery(); batteryArgs.UpdateDischargeCurrent(battery.Uid, 100); }); // network var networkSim = new MeshNetworkSimulator(); BuildMeshNetwork(networkSim.Arguments as MeshNetworkArgs); // pack all simulators in a reop var simRepo = new SimulatorRepository(); simRepo.AddRange(new ISimulatable[] { radioSim, networkSim, batterySim }); // runtime var runner = new Runner(); runner.BindSimulators(simRepo); runner.Started += (o, e) => { _log.Trace($"Runner started."); }; runner.IterationPassed += (o, e) => { _log.Trace($"Runner passed iteration"); }; runner.Stopped += (o, e) => { _log.Trace($"Runner stopped."); }; // -- act int iterations = 5; if (!runner.Validate()) { Assert.Fail("Error on validating the simulation runtime."); } _log.Trace($"RunAsync for {iterations} times"); await runner.RunAsync(iterations); // -- assert var runArgs = runner.Arguments as RuntimeArgs; Assert.IsTrue(runArgs.Iterations == 5, $"simulation should have passed {iterations} iterations"); }