Exemple #1
0
            public void CalculateNode()
            {
                lock (edgeLocker)
                {
                    if (IsCalculated)
                    {
                        return;
                    }

                    List <double[]> inSchedule, outSchedule,
                                    inFixValues  = AH.CreateListOfArrays(start.algorithm._period, start.algorithm._inDimension, -1.0),
                                    outFixValues = AH.CreateListOfArrays(start.algorithm._period, start.algorithm._outDimension, -1.0);

                    for (int i = 0; i < start.crashIndex - 1; i++)
                    {
                        inFixValues[i]  = start.inSchedule[i].Select(x => x).ToArray();
                        outFixValues[i] = start.outSchedule[i].Select(x => x).ToArray();
                    }
                    for (int i = start.crashIndex - 1; i < workTime; i++)
                    {
                        inFixValues[i]  = regimesPair.Item1.Select(x => x).ToArray();
                        outFixValues[i] = regimesPair.Item2.Select(x => x).ToArray();
                    }

                    inSchedule  = start.algorithm._inAlgorithm.GetSchedule(start.initVals.inTargets, inFixValues);
                    outSchedule = start.algorithm._outAlgorithm.GetSchedule(start.initVals.outTargets, outFixValues);

                    end           = new GreedyNode(inSchedule, outSchedule, start.algorithm, start.initVals);
                    end.inputEdge = this;

                    IsCalculated = true;
                }
            }
    public override Vector3[] CalculatePath(GraphNode startNode, GraphNode goalNode)
    {
        GreedyNode start = new GreedyNode(null, startNode, Heuristic(startNode, goalNode));

        PriorityQueue <GreedyNode> openSet      = new PriorityQueue <GreedyNode>();
        List <GreedyNode>          visitedNodes = new List <GreedyNode>();

        openSet.Enqueue(start);
        int attempts = 0;

        while (openSet.Count() > 0 && attempts < 100000)
        {
            attempts += 1;
            GreedyNode currNode = openSet.Dequeue();

            if (currNode.Location == goalNode.Location)
            {
                return(ReconstructPath(start, currNode));
            }

            foreach (GraphNode neighbor in currNode.GraphNode.Neighbors)
            {
                GreedyNode greedyNodeNeighbor = new GreedyNode(currNode, neighbor, Heuristic(neighbor, goalNode));
                if (!visitedNodes.Contains(greedyNodeNeighbor))
                {
                    openSet.Enqueue(greedyNodeNeighbor);
                    visitedNodes.Add(greedyNodeNeighbor);
                }
            }
        }
        return(null);
    }
Exemple #3
0
 public GreedyEdge(GreedyNode start, Tuple <double[], double[]> regimesPair, int workTime)
 {
     this.start       = start;
     this.end         = null;
     this.regimesPair = regimesPair;
     this.workTime    = workTime;
     IsCalculated     = false;
 }
    private Vector3[] ReconstructPath(GreedyNode startNode, GreedyNode currNode)
    {
        List <Vector3> backwardsPath = new List <Vector3>();

        while (currNode != startNode)
        {
            backwardsPath.Add(currNode.Location);
            currNode = currNode.Parent;
        }
        backwardsPath.Reverse();

        return(backwardsPath.ToArray());
    }
Exemple #5
0
        internal static Stepper <NODE> BuildPath <NODE, NUMERIC>(GreedyNode <NODE, NUMERIC> node)
        {
            PathNode <NODE> start = BuildPath(node, out PathNode <NODE> end);

            return((Step <NODE> step) =>
            {
                PathNode <NODE> current = start;
                while (current != null)
                {
                    step(current.Value);
                    current = current.Next;
                }
            });
        }
Exemple #6
0
 internal static PathNode <NODE> BuildPath <NODE, NUMERIC>(GreedyNode <NODE, NUMERIC> currentNode, out PathNode <NODE> currentPathNode)
 {
     if (currentNode.Previous == null)
     {
         PathNode <NODE> start = new PathNode <NODE>(currentNode.Value);
         currentPathNode = start;
         return(start);
     }
     else
     {
         PathNode <NODE> start = BuildPath(currentNode.Previous, out PathNode <NODE> previous);
         currentPathNode = new PathNode <NODE>(currentNode.Value);
         previous.Next   = currentPathNode;
         return(start);
     }
 }
Exemple #7
0
        /// <summary>Runs the Greedy search algorithm algorithm on a graph.</summary>
        /// <param name="start">The node to start at.</param>
        /// <param name="neighbors">Step function for all neigbors of a given node.</param>
        /// <param name="heuristic">Computes the heuristic value of a given node in a graph.</param>
        /// <param name="goal">Predicate for determining if we have reached the goal node.</param>
        /// <returns>Stepper of the shortest path or null if no path exists.</returns>
        public static Stepper <NODE> Graph <NODE, NUMERIC>(NODE start, Neighbors <NODE> neighbors, Heuristic <NODE, NUMERIC> heuristic, Goal <NODE> goal)
        {
            // using a heap (aka priority queue) to store nodes based on their computed heuristic value
            IHeap <GreedyNode <NODE, NUMERIC> > fringe = new HeapArray <GreedyNode <NODE, NUMERIC> >(
                // NOTE: Typical graph search implementations prioritize smaller values
                (a, b) => Compute.Compare(b.Priority, a.Priority));

            // push starting node
            fringe.Enqueue(
                new GreedyNode <NODE, NUMERIC>(
                    null,
                    start,
                    default(NUMERIC)));

            // run the algorithm
            while (fringe.Count != 0)
            {
                GreedyNode <NODE, NUMERIC> current = fringe.Dequeue();
                if (goal(current.Value))
                {
                    return(BuildPath(current));
                }
                else
                {
                    neighbors(current.Value,
                              (NODE neighbor) =>
                    {
                        fringe.Enqueue(
                            new GreedyNode <NODE, NUMERIC>(
                                current,
                                neighbor,
                                heuristic(neighbor)));
                    });
                }
            }
            return(null); // goal node was not reached (no path exists)
        }
Exemple #8
0
 internal GreedyNode(GreedyNode <NODE, NUMERIC> previous, NODE value, NUMERIC priority)
 {
     this.Previous = previous;
     this.Value    = value;
     this.Priority = priority;
 }
 public GreedyTree(GreedyNode root)
 {
     this.root = root;
 }
 public void addCandidate(GreedyNode gn)
 {
     this.bestCandidates[nbCandidates] = gn;
     this.nbCandidates++;
 }
Exemple #11
0
        private List <Tuple <List <double[]>, List <double[]> > > GreedyRandomizeSearch(ref InitialValues initVals, List <double[]> inSchedule, List <double[]> outSchedule, Action <string> action)
        {
            ConcurrentBag <Tuple <List <double[]>, List <double[]> > > schedulesConcurrent = new ConcurrentBag <Tuple <List <double[]>, List <double[]> > >();
            int counter  = 0,
                part     = 0,
                partSize = 10;
            int threadId = 0;

            Parallel.Invoke(() =>
            {
                threadId = Thread.CurrentThread.ManagedThreadId;
            });
            GreedyNode rootNode;

            if (initVals.initRootNode == null)
            {
                rootNode = new GreedyNode(inSchedule, outSchedule, this, initVals);
                rootNode.CalculateEdges();
            }
            else
            {
                rootNode = initVals.initRootNode;
            }

            Parallel.For(0, GENERATION_NUMBER, (generation, state) =>
            {
                if (rootNode.edgesList.Count() == 0)
                {
                    state.Break();
                }

                GreedyNode currentNode = rootNode;

                while (currentNode.crashIndex != -1)
                {
                    if (!currentNode.IsCalculated)
                    {
                        currentNode.CalculateEdges();
                    }

                    GreedyEdge edge = currentNode.GetRandomEdge();

                    if (edge == null)
                    {
                        break;
                    }
                    else if (!edge.IsCalculated)
                    {
                        edge.CalculateNode();
                    }

                    currentNode = edge.end;
                }

                if (currentNode.crashIndex == -1)
                {
                    schedulesConcurrent.Add(new Tuple <List <double[]>, List <double[]> >(currentNode.inSchedule, currentNode.outSchedule));
                }

                if (currentNode.inputEdge != null)
                {
                    currentNode.inputEdge.start.RemoveEdge(currentNode.inputEdge);
                }

                Interlocked.Increment(ref counter);
                if (counter > part * partSize && Thread.CurrentThread.ManagedThreadId == threadId)
                {
                    action("Просмотрено листьев " + counter.ToString() + ", найдено решений " + schedulesConcurrent.Count().ToString());
                    part++;
                }
            });
            initVals.initRootNode = rootNode;

            return(schedulesConcurrent.ToList());
        }