1
0
Fork 0

Fixed bug with constraint optimization

master
Luca Moretti 4 years ago
parent 9e4d6b9fe5
commit c1ce2de331
  1. 27
      main_package/classes/optimizers/constraint_based_optimizer.py
  2. 6
      main_package/classes/structure_graph/sample_path.py
  3. 4
      main_package/tests/estimators/test_structure_constraint_based_estimator.py

@ -66,36 +66,17 @@ class ConstraintBasedOptimizer(Optimizer):
child_states_numb = self.structure_estimator.sample_path.structure.get_states_number(self.node_id) child_states_numb = self.structure_estimator.sample_path.structure.get_states_number(self.node_id)
b = 0 b = 0
while b < len(u): while b < len(u):
#for parent_id in u:
parent_indx = 0 parent_indx = 0
list_parent= copy.deepcopy(u) while parent_indx < len(u):
for possible_parent in list_parent:
removed = False removed = False
#if not list(self.structure_estimator.generate_possible_sub_sets_of_size(u, b, u[parent_indx])): S = self.structure_estimator.generate_possible_sub_sets_of_size(u, b, u[parent_indx])
#break test_parent = u[parent_indx]
S = self.structure_estimator.generate_possible_sub_sets_of_size(u, b, possible_parent)
#print("U Set", u)
#print("S", S)
test_parent = possible_parent
#print("Test Parent", test_parent)
for parents_set in S: for parents_set in S:
#print("Parent Set", parents_set)
#print("Test Parent", test_parent)
if self.structure_estimator.complete_test(test_parent, self.node_id, parents_set, child_states_numb, self.tot_vars_count): if self.structure_estimator.complete_test(test_parent, self.node_id, parents_set, child_states_numb, self.tot_vars_count):
#print("Removing EDGE:", test_parent, self.node_id)
graph.remove_edges([(test_parent, self.node_id)]) graph.remove_edges([(test_parent, self.node_id)])
other_nodes.remove(test_parent) u.remove(test_parent)
print(f"TEST PARENT: {test_parent}")
try:
u.remove(test_parent)
except:
print(f"u: {u}")
print(f"utest_parent: {test_parent}")
removed = True removed = True
break break
#else:
#parent_indx += 1
if not removed: if not removed:
parent_indx += 1 parent_indx += 1
b += 1 b += 1

@ -45,11 +45,11 @@ class SamplePath(asam.AbstractSamplePath):
self.importer.import_data() self.importer.import_data()
#TODO: VALUTARE PARAMETRO PER DATA AUGMENTATION #TODO: VALUTARE PARAMETRO PER DATA AUGMENTATION
trajects_samples = pd.concat([self.importer.concatenated_samples, #trajects_samples = pd.concat([self.importer.concatenated_samples,
self.importer.concatenated_samples]) # self.importer.concatenated_samples])
self._trajectories = \ self._trajectories = \
tr.Trajectory(self.importer.build_list_of_samples_array(trajects_samples), tr.Trajectory(self.importer.build_list_of_samples_array(self.importer.concatenated_samples),
len(self.importer.sorter) + 1) len(self.importer.sorter) + 1)
#self.trajectories.append(trajectory) #self.trajectories.append(trajectory)
self.importer.clear_concatenated_frame() self.importer.clear_concatenated_frame()

@ -22,7 +22,7 @@ class TestStructureConstraintBasedEstimator(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
#cls.read_files = glob.glob(os.path.join('../../data', "*.json")) #cls.read_files = glob.glob(os.path.join('../../data', "*.json"))
cls.importer = ji.JsonImporter("../../data/networks_and_trajectories_ternary_data_01_6.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name') cls.importer = ji.JsonImporter("../../data/networks_and_trajectories_ternary_data_15.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
cls.s1 = sp.SamplePath(cls.importer) cls.s1 = sp.SamplePath(cls.importer)
cls.s1.build_trajectories() cls.s1.build_trajectories()
cls.s1.build_structure() cls.s1.build_structure()
@ -32,7 +32,7 @@ class TestStructureConstraintBasedEstimator(unittest.TestCase):
true_edges = set(map(tuple, true_edges)) true_edges = set(map(tuple, true_edges))
se1 = se.StructureConstraintBasedEstimator(self.s1,0.1,0.1) se1 = se.StructureConstraintBasedEstimator(self.s1,0.1,0.1)
edges = se1.estimate_structure(disable_multiprocessing=True) edges = se1.estimate_structure(disable_multiprocessing=False)
self.assertEqual(edges, true_edges) self.assertEqual(edges, true_edges)