Ejemplo n.º 1
0
        public override double Search(bool quiet = false)
        {
            startNode = dom.nodes[dom.startPosition];
            visited   = new HashSet <int>();
            visited.Add(startNode.ID);

            VisitAllNode currentNode  = startNode;
            IState       currentState = (IState)dom.visitAllproblem.GetInitialState();

            plan.Clear();
            plan.Add(currentState);

            var operators = dom.visitAllproblem.Operators.GroupBy(op => op.GetPreconditions().Where(p => p.GetVariable() == 0).Single().GetValue()).ToDictionary(q => q.Key,
                                                                                                                                                                 q => q.GroupBy(g => g.GetEffects().Where(eff => eff.GetAssignment().GetVariable() == 0).Single().GetAssignment().GetValue()).ToDictionary(r => r.Key, r => r.Single()));

            while (visited.Count < dom.nodes.Count)
            {
                var bestSucc = getSuccessors(currentNode).ArgMax(s => evaluateSuccessor(s));
                var op       = getTransitionOperator(currentNode, bestSucc, operators);
                var newState = (IState)op.Apply(currentState);
                plan.Add(newState);
                currentNode  = bestSucc;
                currentState = newState;
                visited.Add(bestSucc.ID);
            }
            return(plan.Count() - 1);
        }
Ejemplo n.º 2
0
            public Tile(VisitAllNode node, VisitAllState state)
            {
                this.node = node;

                /*
                 * if (node.ID == 119)
                 * {
                 *
                 * }
                 */
                this.isLeaf    = false;
                this.isBlack   = (node.gridCoordX + node.gridCoordY) % 2 == 0;
                this.isVisited = state.visited[this.ID];
                int nonVisitedNeighours = 0;

                foreach (var item in this.node.successors)
                {
                    if (!state.visited[item.ID])
                    {
                        nonVisitedNeighours++;
                    }
                }
                if (!this.isVisited && nonVisitedNeighours == 1)
                {
                    this.isLeaf = true;
                }
                if (this.isVisited && nonVisitedNeighours > 0)
                {
                    this.visitedConectedToNonVisited = nonVisitedNeighours;
                }
                if (!isVisited)
                {
                    this.governor = new ComponentGovernor();
                }
            }
Ejemplo n.º 3
0
 /// <summary>
 /// Returns how good it is to visit the given successor
 /// </summary>
 /// <param name="successorNodeID"></param>
 /// <returns></returns>
 protected double evaluateSuccessor(VisitAllNode node)
 {
     if (getSuccessors(node).Count() <= 1)
     {
         return(double.MaxValue);
     }
     return(1d / (getDistanceFromStart(node) + 1));
 }
Ejemplo n.º 4
0
        private IOperator getTransitionOperator(VisitAllNode currentNode, VisitAllNode bestSucc, Dictionary <int, Dictionary <int, IOperator> > operators)
        {
            if (!dom.variableNoByNodeID.ContainsKey(currentNode.ID))
            {
                return(operators[((IState)dom.visitAllproblem.GetInitialState()).GetValue(0)][bestSucc.ID]);
            }

            return(operators[currentNode.ID][bestSucc.ID]);
        }
Ejemplo n.º 5
0
        protected override void init()
        {
            previousBest       = int.MaxValue;
            withoutImprovement = 0;
            VisitAllNode.resetIDCounter();

            dom = new VisitAllDomain(this, sasProblem);
            vis = new VisitAllVisualizer(dom);
            //vis.draw(new VisitAllState((IState)sasProblem.GetInitialState(), dom));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns all non-visited successors of given node.
        /// </summary>
        /// <param name="nodeID"></param>
        /// <returns></returns>
        protected IEnumerable <VisitAllNode> getSuccessors(VisitAllNode node)
        {
            var succ = node.successors.Where(s => !visited.Contains(s.ID)).ToList();

            if (succ.Count == 0)
            {
                //drawPlan(plan);
            }
            return(node.successors.Where(s => !visited.Contains(s.ID)));
        }
Ejemplo n.º 7
0
        public VisitAllDomain(VisitAllSolver solver, Problem visitAllproblem)
        {
            nodes = new List <VisitAllNode>();
            nodeIDByVariableNo   = new Dictionary <int, int>();
            nodeIDByOrigName     = new Dictionary <string, int>();
            variableNoByNodeID   = new Dictionary <int, int>();
            nodeIDByCoordinates  = new Dictionary <int, Dictionary <int, int> >();
            this.visitAllproblem = visitAllproblem;

            positionVariable = solver.allVariables.IndexOf(solver.allVariables.Where(v => v.Values.Any(s => s.Contains("at-robot"))).Single());
            startPosition    = ((IState)visitAllproblem.GetInitialState()).GetValue(positionVariable);
            for (int i = 0; i < visitAllproblem.Variables[positionVariable].GetDomainRange(); i++)
            {
                string meaning  = solver.getSymbolicMeaning(positionVariable, i);
                var    splitted = meaning.Split('(').Skip(1).Single();
                var    node     = new VisitAllNode(splitted.Substring(0, splitted.Length - 1));
                nodes.Add(node);
                nodeIDByOrigName.Add(node.originalName, node.ID);
                if (!nodeIDByCoordinates.ContainsKey(node.gridCoordX))
                {
                    nodeIDByCoordinates.Add(node.gridCoordX, new Dictionary <int, int>());
                }
                nodeIDByCoordinates[node.gridCoordX].Add(node.gridCoordY, node.ID);
            }
            for (int i = 0; i < visitAllproblem.Variables.Count; i++)
            {
                if (solver.allVariables[i].Values.Any(s => s.Contains("visited")))
                {
                    string meaning  = solver.allVariables[i].Values.Where(s => s.Contains("visited")).First().Split('(').Skip(1).Single();
                    string cellName = meaning.Substring(0, meaning.Length - 1);
                    int    ID       = nodeIDByOrigName[cellName];
                    var    node     = nodes[ID];
                    node.variableNumber = i;
                    nodeIDByVariableNo.Add(i, node.ID);
                    variableNoByNodeID.Add(node.ID, i);
                }
            }

            connected         = new bool[nodes.Count, nodes.Count];
            shortestDistances = new int[nodes.Count, nodes.Count];

            for (int i = 0; i < connected.GetLength(0); i++)
            {
                for (int j = 0; j < connected.GetLength(1); j++)
                {
                    connected[i, j]         = false;
                    shortestDistances[i, j] = int.MaxValue;
                    if (i == j)
                    {
                        connected[i, j]         = true;
                        shortestDistances[i, j] = 0;
                    }
                }
            }

            foreach (var op in visitAllproblem.Operators)
            {
                if (op.GetName().StartsWith("move"))
                {
                    var    splitted = op.GetName().Split(' ').Skip(1);
                    string from     = splitted.First(),
                           to       = splitted.Last();
                    connected[nodeIDByOrigName[from], nodeIDByOrigName[to]] = true;
                    nodes[nodeIDByOrigName[from]].successors.Add(nodes[nodeIDByOrigName[to]]);
                }
            }
            //computeShortestPaths();
        }
Ejemplo n.º 8
0
 protected double getDistanceFromStart(VisitAllNode n)
 {
     return((n.gridCoordX - startNode.gridCoordX) * (n.gridCoordX - startNode.gridCoordX) + (n.gridCoordY - startNode.gridCoordY) * (n.gridCoordY - startNode.gridCoordY));
 }