def read_solution(ampl_filename, sol_filename): """ Read the solution of the model *ampl_filename* from *sol_filename* and returns the objective value. """ sol = Solution() dirname, filename = os.path.split(ampl_filename) with AMPL(dirname) as ampl: ampl.eval('model "{}";'.format(ampl_filename)) ampl.eval('solution "{}";'.format(sol_filename)) try: sol.obj = float(ampl.eval_expr('_obj[1]')) except AMPLError as e: sol.obj = 'nan' sol.obj_error = str(e) sol.solve_result = ampl.eval_expr('solve_result') sol.solve_message = ampl.eval_expr('solve_message') return sol
def test_ampl_cwd(): ampl = util.AMPL() assert ampl.cwd is None dirname = tempfile.mkdtemp() try: with util.AMPL(dirname) as ampl: assert dirname in ampl.eval('cd;')[0][1] finally: os.rmdir(dirname)
def test_ampl(): with util.AMPL() as ampl: assert ampl.eval('print 42;') == [('print', '42\n')] assert ampl.eval_expr(42) == 42