From c3badd68d3abf86c505f412b577eaa69d6e82426 Mon Sep 17 00:00:00 2001
From: Anne-Liza <a.m.r.m.bruggeman@student.tudelft.nl>
Date: Thu, 23 Nov 2017 18:02:18 +0100
Subject: [PATCH] Bug fix in get_possible_function_order()

Former-commit-id: 116178d6d0cd8dc7206d16a597969aa55f0a9024
---
 kadmos/graph/graph_data.py | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/kadmos/graph/graph_data.py b/kadmos/graph/graph_data.py
index 1fc0bff7f..84a681946 100644
--- a/kadmos/graph/graph_data.py
+++ b/kadmos/graph/graph_data.py
@@ -31,6 +31,8 @@ logger = logging.getLogger(__name__)
 
 class DataGraph(KadmosGraph):
 
+    OPTIONS_FUNCTION_ORDER_METHOD = ['manual', 'minimum feedback']
+
     def __init__(self, *args, **kwargs):
         super(DataGraph, self).__init__(*args, **kwargs)
 
@@ -376,7 +378,6 @@ class DataGraph(KadmosGraph):
             graph = self.cleancopy()
         nodes_to_remove = list()
         # TODO: Consider using the check function
-        assert not graph.find_all_nodes(subcategory='all problematic variables'), 'Graph still has problematic variables.'
         nodes_to_remove.extend(graph.find_all_nodes(subcategory='all inputs'))
         nodes_to_remove.extend(graph.find_all_nodes(subcategory='all outputs'))
         graph.remove_nodes_from(nodes_to_remove)
@@ -410,19 +411,27 @@ class DataGraph(KadmosGraph):
 
         return coupling_matrix
 
-    def get_possible_function_order(self, method, multi_start=None):
+    def get_possible_function_order(self, method, multi_start=None, check_graph=False):
         """ Method to find a possible function order, in the order: pre-coupled, coupled, post-coupled functions
 
         :param method: algorithm which will be used to minimize the feedback loops
         :type method: str
         :param multi_start: start the algorithm from multiple starting points
         :type multi_start: int
+        :param check_graph: check whether graph has problematic variables
+        :type check_graph: bool
         :return Possible function order
         :rtype list
         """
 
+        # Input assertions
+        if check_graph:
+            assert not self.find_all_nodes(subcategory='all problematic variables'), \
+                'Graph still has problematic variables.'
+
         # Get function graph
         function_graph = self.get_function_graph()
+        function_graph.remove_edges_from(function_graph.selfloop_edges())
 
         # Add a super node in which the coupled functions will be merged
         function_graph.add_node('super_node', category='function')
@@ -439,13 +448,14 @@ class DataGraph(KadmosGraph):
                 cycle = nx.find_cycle(function_graph, orientation='original')
 
             # Find the functions in the cycle
-            functions_in_cycle = list(set(function_id for edges in cycle for function_id in edges))
+            functions_in_cycle = set()
+            functions_in_cycle.update(function_id for edges in cycle for function_id in edges)
+            functions_in_cycle = list(functions_in_cycle)
 
             # Merge the coupled functions in the super node
             for function_id in functions_in_cycle:
                 if function_id != 'reverse' and function_id != 'super_node':
                     coupled_functions.append(function_id)
-                    coupled_functions = list(set(coupled_functions)) # TODO: Quick fix to avoid double entries...
                     function_graph = nx.contracted_nodes(function_graph, 'super_node', function_id)
                     function_graph.remove_edges_from(function_graph.selfloop_edges())
 
@@ -1714,8 +1724,6 @@ class RepositoryConnectivityGraph(DataGraph):
 
 class FundamentalProblemGraph(DataGraph, KeChainMixin):
 
-    OPTIONS_FUNCTION_ORDER_METHOD = ['manual', 'minimum feedback']
-
     def __init__(self, *args, **kwargs):
         super(FundamentalProblemGraph, self).__init__(*args, **kwargs)
 
@@ -2165,6 +2173,10 @@ class FundamentalProblemGraph(DataGraph, KeChainMixin):
 
         logger.info('Adding function problem roles...')
 
+        # Input assertions
+        assert not self.find_all_nodes(subcategory='all problematic variables'), \
+            'Problem roles could not be determined. Graph still has problematic variables.'
+
         # Determine and check function ordering method
         assert function_order_method in self.OPTIONS_FUNCTION_ORDER_METHOD
         if function_order_method == 'manual':
-- 
GitLab