/// <summary>
 /// Constructs a new instance.
 /// </summary>
 /// <param name="explorationStrategy">The exploration strategy.</param>
 /// <param name="playoutStrategy">The playout strategy.</param>
 /// <param name="samplingStrategy">The sampling strategy.</param>
 /// <param name="solutionStrategy">The solution strategy.</param>
 /// <param name="policyGlobal">The global policy.</param>
 public NMCTSBuilder(IExplorationStrategy <D, P, A, S, Sol> explorationStrategy, IPlayoutStrategy <D, P, A, S, Sol> playoutStrategy, ISamplingStrategy <P, A> samplingStrategy, ISolutionStrategy <D, P, A, S, Sol, TreeSearchNode <P, A> > solutionStrategy, double policyGlobal)
 {
     ExplorationStrategy = explorationStrategy;
     PlayoutStrategy     = playoutStrategy;
     SamplingStrategy    = samplingStrategy;
     SolutionStrategy    = solutionStrategy;
     PolicyGlobal        = policyGlobal;
 }
 /// <summary>
 /// Constructs a new instance without time or iterative restrictions.
 /// </summary>
 /// <param name="selectionStrategy">The selection strategy.</param>
 /// <param name="expansionStrategy">The expansion strategy.</param>
 /// <param name="backPropagationStrategy">The back propagation strategy.</param>
 /// <param name="finalNodeSelectionStrategy">The final node selection strategy.</param>
 /// <param name="evaluationStrategy">The state evaluation strategy.</param>
 /// <param name="solutionStrategy">The solution strategy.</param>
 /// <param name="time">[Optional] The time budget for this search. Default value is <see cref="Constants.NO_LIMIT_ON_THINKING_TIME"/>.</param>
 /// <param name="iterations">[Optional] The iteration budget for this search. Default value is <see cref="Constants.NO_LIMIT_ON_ITERATIONS"/>.</param>
 protected TreeSearch(ITreeSelection <D, P, A, S, Sol> selectionStrategy, ITreeExpansion <D, P, A, S, Sol> expansionStrategy, ITreeBackPropagation <D, P, A, S, Sol> backPropagationStrategy, ITreeFinalNodeSelection <D, P, A, S, Sol> finalNodeSelectionStrategy, IStateEvaluation <D, P, A, S, Sol, TreeSearchNode <P, A> > evaluationStrategy, ISolutionStrategy <D, P, A, S, Sol, TreeSearchNode <P, A> > solutionStrategy, long time = Constants.NO_LIMIT_ON_THINKING_TIME, int iterations = Constants.NO_LIMIT_ON_ITERATIONS)
 {
     SelectionStrategy          = selectionStrategy;
     ExpansionStrategy          = expansionStrategy;
     BackPropagationStrategy    = backPropagationStrategy;
     FinalNodeSelectionStrategy = finalNodeSelectionStrategy;
     EvaluationStrategy         = evaluationStrategy;
     SolutionStrategy           = solutionStrategy;
     Time       = time;
     Iterations = iterations;
 }
예제 #3
0
 /// <summary>
 /// Constructs a new instance.
 /// </summary>
 /// <param name="selectionStrategy">The selection strategy.</param>
 /// <param name="expansionStrategy">The expansion strategy.</param>
 /// <param name="backPropagationStrategy">The back propagation strategy.</param>
 /// <param name="finalNodeSelectionStrategy">The final node selection strategy.</param>
 /// <param name="evaluationStrategy">The state evaluation strategy.</param>
 /// <param name="solutionStrategy">The solution strategy.</param>
 /// <param name="playoutStrategy">The playout strategy.</param>
 /// <param name="time">The amount of time allowed for the search.</param>
 /// <param name="iterations">The amount of iterations allowed for the search.</param>
 public FlatMCS(ITreeSelection <D, P, A, S, Sol> selectionStrategy,
                ITreeExpansion <D, P, A, S, Sol> expansionStrategy,
                ITreeBackPropagation <D, P, A, S, Sol> backPropagationStrategy,
                ITreeFinalNodeSelection <D, P, A, S, Sol> finalNodeSelectionStrategy,
                IStateEvaluation <D, P, A, S, Sol, TreeSearchNode <P, A> > evaluationStrategy,
                ISolutionStrategy <D, P, A, S, Sol, TreeSearchNode <P, A> > solutionStrategy,
                IPlayoutStrategy <D, P, A, S, Sol> playoutStrategy, long time, int iterations) : base(
         selectionStrategy, expansionStrategy, backPropagationStrategy, finalNodeSelectionStrategy,
         evaluationStrategy, solutionStrategy, time, iterations)
 {
     PlayoutStrategy = playoutStrategy;
 }
예제 #4
0
 /// <summary>
 /// Constructs a new instance.
 /// </summary>
 /// <param name="selectionStrategy">The selection strategy.</param>
 /// <param name="expansionStrategy">The expansion strategy.</param>
 /// <param name="backPropagationStrategy">The back propagation strategy.</param>
 /// <param name="finalNodeSelectionStrategy">The final node selection strategy.</param>
 /// <param name="evaluationStrategy">The state evaluation strategy.</param>
 /// <param name="explorationStrategy">The exploration strategy.</param>
 /// <param name="solutionStrategy">The solution strategy.</param>
 /// <param name="samplingStrategy">The sampling strategy.</param>
 /// <param name="playoutStrategy">The playout strategy.</param>
 /// <param name="time">The amount of time allowed for the search.</param>
 /// <param name="iterations">The amount of iterations allowed for the search.</param>
 /// <param name="globalPolicy">The global policy.</param>
 public NMCTS(ITreeSelection <D, P, A, S, Sol> selectionStrategy,
              ITreeExpansion <D, P, A, S, Sol> expansionStrategy,
              ITreeBackPropagation <D, P, A, S, Sol> backPropagationStrategy,
              ITreeFinalNodeSelection <D, P, A, S, Sol> finalNodeSelectionStrategy,
              IStateEvaluation <D, P, A, S, Sol, TreeSearchNode <P, A> > evaluationStrategy,
              IExplorationStrategy <D, P, A, S, Sol> explorationStrategy,
              ISolutionStrategy <D, P, A, S, Sol, TreeSearchNode <P, A> > solutionStrategy,
              ISamplingStrategy <P, A> samplingStrategy,
              IPlayoutStrategy <D, P, A, S, Sol> playoutStrategy, long time, int iterations, double globalPolicy) :
     base(selectionStrategy, expansionStrategy, backPropagationStrategy, finalNodeSelectionStrategy,
          evaluationStrategy, solutionStrategy, time, iterations)
 {
     ExplorationStrategy = explorationStrategy;
     SamplingStrategy    = samplingStrategy;
     PlayoutStrategy     = playoutStrategy;
     PolicyGlobal        = globalPolicy;
 }
예제 #5
0
        /// <summary>
        /// Get Updates on the work being completed from the WorkerRole that is working on this problem using this strategy as they are happening
        /// </summary>
        /// <param name="ctx"><see cref="ZeroMQ.ZmqContext"/> on which to open ZmqSocket.SUB connection to the Publisher WorkerRole doing the work</param>
        /// <param name="strategy">The <see cref="WaterBucket.Domain.ISolutionStrategy"/> being employed to solve the problem on which you want to receive update messages</param>
        /// <param name="onCompletion">What to execute when the Worker has sent a completion of the problem message</param>
        /// <returns>A sequence of <see cref="WaterBucket.Domain.BucketActionStep"/>s every time one is received on the <see cref="ZeroMQ.ZmqSocket"/> from the WorkerRole</returns>
        /// <seealso cref="ZeroMQ.ZmqSocket"/>
        protected IEnumerable<BucketActionStep> GetWorkerUpdates(ZmqContext ctx, ISolutionStrategy strategy, Action<SolutionResult> onCompletion = null)
        {
            if (ctx == null)
                throw new ArgumentNullException("ctx");
            if (strategy == null)
                throw new ArgumentNullException("strategy");
            var publisherAddresses = GetPublisherAddresses();
            if (!publisherAddresses.Any())
            {
                throw new WorkerException("Could not get any publisher endpoints to receive messages from the Worker");
            }

            // Sockets are NOT Thread safe so they must be created in the Thread in which they will be used
            ZmqSocket subscriber = ctx.CreateSocket(SocketType.SUB);
            subscriber.Subscribe(Encoding.UTF8.GetBytes(strategy.Signature));
            foreach (var pubAddr in publisherAddresses)
            {
                subscriber.Connect(pubAddr);
            }
            ProblemUpdate update;
            do
            {
                update = null;
                // This is the blocking version of Receiving from a ZMQ Socket - this is okay since we will run this call from a child Task or wrap in Rx
                ZmqMessage msg = subscriber.ReceiveMessage();
                // Skip the first Frame since it is the Subscription Prefix
                var msgSig = msg.Unwrap();
                // Get the UpdateType from the Message
                byte[] updateTypeData = msg.Unwrap().Buffer;
                ProblemUpdateType updateType = (ProblemUpdateType)updateTypeData[0];
                //update = new ProblemUpdate(updateType, msg.Last.Buffer);
                // Find the data demarcation frame
                var updateFrames = msg.SkipWhile(f => (f.BufferSize != 1) && (f.Buffer[0] != 0x7f));//.ToList();
                // Skip the data demarcation frame when creating the ProblemUpdate
                update = new ProblemUpdate(updateType, updateFrames.Skip(1).ToList());
                if (update.IsAction || update.IsInitial)
                    // Return BucketActionSteps to the awaiting caller
                    yield return update.IntoFrameableType<BucketActionStep>().UpdateState;
            } while ((update != null) && !(update.IsCompletion || update.IsError));

            if (update != null)
            {
                // Signal Completion
                if ((update.IsCompletion) && (onCompletion != null))
                    onCompletion(update.IntoFrameableType<SolutionResult>().UpdateState);

                // Throw an Exception to the caller if the WorkerRole experienced an Exception
                if (update.IsError)
                {
                    throw update.GetException<Exception>();
                }
            }
        }
예제 #6
0
 private static void SolutionExceptionOccurred(ISolutionStrategy strategy, ZmqSocket socket, Exception ex, IFormatter formatter = null)
 {
     SendUpdateMessage(strategy, ProblemUpdateType.Error, socket, ex, formatter);
 }
예제 #7
0
 private static void SolutionCompletion(ISolutionStrategy strategy, ZmqSocket socket, ref bool completed, IFormatter formatter = null)
 {
     SendUpdateMessage(strategy, ProblemUpdateType.Completion, socket, strategy.Result as IFrameable);
     completed = true;
 }
예제 #8
0
 private static void SolutionAction(ISolutionStrategy strategy, ZmqSocket socket, BucketActionStep step, IFormatter formatter = null)
 {
     SendUpdateMessage(strategy, step.ActionTaken == BucketActions.Init ? ProblemUpdateType.Initial : ProblemUpdateType.Action, socket, step as IFrameable);
 }
예제 #9
0
 private static void SendUpdateMessage(ISolutionStrategy strategy, ProblemUpdateType updateType, ZmqSocket socket, IFrameable data)
 {
     ZmqMessage message = GetUpdateMessage(strategy, updateType, data);
     socket.SendMessage(message);
 }
예제 #10
0
 private static void SendUpdateMessage(ISolutionStrategy strategy, ProblemUpdateType updateType, ZmqSocket socket, IBinaryConvertible data)
 {
     ZmqMessage message = new ZmqMessage();
     message.Append(Encoding.UTF8.GetBytes(strategy.Signature));
     message.AppendEmptyFrame();
     message.Append(new byte[1] { (byte)updateType });
     message.AppendEmptyFrame();
     message.Append(new byte[1] { 0x7f });
     message.Append(data.GetBytes());
     socket.SendMessage(message);
 }
예제 #11
0
 private static void SendUpdateMessage(ISolutionStrategy strategy, ProblemUpdateType updateType, ZmqSocket socket, object data, IFormatter formatter = null)
 {
     formatter = formatter ?? new BinaryFormatter();
     ZmqMessage message = new ZmqMessage();
     message.Append(Encoding.UTF8.GetBytes(strategy.Signature));
     message.AppendEmptyFrame();
     message.Append(new byte[1] { (byte)updateType });
     message.AppendEmptyFrame();
     message.Append(new byte[1] { 0x7f });
     using (MemoryStream ms = new MemoryStream())
     {
         formatter.Serialize(ms, data);
         // TODO: Find more efficient way to write byte[] since MemoryStream.ToArray performs a copy
         //       whereas MemoryStream.GetBuffer() returns all allocated bytes whether they are empty or not
         message.Append(ms.ToArray());
     }
     socket.SendMessage(message);
 }
예제 #12
0
 private static ZmqMessage GetUpdateMessage(ISolutionStrategy strategy, ProblemUpdateType updateType, IFrameable data)
 {
     var dataFrames = data.GetFrames(Encoding.UTF8).ToList();
     List<Frame> msgFrames = new List<Frame>(dataFrames.Count + 5);
     msgFrames.Add(new Frame(Encoding.UTF8.GetBytes(strategy.Signature)));
     msgFrames.Add(Frame.Empty);
     msgFrames.Add(new Frame(new byte[1] { (byte)updateType }));
     msgFrames.Add(Frame.Empty);
     msgFrames.Add(new Frame(new byte[1] { 0x7f }));
     msgFrames.AddRange(dataFrames);
     ZmqMessage message = new ZmqMessage(msgFrames);
     return message;
 }
예제 #13
0
 /// <summary>
 /// Constructs a new instance.
 /// </summary>
 /// <param name="playoutStrategy">The playout strategy.</param>
 /// <param name="solutionStrategy">The solution strategy.</param>
 public FlatMCSBuilder(IPlayoutStrategy <D, P, A, S, Sol> playoutStrategy, ISolutionStrategy <D, P, A, S, Sol, TreeSearchNode <P, A> > solutionStrategy)
 {
     PlayoutStrategy  = playoutStrategy;
     SolutionStrategy = solutionStrategy;
 }
예제 #14
0
 public IEnumerable<BucketActionStep> Solve(ISolutionStrategy strategy)
 {
     if (!this.IsSolvable)
         throw new NotSolvableException("Attempting to solve an unsolvable Problem will result in infinite recursion");
     ProblemState initialState = strategy.CurrentProblemState;
     yield return new BucketActionStep(strategy.StrategyName, FirstBucket, BucketActions.Init, 0, 0, "Initial State", initialState);
     while (!strategy.TestGoal())
     {
         yield return strategy.TakeAction();
         if (initialState.SameBucketState(strategy.CurrentProblemState))
             throw new NotSolvableException("Problem has reached Initial State again and therefore is not Solvable");
     }
 }