Example #1
0
            public void RemoveEdge(GreedyEdge edge)
            {
                lock (nodeLocker)
                {
                    int edgeIdx = edgesList.IndexOf(edge);
                    if (edgeIdx == -1)
                    {
                        return;
                    }

                    edgesList.RemoveAt(edgeIdx);
                    int probabilityIdx = avaliablePairs.IndexOf(edge.regimesPair);
                    edgesCountForPair[probabilityIdx]--;

                    bool recalcProbs = false;
                    if (edgesCountForPair[probabilityIdx] == 0)
                    {
                        pairProbabilities.RemoveAt(probabilityIdx);
                        edgesCountForPair.RemoveAt(probabilityIdx);
                        avaliablePairs.RemoveAt(probabilityIdx);
                        recalcProbs = true;
                    }

                    if (edgesList.Count() == 0)
                    {
                        IsFullExplored = true;
                        if (inputEdge != null)
                        {
                            inputEdge.start.RemoveEdge(inputEdge);
                        }
                    }
                    else if (recalcProbs)
                    {
                        pairProbabilities = pairProbabilities.Select(x => x / pairProbabilities.Sum()).ToList();
                    }
                }
            }
Example #2
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());
        }