コード例 #1
0
        public CRFResult Run(CRFGraph graph)
        {
            Graph = graph;
            graph.Nodes.Each(n => { n.Data.IsChosen = false; });
            var vertexQueue = computeQueue(graph.Nodes.Cast <IGWNode>().ToList(), null).Cast <IGWNode <ICRFNodeDataBinary, ICRFEdgeDataBinary, ICRFGraphData> >().ToList();

            //preparation
            int counterp = 0;

            foreach (var item in vertexQueue)
            {
                item.Data.Ordinate = counterp;
                counterp++;
                item.Data.UnchosenNeighboursTemp = item.Edges.Where(e => !item.Neighbour(e).Data.IsChosen).Count();
            }

            //Create starting Combination
            {
                Combination newCombination = new Combination();
                newCombination.Assignment = new int[Graph.Nodes.Count()];


                newCombination.BorderFingerPrint = 0;
                Combinations.Add(newCombination);
            }

            Stopwatch watch = new Stopwatch();

            watch.Start();

            int counter2 = 0;

            foreach (var vertex in vertexQueue)
            {
                vertex.Data.IsChosen = true;
                var NewInnerVertices = ComputeNewBoundaryVertices(vertex);

                Barrier = DetermineBarrier(Combinations, vertex, NewInnerVertices.Count == 0, MaximalCombinationsUnderConsideration, RunsToDetermineBarrier);

                Do(Combinations, vertex, NewInnerVertices, comb => (comb.Score > Barrier || (comb.Score == Barrier && Random.NextDouble() > 0.5)), BoundaryVertices);

                //filter step two
                //this is needed, because of the heuristik the average combinations still grow if(NewInnerVertices == 1)
                if (NextGeneration.Count > MaximalCombinationsUnderConsideration)
                {
                    var ng       = NextGeneration.ToList();
                    var barrier2 = DetermineBarrierStep2(ng, MaximalCombinationsUnderConsideration, RunsToDetermineBarrier);
                    NextGeneration = new LinkedList <Combination>(ng.Where(item => item.Score > barrier2 || (item.Score == barrier2 && Random.NextDouble() > 0.5)));
                }
                if (NextGeneration.NullOrEmpty())
                {
                }

                Combinations = NextGeneration.ToList();
                NextGeneration.Clear();

                counter2++;
                //Log.Post("Vertex: " + vertex.Data.Id + " " + vertex.Data.Ordinate + @"/" + vertexQueue.Count);
                //Log.Post("Barrier: " + Barrier + "   Combinations: " + Combinations.Count);
            }
            watch.Stop();
            //Log.Post("Time Used: " + watch.ElapsedMilliseconds);

            //return result
            var result = new CRFResult();

            result.Labeling = new int[vertexQueue.Count];

            var winner = Combinations[0];

            foreach (var node in vertexQueue)
            {
                result.Labeling[node.Data.Ordinate] = winner.Assignment[node.GraphId];
            }
            result.RunTime = watch.Elapsed;
            result.Score   = winner.Score;

            return(result);
        }
コード例 #2
0
        public CRFResult Run(CRFGraph graph, IDictionary <IGWNode, int> startLabeling)
        {
            Graph = graph;
            graph.Nodes.Each(n => n.Data.IsChosen = false);
            var startPatch  = (startLabeling != null) ? startLabeling.Keys.Cast <IGWNode>() : new List <IGWNode>();
            var vertexQueue = computeQueue(graph.Nodes.Cast <IGWNode>().ToList(), startPatch).Cast <IGWNode <ICRFNodeData, ICRFEdgeData, ICRFGraphData> >().ToList();

            //preparation
            int counter = 0;

            if (startLabeling != null)
            {
                foreach (IGWNode <ICRFNodeData, ICRFEdgeData, ICRFGraphData> item in startLabeling.Keys)
                {
                    item.Data.IsChosen = true;
                }
            }
            foreach (var item in vertexQueue)
            {
                counter++;
                item.Data.UnchosenNeighboursTemp = item.Edges.Where(e => !item.Neighbour(e).Data.IsChosen).Count();
            }

            //Create starting Combination
            {
                CRFLabelling newCombination = new CRFLabelling();
                newCombination.AssignedLabels = new int[Graph.Nodes.Count() - startPatch.Count()];

                var score = 0.0;
                if (startLabeling != null)
                {
                    foreach (var item in startLabeling)
                    {
                        var key = item.Key as IGWNode <ICRFNodeData, ICRFEdgeData, ICRFGraphData>;
                        score += key.Data.Score(item.Value);
                    }

                    foreach (var edge in graph.Edges)
                    {
                        if (edge.Head.Data.IsChosen && edge.Foot.Data.IsChosen)
                        {
                            score += edge.Score(edge.Head.GraphId, startLabeling[edge.Head], edge.Foot.GraphId, startLabeling[edge.Foot]);
                        }
                    }
                }
                newCombination.BorderFingerPrint = 0;
                Combinations.Add(newCombination);
            }

            Stopwatch watch = new Stopwatch();

            watch.Start();

            int counter2 = 0;

            foreach (var vertex in vertexQueue)
            {
                vertex.Data.IsChosen = true;
                var NewInnerVertices = ComputeNewBoundaryVertices(vertex);

                Barrier = DetermineBarrier(Combinations, vertex, NewInnerVertices.Count == 0, MaximalCombinationsUnderConsideration, RunsToDetermineBarrier);

                Do(Combinations, vertex, NewInnerVertices, comb => (comb.Score > Barrier || (comb.Score == Barrier && Random.NextDouble() > 0.5)), BoundaryVertices);

                //filter step two
                //this is needed, because of the heuristik the average combinations still grow if(NewInnerVertices == 1)
                if (NextGeneration.Count > MaximalCombinationsUnderConsideration)
                {
                    var ng       = NextGeneration.ToList();
                    var barrier2 = DetermineBarrierStep2(ng, MaximalCombinationsUnderConsideration, RunsToDetermineBarrier);
                    NextGeneration = new LinkedList <CRFLabelling>(ng.Where(item => item.Score > barrier2 || (item.Score == barrier2 && Random.NextDouble() > 0.5)));
                }

                Combinations = NextGeneration.ToList();
                NextGeneration.Clear();

                counter2++;
            }
            watch.Stop();

            //return result
            var result = new CRFResult();

            result.Labeling = new int[vertexQueue.Count];
            var winner = Combinations[0];

            if (startLabeling != null)
            {
                foreach (var node in startLabeling)
                {
                    result.Labeling[node.Key.GraphId] = node.Value;
                }
            }
            foreach (var node in vertexQueue)
            {
                result.Labeling[node.GraphId] = winner.AssignedLabels[node.GraphId];
            }
            result.RunTime = watch.Elapsed;
            result.Score   = winner.Score;

            return(result);
        }