internal static List <AssemblyCandidate> Run(designGraph assemblyGraph, List <TessellatedSolid> solids, List <int> globalDirPool, List <TessellatedSolid> solides) { //DisassemblyDirections.Directions = TemporaryDirections(); var solutions = new List <AssemblyCandidate>(); assemblyEvaluator = new AssemblyEvaluator(solids); //Updates.UpdateGlobalDirections(globalDirPool); assemblyGraph.addHyperArc(assemblyGraph.nodes); var iniHy = assemblyGraph.hyperarcs[assemblyGraph.hyperarcs.Count - 1]; iniHy.localLabels.Add(DisConstants.SeperateHyperarcs); var candidates = new SortedList <List <double>, AssemblyCandidate>(new MO_optimizeSort()); var found = false; AssemblyCandidate goal = null; var ini = new AssemblyCandidate(new candidate(assemblyGraph, 1)); candidates.Add(new List <double>(), ini); while (candidates.Count != 0 && !found) { var current = candidates.Values[0]; candidates.Clear(); if (isCurrentTheGoal(current)) { goal = current; found = true; break; } var options = new List <option>(); foreach (var cndDirInd in globalDirPool) { foreach ( var seperateHy in current.graph.hyperarcs.Where(h => h.localLabels.Contains(DisConstants.SeperateHyperarcs)) .ToList()) { SCC.StronglyConnectedComponents(current.graph, seperateHy, cndDirInd); //BoostedSCC.StronglyConnectedComponents(current.graph, seperateHy, cndDirInd); var blockingDic = DBG.DirectionalBlockingGraph(current.graph, cndDirInd); options.AddRange(OptionGeneratorPro.GenerateOptions(current.graph, seperateHy, blockingDic, options)); } } foreach (var opt in options) { //var child = (AssemblyCandidate) current.copy(); //SearchProcess.transferLmappingToChild(child.graph, current.graph, opt); //var rest = Updates.AddSecondHyperToOption(child, opt); //Updates.ApplyChild(child, opt); //if (assemblyEvaluator.Evaluate(child, opt, rest, solides) > 0) // candidates.Add(child.performanceParams, child); //child.addToRecipe(opt); } } solutions.Add(goal); TemporaryFixingSequence(goal); return(solutions); }
internal static void StronglyConnectedComponents(designGraph graph, HashSet <Component> seperate, int cndDir) { // The function takes every hyperarc with "seperate" lable and generates its SCCs with respect to // the candidate direction. After generation, a new hyperarc is added to the graph with local lable "SCC". var seperateHys = seperate.Select(n => new hyperarc("", new List <node> { n })).ToList(); seperateHys.AddRange(graph.hyperarcs.Where(h => h.localLabels.Contains(DisConstants.gSCC))); var stack = new Stack <hyperarc>(); var visited = new HashSet <hyperarc>(); var globalVisited = new HashSet <hyperarc>(); foreach (var nodeHy in seperateHys.Where(n => !globalVisited.Contains(n))) { stack.Clear(); visited.Clear(); stack.Push(nodeHy); while (stack.Count > 0) { var pNodeHy = stack.Pop(); visited.Add(pNodeHy); globalVisited.Add(pNodeHy); var connections = HyperBorderArcs(graph, pNodeHy); foreach (var pHyArc in connections) { if (Removable(pHyArc, cndDir)) { continue; } //var otherNode = (Component)(pNodeArc.From == pNodeHy ? pNodeArc.To : pNodeArc.From); var otherHys = OtherHyperarcFinder(pHyArc, pNodeHy, seperateHys).ToList(); if (!otherHys.Any()) { continue; } if (visited.Contains(otherHys[0])) { continue; } //if (!seperateHys.Contains(otherHys[0])) //continue; stack.Push(otherHys[0]); } } var visNodes = visited.SelectMany(v => v.nodes).ToList(); if (visNodes.Count == seperate.Count) { continue; } var last = graph.addHyperArc(visNodes); last.localLabels.Add(DisConstants.SCC); } }
//Calculate the heuristic value of a given assembly A protected static double H(HashSet <Component> A) { if (A.Count <= 1) { return(0); } if (Memo.ContainsKey(A)) { return(Memo[A].Value); } var L = Math.Log(A.Count, 2); double MinTreeDepth = Math.Ceiling(L); var hy = Graph.addHyperArc(A.Cast <node>().ToList()); List <double> Values = new List <double>(); foreach (Connection arc in hy.IntraArcs.Where(a => a is Connection)) { HashSet <Component> arcnodes = new HashSet <Component>(new Component[] { (Component)arc.From, (Component)arc.To }); Values.Add(Memo[arcnodes].Value); } Graph.removeHyperArc(hy); Values.Sort(); double total = 0; for (int x = 0; x < MinTreeDepth; x++) { total = total + Values[x]; } return(Math.Max(total, Values.Last())); /* * var intraArcs = new List<arc>(); * foreach (var Component in subassemblyNodes) * { * foreach (arc arc in Component.arcs) * { * if (Component == arc.From) * { * if (subassemblyNodes.Contains(arc.To)) * intraArcs.Add(arc); * } * else * if (subassemblyNodes.Contains(arc.From)) * intraArcs.Add(arc); * } * } */ }
internal static void StronglyConnectedComponents(designGraph assemblyGraph, hyperarc seperate, int cndDir) { // The function takes every hyperarc with "seperate" lable and generates its SCCs with respect to // the candidate direction. After generation, a new hyperarc is added to the graph with local lable "SCC". var stack = new Stack <Component>(); var visited = new HashSet <Component>(); var globalVisited = new HashSet <Component>(); foreach (Component node in seperate.nodes.Where(n => !globalVisited.Contains(n))) { stack.Clear(); visited.Clear(); stack.Push(node); while (stack.Count > 0) { var pNode = stack.Pop(); visited.Add(pNode); globalVisited.Add(pNode); foreach (Connection pNodeArc in pNode.arcs.Where(a => a.GetType() == typeof(Connection))) { if (Removable(pNodeArc, cndDir)) { continue; } var otherNode = (Component)(pNodeArc.From == pNode ? pNodeArc.To : pNodeArc.From); if (visited.Contains(otherNode)) { continue; } stack.Push(otherNode); } } if (visited.Count == seperate.nodes.Count) { continue; } var last = assemblyGraph.addHyperArc(visited.Cast <node>().ToList()); last.localLabels.Add(DisConstants.SCC); } }
internal static Dictionary <hyperarc, List <hyperarc> > SolveMutualBlocking(designGraph assemblyGraph, Dictionary <hyperarc, List <hyperarc> > dbgDictionary) { for (var i = 0; i < dbgDictionary.Count - 1; i++) { var iKey = dbgDictionary.Keys.ToList()[i]; for (var j = i + 1; j < dbgDictionary.Count; j++) { var jKey = dbgDictionary.Keys.ToList()[j]; if (dbgDictionary[iKey].Contains(jKey) && dbgDictionary[jKey].Contains(iKey)) { var nodes = new List <node>(); nodes.AddRange(iKey.nodes); nodes.AddRange(jKey.nodes); var nodes2 = new List <node>(nodes); var last = assemblyGraph.addHyperArc(nodes2); last.localLabels.Add(DisConstants.SCC); var updatedBlocking = new List <hyperarc>(); updatedBlocking.AddRange(dbgDictionary[iKey].Where(hy => hy != jKey)); updatedBlocking.AddRange(dbgDictionary[jKey].Where(hy => hy != iKey && !updatedBlocking.Contains(hy))); var updatedBlocking2 = new List <hyperarc>(updatedBlocking); dbgDictionary.Remove(iKey); dbgDictionary.Remove(jKey); foreach (var key in dbgDictionary.Keys.ToList()) { if (dbgDictionary[key].Contains(iKey) || dbgDictionary[key].Contains(jKey)) { dbgDictionary[key].Add(last); dbgDictionary[key].Remove(iKey); dbgDictionary[key].Remove(jKey); } } assemblyGraph.removeHyperArc(iKey); assemblyGraph.removeHyperArc(jKey); dbgDictionary.Add(last, updatedBlocking2); i--; break; } } } return(dbgDictionary); }
private static bool MutualBlocking(designGraph assemblyGraph, Dictionary <hyperarc, List <hyperarc> > dbgDictionary) { for (var i = 0; i < dbgDictionary.Count - 1; i++) { var iKey = dbgDictionary.Keys.ToList()[i]; for (var j = i + 1; j < dbgDictionary.Count; j++) { var jKey = dbgDictionary.Keys.ToList()[j]; if (dbgDictionary[iKey].Contains(jKey) && dbgDictionary[jKey].Contains(iKey)) { var nodes = new List <node>(); nodes.AddRange(iKey.nodes); nodes.AddRange(jKey.nodes); assemblyGraph.removeHyperArc(iKey); assemblyGraph.removeHyperArc(jKey); var last = assemblyGraph.addHyperArc(nodes); last.localLabels.Add(DisConstants.SCC); return(true); } } } return(false); }
internal static void GenerateOptions(designGraph assemblyGraph, hyperarc seperate, Dictionary <hyperarc, List <hyperarc> > blockingDic) { // This sorting is not necessary, but I think it will speed up the search. Let's see! blockingDic = blockingDic.OrderBy(a => a.Value.Count).ToDictionary(x => x.Key, x => x.Value); var localRemovableHy = new List <List <hyperarc> >(); var trash = new List <hyperarc>(); var visitedStatesCount = 0; var visitedStates = new List <hyperarc>(); while (visitedStatesCount != blockingDic.Count) { foreach (var sccHy in blockingDic.Keys.Where(scc => !visitedStates.Contains(scc))) { if (blockingDic[sccHy].Count == 0) { sccHy.localLabels.Add(DisConstants.Removable); trash.Add(sccHy); visitedStatesCount++; visitedStates.Add(sccHy); localRemovableHy.Add(new List <hyperarc> { sccHy }); } else { if (blockingDic[sccHy].All(trash.Contains)) { Preceedings.Clear(); visitedStatesCount++; visitedStates.Add(sccHy); PreceedingFinder(sccHy, blockingDic); Preceedings = Updates.UpdatePreceedings(Preceedings); var nodes = new List <node>(); foreach (var hyperarc in Preceedings) { nodes.AddRange(hyperarc.nodes); } if (nodes.Count == seperate.nodes.Count) { continue; } assemblyGraph.addHyperArc(nodes); assemblyGraph.hyperarcs[assemblyGraph.hyperarcs.Count - 1].localLabels.Add( DisConstants.Removable); trash.Add(sccHy); localRemovableHy.Add(new List <hyperarc>(Preceedings)); Preceedings.Clear(); } } } } //I am not done yet, not all of the options are generated by this point! for (var i = 0; i < localRemovableHy.Count - 1; i++) { for (var j = i + 1; j < localRemovableHy.Count; j++) { if (localRemovableHy[i].All(localRemovableHy[j].Contains) || localRemovableHy[j].All(localRemovableHy[i].Contains)) { continue; } var merged = new List <hyperarc>(); merged.AddRange(localRemovableHy[i]); foreach (var l in localRemovableHy[j].Where(l => !merged.Contains(l))) { merged.Add(l); } var exists = false; for (var k = j + 1; k < localRemovableHy.Count; k++) { if (localRemovableHy[k].All(merged.Contains) && merged.All(localRemovableHy[k].Contains)) { exists = true; break; } } if (exists) { continue; } var nodes = new List <node>(); foreach (var hy in merged) { nodes.AddRange(hy.nodes); } if (nodes.Count == seperate.nodes.Count) { continue; } assemblyGraph.addHyperArc(nodes); assemblyGraph.hyperarcs[assemblyGraph.hyperarcs.Count - 1].localLabels.Add( DisConstants.Removable); localRemovableHy.Add(merged); } } //for (var i = 0; i < assemblyGraph.hyperarcs.Count; i++) //{ // var hyScc = assemblyGraph.hyperarcs[i]; // if (hyScc.localLabels.Contains(DisConstants.SCC) && // !hyScc.localLabels.Contains(DisConstants.Removable)) // assemblyGraph.hyperarcs.Remove(hyScc); //} foreach (var SCCHy in assemblyGraph.hyperarcs.Where(hyScc => hyScc.localLabels.Contains(DisConstants.SCC) && !hyScc.localLabels.Contains(DisConstants.Removable)).ToList()) { assemblyGraph.hyperarcs.Remove(SCCHy); } }
internal static Dictionary <hyperarc, List <hyperarc> > SolveMutualBlocking(designGraph assemblyGraph, Dictionary <hyperarc, List <hyperarc> > dbgDictionary, Dictionary <hyperarc, List <hyperarc> > dbgDictionaryAdjacent, bool relaxingSc = false) { foreach (var key in dbgDictionary.Keys) { if (dbgDictionary[key].Contains(key)) { dbgDictionary[key].Remove(key); } } for (var i = 0; i < dbgDictionary.Count - 1; i++) { var iKey = dbgDictionary.Keys.ToList()[i]; dbgDictionary[iKey].RemoveAll(a => a == null); for (var j = i + 1; j < dbgDictionary.Count; j++) { var jKey = dbgDictionary.Keys.ToList()[j]; dbgDictionary[jKey].RemoveAll(a => a == null); if (dbgDictionary[iKey].Contains(jKey) && dbgDictionary[jKey].Contains(iKey)) { if (relaxingSc) { // if these two keys are not phisically connected, update the dbg /*if ( * assemblyGraph.arcs.Where(a => a is Connection) * .Cast<Connection>() * .Any( * a => * (iKey.nodes.Any(n => n.name == a.From.name) && * jKey.nodes.Any(n => n.name == a.To.name)) || * (iKey.nodes.Any(n => n.name == a.To.name) && * jKey.nodes.Any(n => n.name == a.From.name)))) * continue;*/ if (dbgDictionaryAdjacent[iKey].Contains(jKey) && dbgDictionaryAdjacent[jKey].Contains(iKey)) { continue; } // take the one with less volume and delete it from the value of the otherone's key var volumei = iKey.nodes.Cast <Component>().Sum(n => n.Volume); var volumej = jKey.nodes.Cast <Component>().Sum(n => n.Volume); if ((dbgDictionary[iKey].Count == 1 && dbgDictionary[jKey].Count == 1) || (dbgDictionary[iKey].Count > 1 && dbgDictionary[jKey].Count > 1)) { if (!dbgDictionaryAdjacent[iKey].Contains(jKey) && !dbgDictionaryAdjacent[jKey].Contains(iKey)) { if (volumei < volumej) { if (!dbgDictionaryAdjacent[iKey].Contains(jKey)) { dbgDictionary[iKey].Remove(jKey); } } else { dbgDictionary[jKey].Remove(iKey); } } if (!dbgDictionaryAdjacent[iKey].Contains(jKey)) { dbgDictionary[iKey].Remove(jKey); } else { dbgDictionary[jKey].Remove(iKey); } dbgDictionary[iKey].RemoveAll(a => a == null); dbgDictionary[jKey].RemoveAll(a => a == null); } else { if (!dbgDictionaryAdjacent[iKey].Contains(jKey) && !dbgDictionaryAdjacent[jKey].Contains(iKey)) { if (dbgDictionary[iKey].Count == 1) { dbgDictionary[iKey].Remove(jKey); } else { dbgDictionary[jKey].Remove(iKey); } } if (!dbgDictionaryAdjacent[iKey].Contains(jKey)) { dbgDictionary[iKey].Remove(jKey); } else { dbgDictionary[jKey].Remove(iKey); } dbgDictionary[iKey].RemoveAll(a => a == null); dbgDictionary[jKey].RemoveAll(a => a == null); } continue; } var nodes = new List <node>(); nodes.AddRange(iKey.nodes); nodes.AddRange(jKey.nodes); var nodes2 = new List <node>(nodes); var last = assemblyGraph.addHyperArc(nodes2); last.localLabels.Add(DisConstants.SCC); var updatedBlocking = new List <hyperarc>(); updatedBlocking.AddRange(dbgDictionary[iKey].Where(hy => hy != jKey)); updatedBlocking.AddRange( dbgDictionary[jKey].Where(hy => hy != iKey && !updatedBlocking.Contains(hy))); var updatedBlocking2 = new List <hyperarc>(updatedBlocking); dbgDictionary.Remove(iKey); dbgDictionary.Remove(jKey); foreach (var key in dbgDictionary.Keys.ToList()) { if (dbgDictionary[key].Contains(iKey) || dbgDictionary[key].Contains(jKey)) { dbgDictionary[key].Add(last); dbgDictionary[key].Remove(iKey); dbgDictionary[key].Remove(jKey); dbgDictionary[key].RemoveAll(a => a == null); } } assemblyGraph.removeHyperArc(iKey); assemblyGraph.removeHyperArc(jKey); // if there is no connection between the keys dbgDictionary.Add(last, updatedBlocking2); i--; break; } } } return(dbgDictionary); }
// OptimizedSCC is the modified SCC. In this class, a dictionary of SCCs for each direction is created // to take benefit from premade SCC hyperarcs. The dictionary will be updated after each "apply" internal static void StronglyConnectedComponents(designGraph assemblyGraph, hyperarc seperateHy, int cndDir) { var preAddedSccs = new List <Component>(); if (BeamSearch.SccTracker.Keys.Contains(cndDir)) { foreach (var premadeSCC in BeamSearch.SccTracker[cndDir]) { var c = 0; foreach (var node in premadeSCC) { c += seperateHy.nodes.Count(n => n.name == node.name); } if (c != premadeSCC.Count) { continue; } var nodes = new List <node>(); foreach (var n in premadeSCC) { nodes.AddRange(seperateHy.nodes.Where(a => a.name == n.name)); } var last = assemblyGraph.addHyperArc(nodes); last.localLabels.Add(DisConstants.SCC); preAddedSccs.AddRange(premadeSCC); } //foreach (var premadeSCC in DisassemblyProcess.SccTracker[cndDir].Where(pmSCC => pmSCC.All(n => seperateHy.nodes.Contains(n)))) //{ // var last = assemblyGraph.addHyperArc(premadeSCC); // last.localLabels.Add(DisConstants.SCC); // preAddedSccs.AddRange(premadeSCC); //} } var globalVisited = new List <Component>(); var stack = new Stack <Component>(); var visited = new HashSet <Component>(); var sccTrackerNodes = new List <List <Component> >(); foreach (Component node in seperateHy.nodes.Where(n => !globalVisited.Contains(n))) { if (preAddedSccs.Any(n => n.name == node.name)) { continue; } stack.Clear(); visited.Clear(); stack.Push(node); while (stack.Count > 0) { var pNode = stack.Pop(); visited.Add(pNode); globalVisited.Add(pNode); foreach (Connection pNodeArc in pNode.arcs.Where(a => a.GetType() == typeof(Connection))) { if (SCC.Removable(pNodeArc, cndDir)) { continue; } var otherNode = (Component)(pNodeArc.From == pNode ? pNodeArc.To : pNodeArc.From); if (visited.Contains(otherNode)) { continue; } stack.Push(otherNode); } } assemblyGraph.addHyperArc(visited.Cast <node>().ToList()); assemblyGraph.hyperarcs[assemblyGraph.hyperarcs.Count - 1].localLabels.Add(DisConstants.SCC); sccTrackerNodes.Add(visited.ToList()); } if (BeamSearch.SccTracker.Keys.Contains(cndDir)) { BeamSearch.SccTracker[cndDir] = sccTrackerNodes; } else { BeamSearch.SccTracker.Add(cndDir, sccTrackerNodes); } }
public override void Paste() { var ClipboardString = Clipboard.GetText(); var copiedSelection = SelectionClass.DeSerializeClipboardFormatFromXML(ClipboardString); RestoreDisplayShapes(copiedSelection.ReadInXmlShapes, copiedSelection.selectedNodes, copiedSelection.selectedArcs, copiedSelection.selectedHyperArcs); var newSelection = new List <UIElement>(); if (copiedSelection != null) { var tempGraph = new designGraph(); foreach (node n in copiedSelection.selectedNodes) { if (n is ruleNode) { tempGraph.addNode(n); } else { tempGraph.addNode(new ruleNode(n)); } } foreach (arc a in copiedSelection.selectedArcs) { if (a is ruleArc) { tempGraph.arcs.Add(a); } else { tempGraph.arcs.Add(new ruleArc(a)); } } foreach (hyperarc a in copiedSelection.selectedHyperArcs) { if (a is ruleHyperarc) { tempGraph.addHyperArc(a); } else { tempGraph.addHyperArc(new ruleHyperarc(a)); } } tempGraph.RepairGraphConnections(); foreach (node n in tempGraph.nodes) { var mousePos = Mouse.GetPosition(this); n.X = mousePos.X + n.X - copiedSelection.ReferencePoint.X - Origin.X; n.Y = mousePos.Y + n.Y - copiedSelection.ReferencePoint.Y - Origin.Y; n.name = rW.rule.makeUniqueNodeName(n.name); addNodeShape(n); graph.addNode(n); newSelection.Add((Shape)n.DisplayShape.Shape); if (rW.graphGUIK == this) { AddKNodeToLandR(n); } } foreach (arc a in tempGraph.arcs) { a.name = rW.rule.makeUniqueArcName(a.name); graph.arcs.Add(a); AddArcShape(a); SetUpNewArcShape(a); newSelection.Add((ArcShape)a.DisplayShape.Shape); if (rW.graphGUIK == this) { AddKArcToLandR(a); } } foreach (hyperarc h in tempGraph.hyperarcs) { h.name = rW.rule.makeUniqueHyperarcName(h.name); graph.addHyperArc(h); AddHyperArcShape(h); newSelection.Add((HyperArcShape)h.DisplayShape.Shape); if (rW.graphGUIK == this) { AddKHyperToLandR(h); } } Select(newSelection); } }