/// <inheritdoc /> public SabberStoneAction Act(SabberStoneState state) { var timer = Stopwatch.StartNew(); var stateCopy = (SabberStoneState)state.Copy(); if (_debug) { Console.WriteLine(); } if (_debug) { Console.WriteLine(Name()); } if (_debug) { Console.WriteLine($"Starting an LSI search in turn {(stateCopy.Game.Turn + 1) / 2}"); } // Create a new LSI search var search = new LSI <List <SabberStoneAction>, SabberStoneState, SabberStoneAction, object, TreeSearchNode <SabberStoneState, SabberStoneAction>, OddmentTable <SabberStonePlayerTask> >( SideInformationStrategy, SamplingStrategy, Playout, Evaluation, GameLogic, BudgetEstimationStrategy ); // Reset the solutions collection EnsembleSolutions = new List <SabberStoneAction>(); // Create a SearchContext that just holds the current state as Source and the Search. var context = SearchContext <List <SabberStoneAction>, SabberStoneState, SabberStoneAction, object, SabberStoneAction> .Context(EnsembleSolutions, stateCopy, null, null, search, null); // The Playout strategy will call the Goal strategy from the context, so we set it here context.Goal = Goal; // Execute the search Ensemble.EnsembleSearch(context, Searcher.Search, EnsembleSize); SamplesSpent = EnsembleSolutions.Sum(i => i.BudgetUsed); // Determine a solution var solution = Searcher.VoteForSolution(EnsembleSolutions, state); timer.Stop(); if (_debug) { Console.WriteLine(); } if (_debug) { Console.WriteLine($"LSI returned with solution: {solution}"); } if (_debug) { Console.WriteLine($"My total calculation time was: {timer.ElapsedMilliseconds}ms"); } // Check if the solution is a complete action. if (!solution.IsComplete()) { // Otherwise add an End-Turn task before returning. solution.Tasks.Add((SabberStonePlayerTask)EndTurnTask.Any(Player)); } // If we are estimating the budget by using the previous search's results, save these now if (BudgetEstimation == BudgetEstimationType.PreviousSearchAverage && BudgetEstimationStrategy is PreviousSearchAverageBudgetEstimationStrategy estimationStrategy) { estimationStrategy.PreviousSearchTime = timer.ElapsedMilliseconds; estimationStrategy.PreviousSearchIterations = SamplesSpent; } if (_debug) { Console.WriteLine(); } return(solution); }
public void DoStreamingEndpointMonitor(IStreamingEndpoint origin, OperationType operationtype) { Task.Run(() => { if (operationtype == OperationType.Scale) { List <StatusInfo> LSI; DateTime starttime = DateTime.Now; System.Threading.Thread.Sleep(1000); // it take some time for the origin to switch to scaling mode while (origin.State == StreamingEndpointState.Scaling) { RefreshStreamingEndpoint(origin); System.Threading.Thread.Sleep(500); if (DateTime.Now > starttime.AddMinutes(10)) { break; } LSI = ListStatus.Where(l => l.EntityName == origin.Name).ToList(); if (LSI.Count > 0) { MessageBox.Show(LSI.FirstOrDefault().ErrorMessage); break; } } RefreshStreamingEndpoint(origin); } else if (operationtype == OperationType.Delete) { string originid = origin.Id; List <StatusInfo> LSI; DateTime starttime = DateTime.Now; while (_context.StreamingEndpoints.Where(o => o.Id == originid).FirstOrDefault() != null) { RefreshStreamingEndpoint(origin); System.Threading.Thread.Sleep(1000); if (DateTime.Now > starttime.AddMinutes(10)) { break; } LSI = ListStatus.Where(l => l.EntityName == origin.Name).ToList(); if (LSI.Count > 0) { MessageBox.Show(LSI.FirstOrDefault().ErrorMessage); break; } } RefreshStreamingEndpoints(); } else { StreamingEndpointState StateToReach; switch (operationtype) { case OperationType.Create: StateToReach = StreamingEndpointState.Stopped; break; case OperationType.Start: StateToReach = StreamingEndpointState.Running; break; case OperationType.Stop: StateToReach = StreamingEndpointState.Stopped; break; default: StateToReach = StreamingEndpointState.Stopped; break; } List <StatusInfo> LSI; DateTime starttime = DateTime.Now; while (origin.State != StateToReach) { RefreshStreamingEndpoint(origin); System.Threading.Thread.Sleep(500); if (DateTime.Now > starttime.AddMinutes(10)) { break; } LSI = ListStatus.Where(l => l.EntityName == origin.Name).ToList(); if (LSI.Count > 0) { MessageBox.Show(LSI.FirstOrDefault().ErrorMessage); break; } } RefreshStreamingEndpoint(origin); } }); }
public void DoProgramMonitor(IProgram program, OperationType operationtype) { Task.Run(() => { if (operationtype == OperationType.Delete) { List <StatusInfo> LSI; DateTime starttime = DateTime.Now; while (_context.Programs.Where(p => p.Id == program.Id).FirstOrDefault() != null) { RefreshProgram(program); System.Threading.Thread.Sleep(1000); if (DateTime.Now > starttime.AddMinutes(10)) { break; } LSI = ListStatus.Where(l => l.EntityName == program.Name).ToList(); if (LSI.Count > 0) { MessageBox.Show(LSI.FirstOrDefault().ErrorMessage); break; } } RefreshPrograms(); } else { ProgramState StateToReach; switch (operationtype) { case OperationType.Create: StateToReach = ProgramState.Stopped; break; case OperationType.Start: StateToReach = ProgramState.Running; break; case OperationType.Stop: StateToReach = ProgramState.Stopped; break; case OperationType.Reset: StateToReach = ProgramState.Stopped; break; default: StateToReach = ProgramState.Stopped; break; } List <StatusInfo> LSI; DateTime starttime = DateTime.Now; while (program.State != StateToReach) { RefreshProgram(program); System.Threading.Thread.Sleep(500); if (DateTime.Now > starttime.AddMinutes(10)) { break; } LSI = ListStatus.Where(l => l.EntityName == program.Name).ToList(); if (LSI.Count > 0) { MessageBox.Show(LSI.FirstOrDefault().ErrorMessage); break; } } RefreshProgram(program); } }); }
public void DoChannelMonitor(IChannel channel, OperationType operationtype) { Task.Run(() => { if (operationtype == OperationType.Delete) { bool timeout = false; bool Error = false; List <StatusInfo> LSI; DateTime starttime = DateTime.Now; while (_context.Channels.Where(o => o.Id == channel.Id).FirstOrDefault() != null) { RefreshChannel(channel); System.Threading.Thread.Sleep(1000); if (DateTime.Now > starttime.AddMinutes(10)) { timeout = true; break; } LSI = ListStatus.Where(l => l.EntityName == channel.Name).ToList(); if (LSI.Count > 0) { Error = true; MessageBox.Show(LSI.FirstOrDefault().ErrorMessage); break; } } RefreshChannels(); } else { ChannelState StateToReach; switch (operationtype) { case OperationType.Create: StateToReach = ChannelState.Stopped; break; case OperationType.Start: StateToReach = ChannelState.Running; break; case OperationType.Stop: StateToReach = ChannelState.Stopped; break; case OperationType.Reset: StateToReach = ChannelState.Running; break; default: StateToReach = ChannelState.Stopped; break; } bool timeout = false; bool Error = false; List <StatusInfo> LSI; DateTime starttime = DateTime.Now; while (channel.State != StateToReach) { RefreshChannel(channel); System.Threading.Thread.Sleep(500); if (DateTime.Now > starttime.AddMinutes(10)) { timeout = true; break; } LSI = ListStatus.Where(l => l.EntityName == channel.Name).ToList(); if (LSI.Count > 0) { Error = true; //MessageBox.Show(LSI.FirstOrDefault().ErrorMessage); break; } } RefreshChannel(channel); } }); }
public void TestLSI(LSI <D, P, A, S, TreeSearchNode <P, A>, SI> search) { TestAI(SearchContext <D, P, A, S, A> .GameSearchSetup(GameLogic, null, State, null, search)); }