def combine_problems(dirname): composite_index = OrderedDict() for indices in combinations: merged_model, best_obj = merge_models([index[i] for i in indices]) name = '-'.join(['{:02}'.format(i + 1) for i in indices]) filename = name + '.mod' path = os.path.join(dirname, filename) with open(path, 'w') as f: ampl.pretty_print(f, merged_model) composite_index[name] = {'best_obj': best_obj, 'path': path} return composite_index
def test_merge_models(): index = util.load_index('casado') stmt, best_obj = util.merge_models([index['casado01'], index['casado03']]) output = StringIO() ampl.pretty_print(output, stmt) assert output.getvalue() == \ """var x1 in [0, 20]; var x2 in [-10, 10]; minimize f: ((exp(-3 * x1) - sin(x1) ^ 3) + 1.0) * ((x2 - x2 ^ 2) ^ 2 + (x2 - 1) ^ 2); """ assert best_obj == 0
def check_print(output, node): "Check if the output of pretty_print for the given AST node." stream = StringIO() ampl.pretty_print(stream, node) real_output = stream.getvalue() assert real_output == output