public LinkedList <CRFLabelling> CreateNextGeneration(IGWNode <ICRFNodeData, ICRFEdgeData, ICRFGraphData> chosenVertex, CRFLabelling oldCombination) { LinkedList <CRFLabelling> newCombinations = new LinkedList <CRFLabelling>(); for (int counter = 0; counter < NumberLabels; counter++) { CRFLabelling newCombination = default(CRFLabelling); if (counter == NumberLabels - 1) { newCombination = new CRFLabelling(); newCombination.Score = oldCombination.Score; newCombination.AssignedLabels = oldCombination.AssignedLabels; } else { newCombination = new CRFLabelling(oldCombination); newCombination.Score = oldCombination.Score; } newCombination.AddScore(chosenVertex, counter); newCombinations.AddLast(newCombination); } return(newCombinations); }
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); }