private void InitState() { if (mStatePool == null) { mStatePool = new StatePool(this); } }
internal void Push(StatePool statePool) { foreach (StateModule stateModule in this) { StateManagementEngine.PushStateOnPool(statePool, stateModule); } }
/// <summary> /// Function Compares Bfs and Dfs algorithm functions. /// </summary> public void CompareSolvers() { IMazeGenerator dfsMaze = new DFSMazeGenerator(); // Print the maze. //Console.WriteLine(dfsMaze.ToString()); StatePool <Position> spDfs = new StatePool <Position>(); //Adapter adDfs = new Adapter(2, 2, 0, 0, 1, 1, "test Dfs", spDfs); Maze maze = dfsMaze.Generate(500, 500); Console.WriteLine(maze.ToString()); Adapter adDfs = new Adapter(maze, spDfs); ISearcher <Position> dfs = new Dfs <Position>(); Solution <Position> solDfs = dfs.Search(adDfs); Console.WriteLine(Adapter.ToJson(solDfs, "test")); // Creates the solution in Json format. /* * StatePool<Position> spBfs = new StatePool<Position>(); * // Adapter adBfs = new Adapter(10, 10, 0, 0, 8, 1, "test Bfs", spBfs); * Maze maze = dfsMaze.Generate(500, 500); * Console.WriteLine(maze.ToString()); * Adapter adBfs = new Adapter(maze, spBfs); * ISearcher<Position> bfs = new Bfs<Position>(); * Solution<Position> solBfs = bfs.search(adBfs); * //Console.WriteLine(adBfs.ToJson(solBfs)); */ }
/// <summary> /// Setup the simulation /// </summary> /// <param name="circuit">Circuit</param> protected override void Setup(Circuit circuit) { if (circuit == null) { throw new ArgumentNullException(nameof(circuit)); } // Get base behaviors base.Setup(circuit); // Get behaviors and configurations var config = ParameterSets.Get <TimeConfiguration>() ?? throw new CircuitException("{0}: No time configuration".FormatString(Name)); TimeConfiguration = config; Method = config.Method ?? throw new CircuitException("{0}: No integration method specified".FormatString(Name)); TransientBehaviors = SetupBehaviors <BaseTransientBehavior>(circuit.Objects); // Setup the state pool and register states StatePool = new StatePool(Method); for (int i = 0; i < TransientBehaviors.Count; i++) { TransientBehaviors[i].GetEquationPointers(RealState.Solver); TransientBehaviors[i].CreateStates(StatePool); } StatePool.BuildStates(); }
/// <summary> /// Creates a maze according to row and colom size, and solves the maze using the /// relevant algorithm. /// </summary> /// <param name="row">Num of rows.</param> /// <param name="colom">Num of coloms.</param> /// <param name="algorithmId">The id of the relevant algorithm to run.</param> /// <returns>The solution to the algorithm. </returns> public Solution <Position> Solve(Maze maze, int algorithmId) { StatePool <Position> sp = new StatePool <Position>(); Adapter ad = new Adapter(maze, sp); ISearcher <Position> algorithm = this.algorithmFac.CreateAlgorithm(algorithmId); return(algorithm.Search(ad)); }
private static void PushStateImplementation(StatePool pool, StateModule stateModule) { ExecutionEventLog.RecordStatus("Recording Previous State: " + stateModule); stateModule.StateImplementation.RecordPreviousState(stateModule); ExecutionEventLog.RecordStatus("Applying State: " + stateModule); stateModule.StateImplementation.ApplyState(stateModule); StatePools[(int)pool].Push(stateModule); }
/// <summary> /// Rolls back the state of the specified pool. /// This method is only meant for consumption by the test infrastructure. /// </summary> /// <param name="pool"></param> internal static void PopAllStatesFromPool(StatePool pool) { //Work back from most specific to the selected pool for (int i = (int)StatePool.Last; i >= (int)pool; i--) { PopAllStatesFromPoolImplementation(i); } }
/// <summary> /// Create states /// </summary> /// <param name="states">States</param> public override void CreateStates(StatePool states) { if (states == null) { throw new ArgumentNullException(nameof(states)); } CapCharge = states.CreateDerivative(); }
/// <summary> /// Register states /// </summary> /// <param name="states">States</param> public virtual void CreateStates(StatePool states) { if (states == null) { throw new ArgumentNullException(nameof(states)); } // Do nothing (for now) }
/// <summary> /// Ctor. /// </summary> /// <param name="row">number of rows of maze.</param> /// <param name="col">number of cols of maze.</param> /// <param name="startX">x coordinate of start point.</param> /// <param name="startY">y coordinate of start point.</param> /// <param name="endX">x coordinate of end point.</param> /// <param name="endY">y coordinate of end point.</param> /// <param name="mazeName">name of maze.</param> /// <param name="sp">state pool.</param> public Adapter(int row, int col, int startX, int startY, int endX, int endY, string mazeName, StatePool <Position> sp) { this.maze = new Maze(row, col); Console.WriteLine(this.maze.ToString()); maze.Name = mazeName; maze.InitialPos = new Position(startX, startY); maze.GoalPos = new Position(endX, endY); this.statePool = sp; }
/// <summary> /// Create states /// </summary> /// <param name="states">Pool of all states</param> public override void CreateStates(StatePool states) { if (states == null) { throw new ArgumentNullException(nameof(states)); } // We just need a history without integration here StateChargeBe = states.CreateDerivative(); StateChargeBc = states.CreateDerivative(); StateChargeCs = states.CreateDerivative(); StateChargeBx = states.CreateDerivative(); StateExcessPhaseCurrentBc = states.CreateHistory(); }
/// <summary> /// Create states /// </summary> /// <param name="states">States</param> public override void CreateStates(StatePool states) { if (states == null) { throw new ArgumentNullException(nameof(states)); } VoltageBs = states.CreateHistory(); VoltageGs = states.CreateHistory(); VoltageDs = states.CreateHistory(); CapGs = states.CreateHistory(); CapGd = states.CreateHistory(); CapGb = states.CreateHistory(); ChargeGs = states.CreateDerivative(); ChargeGd = states.CreateDerivative(); ChargeGb = states.CreateDerivative(); ChargeBd = states.CreateDerivative(); ChargeBs = states.CreateDerivative(); }
/// <summary> /// Create states /// </summary> /// <param name="states">States</param> public override void CreateStates(StatePool states) { if (states == null) { throw new ArgumentNullException(nameof(states)); } _vgs = states.CreateHistory(); _vds = states.CreateHistory(); _vbs = states.CreateHistory(); _capgs = states.CreateHistory(); _capgd = states.CreateHistory(); _capgb = states.CreateHistory(); _qgs = states.CreateDerivative(); _qgd = states.CreateDerivative(); _qgb = states.CreateDerivative(); _qbd = states.CreateDerivative(); _qbs = states.CreateDerivative(); }
/// <summary> /// Solves a maze. /// </summary> /// <param name="mazeName">Maze name.</param> /// <param name="algorithmType">Algorithm type.</param> /// <returns>Solution.</returns> public Solution <Position> SolveMaze(string mazeName, int algorithmType) { //Check is solution already exists. if (SolvedMazes.ContainsKey(mazeName)) { return(SolvedMazes[mazeName]); } //Check if maze exists. if (!this.GenerateMazes.ContainsKey(mazeName)) { return(null); } Maze maze = GenerateMazes[mazeName]; //Solve maze. StatePool <Position> statePool = new StatePool <Position>(); SolutionAdapter ad = new SolutionAdapter(maze, statePool); ISearcher <Position> algorithm = this.AlgorithmFactory.CreateAlgorithm(algorithmType); return(algorithm.Search(ad)); }
public override LoanApplicationState Cancel() { return(StatePool.GetState <CancelledState>()); }
/// <summary> /// Ctor. /// </summary> /// <param name="maze">maze object.</param> /// <param name="sp">state pool.</param> public Adapter(Maze maze, StatePool <Position> sp) { this.maze = maze; this.statePool = sp; }
/// <summary> /// Execute the transient simulation /// </summary> protected override void Execute() { // First do temperature-dependent calculations and IC base.Execute(); var exportargs = new ExportDataEventArgs(this); var state = RealState; var baseConfig = BaseConfiguration; var timeConfig = TimeConfiguration; double delta = Math.Min(timeConfig.FinalTime / 50.0, timeConfig.Step) / 10.0; // Initialize before starting the simulation state.UseIc = timeConfig.UseIc; state.Domain = RealState.DomainType.Time; state.Gmin = baseConfig.Gmin; // Use node initial conditions if device initial conditions are not used if (!timeConfig.UseIc) { OnLoad += LoadInitialConditions; } // Calculate the operating point Op(baseConfig.DcMaxIterations); Statistics.TimePoints++; Method.DeltaOld.Clear(timeConfig.MaxStep); Method.Delta = delta; Method.SaveDelta = timeConfig.FinalTime / 50.0; // Stop calculating a DC solution state.UseIc = false; state.UseDc = false; for (int i = 0; i < TransientBehaviors.Count; i++) { TransientBehaviors[i].GetDcState(this); } StatePool.ClearDc(); OnLoad -= LoadInitialConditions; // Start our statistics Statistics.TransientTime.Start(); int startIters = Statistics.Iterations; var startselapsed = Statistics.SolveTime.Elapsed; try { while (true) { // nextTime: // Accept the current timepoint (CKTaccept()) for (int i = 0; i < AcceptBehaviors.Count; i++) { AcceptBehaviors[i].Accept(this); } Method.SaveSolution(state.Solution); // end of CKTaccept() // Check if current breakpoint is outdated; if so, clear Method.UpdateBreakpoints(); Statistics.Accepted++; // Export the current timepoint if (Method.Time >= timeConfig.InitTime) { Export(exportargs); } // Detect the end of the simulation if (Method.Time >= timeConfig.FinalTime) { // Keep our statistics Statistics.TransientTime.Stop(); Statistics.TransientIterations += Statistics.Iterations - startIters; Statistics.TransientSolveTime += Statistics.SolveTime.Elapsed - startselapsed; // Finished! OnLoad -= LoadInitialConditions; return; } // Pause test - pausing not supported // resume: Method.Delta = Math.Min(Method.Delta, timeConfig.MaxStep); Method.Resume(); StatePool.History.Cycle(); // Calculate a new solution while (true) { Method.TryDelta(); // Compute coefficients and predict a solution and reset states to our previous solution Method.ComputeCoefficients(this); Method.Predict(this); // Try to solve the new point if (Method.SavedTime.Equals(0.0)) { state.Init = RealState.InitializationStates.InitTransient; } bool converged = TimeIterate(timeConfig.TranMaxIterations); Statistics.TimePoints++; // Spice copies the states the first time, we're not // I believe this is because Spice treats the first timepoint after the OP as special (MODEINITTRAN) // We don't treat it special (we just assume it started from a circuit in rest) if (!converged) { // Failed to converge, let's try again with a smaller timestep Method.Rollback(); Statistics.Rejected++; Method.Delta /= 8.0; Method.CutOrder(); var data = new TimestepCutEventArgs(Method.Delta / 8.0, TimestepCutEventArgs.TimestepCutReason.Convergence); TimestepCut?.Invoke(this, data); } else { // Do not check the first time point if (Method.SavedTime.Equals(0.0) || Method.LteControl(this)) { // goto nextTime; break; } Statistics.Rejected++; var data = new TimestepCutEventArgs(Method.Delta, TimestepCutEventArgs.TimestepCutReason.Truncation); TimestepCut?.Invoke(this, data); } if (Method.Delta <= timeConfig.DeltaMin) { if (Method.OldDelta > timeConfig.DeltaMin) { Method.Delta = timeConfig.DeltaMin; } else { throw new CircuitException("Timestep too small at t={0:e}: {1:e}".FormatString(Method.SavedTime, Method.Delta)); } } } } } catch (CircuitException ex) { // Keep our statistics Statistics.TransientTime.Stop(); Statistics.TransientIterations += Statistics.Iterations - startIters; Statistics.TransientSolveTime += Statistics.SolveTime.Elapsed - startselapsed; throw new CircuitException("{0}: transient terminated".FormatString(Name), ex); } }
public static LoanApplicationState GetState(int value) { var type = _values.FirstOrDefault(a => a.Value == value); return(StatePool.GetState(type.Key)); }
protected override void Awake() { _head = new HeadState(); //_states = new List<BaseState>(); _pool = new StatePool(); }
public override LoanApplicationState Reject() { return(StatePool.GetState <RejectedState>()); }
/// <summary> /// Constructor /// This constructor should not be used, except for in the <see cref="StatePool"/> class. /// </summary> /// <param name="source">Pool of states instantiating the variable</param> /// <param name="index">The index/identifier of the state variable</param> public StateDerivative(StatePool source, int index) : base(source, index) { }
// /// <summary> /// Sets the state on the specified pool. /// This method is only meant for consumption by the test infrastructure. /// </summary> /// <param name="pool"></param> /// <param name="stateModule"></param> internal static void PushStateOnPool(StatePool pool, StateModule stateModule) { PushStateImplementation(pool, stateModule); }
/// <summary> /// Constructor for the Maze to Searchable adapter that gets the Maze. /// </summary> /// <param name="maze"> /// The maze that we will use for this adapter. /// </param> public MazeToSearchableAdapter(Maze maze) { this.maze = maze; this.statePool = new StatePool <Position>(); }
public override LoanApplicationState Confirm() { return(StatePool.GetState <ConfirmedState>()); }