예제 #1
0
        public void Forage(IAntColonyContext context)
        {
            if (context.Steps.Count == 0)
            {
                throw new ArgumentOutOfRangeException("No steps provided");
            }

            ICollection <IStep> availableSteps = context.Steps;
            Int32 randomIndex = randomInstance.Next(availableSteps.Count());

            // Set the initial start point
            IStep currentStep = default(IStep);

            if (availableSteps.Count > 0)
            {
                //Console.WriteLine(string.Format("Initial random index = {0}", randomIndex));
                currentStep = availableSteps.ElementAt(randomIndex);
                //availableSteps.Remove(currentStep);
            }

            // Process each step until no steps remain
            ICollection <IEdge> availableEdges = currentStep.Available(context).OfType <IEdge>().ToList();

            while (availableEdges.Count() > 0)
            {
                IEdge nextEdge = SelectNext(availableEdges) as IEdge;
                IStep nextStep;
                if (nextEdge.StartStep.Equals(currentStep))
                {
                    nextStep = nextEdge.EndStep;
                }
                else
                {
                    nextStep = nextEdge.StartStep;
                }
                (context.Path as IPathPath).Add(currentStep, nextStep, nextEdge.Score);
                currentStep    = nextStep;
                availableEdges = currentStep.Available(context).OfType <IEdge>().ToList();
            }
            context.Path.Validate(context);
            //Console.WriteLine(string.Format("Path: {0} {1} {2}", context.Path.Score, context.Path.Edges.First().StartStep.StepName, string.Join(",", context.Path.Edges.Select(e => e.EndStep.StepName))));
        }