diff --git a/src/gboml/compiler/classes/factor.py b/src/gboml/compiler/classes/factor.py index 92e3454c65e40faa214a40a13eae94d518f0f6b4..fb85200d4db2cbb1a85837154a54e9ad05fb5850 100644 --- a/src/gboml/compiler/classes/factor.py +++ b/src/gboml/compiler/classes/factor.py @@ -566,6 +566,8 @@ class Factorize: b_expr = self.get_indep_expr().turn_to_python_expression() sign = constraint.get_sign() self.extension_type = sign + bit_array = ByteArray(1) + gl["extension_range"] = bit_array time_range = constraint.get_time_range(gl) name_index = constraint.get_index_var() condition = constraint.get_condition() @@ -621,7 +623,7 @@ class Factorize: + " at line " + str(constraint.get_line()) + " has a time range ill-defined as a " "variable goes out of bounds for " - + str(name_index) + " equals " + str(numpy_range[not bit_array.array])) + + str(name_index) + " equals " + str(numpy_range[~bit_array.array])) all_values.append(coef_values) all_columns.append(offset + index) previous_child_values = np.array([[]]) @@ -690,7 +692,7 @@ class Factorize: if not bit_array.array.any(): print("Warning constraint : %s at line %s is ignored for %s equal to %s" % (str(constraint), str(constraint.get_line()), str(name_index), - str(numpy_range[not bit_array.array]))) + str(numpy_range[~bit_array.array]))) if nb_valid_indexes == 0: self.sparse = None @@ -717,7 +719,9 @@ class Factorize: elif self.type_fact == "objective": objective = self.obj obj_expr = objective.get_expression() - obj_range = objective.get_time_range(definitions) + bit_array = ByteArray(1) + gl["extension_range"] = bit_array + obj_range = objective.get_time_range(gl) name_index = objective.get_index_var() obj_type = objective.get_type() self.extension_type = obj_type @@ -767,7 +771,7 @@ class Factorize: + " at line " + str(objective.get_line()) + " has a time range ill-defined as a " "variable goes out of bounds for " - + str(name_index) + " equals " + str(numpy_range[not bit_array.array])) + + str(name_index) + " equals " + str(numpy_range[~bit_array.array])) all_values.append(coef_values) all_columns.append(offset + index) previous_child_values = np.array([[]]) @@ -832,7 +836,7 @@ class Factorize: if not bit_array.array.any(): print("Warning constraint : %s at line %s is ignored for %s equal to %s" % (str(objective), str(objective.get_line()), str(name_index), - str(numpy_range[not bit_array.array]))) + str(numpy_range[~bit_array.array]))) if nb_valid_indexes == 0 or len(values_flat_array) == 0: self.sparse = None diff --git a/src/gboml/compiler/classes/time_obj.py b/src/gboml/compiler/classes/time_obj.py index b163b80cdae747d735842220c55d43a3e8c31b9b..74280efd09413dcc91bc2b395734acf2f9ed2244 100644 --- a/src/gboml/compiler/classes/time_obj.py +++ b/src/gboml/compiler/classes/time_obj.py @@ -158,6 +158,7 @@ class TimeInterval: return range(begin_value, end_value + 1, step_value) else: + begin_value = eval(self.begin.turn_to_python_expression(), definitions, {}) end_value = eval(self.end.turn_to_python_expression(), @@ -291,8 +292,9 @@ class TimeInterval: value = int(value) - #elif isinstance(value, np.ndarray): - # value = np.array(value, dtype=np.int64) + elif isinstance(value, np.ndarray): + if value.size == 1: + value = int(value) return value diff --git a/src/gboml/gboml_main.py b/src/gboml/gboml_main.py index 911c1eee6ed844c13dfca37d9c11af61660727e0..e6ce722f9660e6b465583c30084a2e80314d793e 100644 --- a/src/gboml/gboml_main.py +++ b/src/gboml/gboml_main.py @@ -135,8 +135,9 @@ def main(): scipy_solver(A_eq, b_eq, A_ineq, b_ineq, C_sum, objective_offset, name_tuples) elif args.clp: - x, objective, status, solver_info = \ - clp_solver(A_eq, b_eq, A_ineq, b_ineq, C_sum, objective_offset, name_tuples) + x, objective, status, solver_info, constraints_additional_information = \ + clp_solver(A_eq, b_eq, A_ineq, b_ineq, C_sum, objective_offset, name_tuples, + details=args.detailed) elif args.cbc: x, objective, status, solver_info = \ diff --git a/src/gboml/solver_api/clp_solver.py b/src/gboml/solver_api/clp_solver.py index b614653771a986389836d383ba7c3a6a238c1c21..859c9447014a5209b5a7e80b0e67c19aee22d19f 100644 --- a/src/gboml/solver_api/clp_solver.py +++ b/src/gboml/solver_api/clp_solver.py @@ -30,7 +30,8 @@ def clp_solver(matrix_a_eq: coo_matrix, vector_b_eq: np.ndarray, matrix_a_ineq: coo_matrix, vector_b_ineq: np.ndarray, vector_c: np.ndarray, objective_offset: float, - name_tuples: list) -> tuple: + name_tuples: list, + details=False) -> tuple: """clp_solver takes as input the matrix A, the vectors b and c. It returns the @@ -130,9 +131,16 @@ def clp_solver(matrix_a_eq: coo_matrix, vector_b_eq: np.ndarray, solver = s solution = None objective = None + constraints_additional_info_eq = dict() + constraints_additional_info_ineq = dict() + if status_code == "solution": status = "optimal" solution = solver.primalVariableSolution['variables'] + if details: + dual = solver.dualVariableSolution['variables'] + constraints_additional_info_ineq["dual"] = dual[:matrix_a_ineq.shape[0]].tolist() + constraints_additional_info_eq["dual"] = dual[matrix_a_ineq.shape[0]:].tolist() # if we added an additional variable artificially # we remove it @@ -151,4 +159,7 @@ def clp_solver(matrix_a_eq: coo_matrix, vector_b_eq: np.ndarray, else: status = "unknown" - return solution, objective, status, solver_info + + constraints_additional_information = {"eq": constraints_additional_info_eq, + "ineq": constraints_additional_info_ineq} + return solution, objective, status, solver_info, constraints_additional_information diff --git a/test.py b/test.py index 6831a0eecf19a8dbbc179ae9c3f13d9ad6bbcd2f..37feeb3f1dc521e9bc68c0e8e846c1a01a77a0c1 100644 --- a/test.py +++ b/test.py @@ -520,6 +520,18 @@ class CompilerTests(unittest.TestCase): return_code = process.returncode self.assertNotEqual(return_code, 0) + def test_array_in_for(self): + """ test_array_in_for + + Tests a simple file with an array in for + + """ + process = subprocess.run(['gboml', 'test/test_33.txt'], + stdout=subprocess.PIPE, + universal_newlines=True) + return_code = process.returncode + self.assertEqual(return_code, 0) + def test_sum_objective(self): """ test_sum_objective diff --git a/test/test_33.txt b/test/test_33.txt new file mode 100644 index 0000000000000000000000000000000000000000..6fd16379be0004027176a312931d090157d2a410 --- /dev/null +++ b/test/test_33.txt @@ -0,0 +1,9 @@ +#NODE A +#PARAMETERS +parameter = {3, 2, 3}; +#VARIABLES +internal : x[12]; +#CONSTRAINTS +hi: x[i] == 1 for i in [0:parameter[0]]; +#OBJECTIVES +min: x[i] for i in [0:parameter[0]]; \ No newline at end of file