Ejemplo n.º 1
0
 def test_reader_factory(self):
     """
     Testing the pyomo.opt reader factory
     """
     ReaderFactory.register('rtest3')(MockReader)
     ans = ReaderFactory
     #self.assertEqual(len(ans),4)
     self.assertTrue(set(ans) >= set(["rtest3", "sol", "yaml", "json"]))
Ejemplo n.º 2
0
 def test_reader_instance(self):
     """
     Testing that we get a specific reader instance
     """
     ans = ReaderFactory("none")
     self.assertEqual(ans, None)
     ans = ReaderFactory("sol")
     self.assertEqual(type(ans), ResultsReader_sol)
Ejemplo n.º 3
0
 def test_reader_registration(self):
     """
     Testing methods in the reader factory registration process
     """
     ReaderFactory.unregister('rtest3')
     self.assertTrue(not 'rtest3' in ReaderFactory)
     ReaderFactory.register('rtest3')(MockReader)
     self.assertTrue('rtest3' in ReaderFactory)
Ejemplo n.º 4
0
def read_sol_smap_var(model, sol_filename, smap, suffixes=[".*"]):
    """
    Reads the solution from the SOL file and generates a
    results object with an appropriate symbol map (smap passed as variable!) for
    loading it into the given Pyomo model. By default all
    suffixes found in the NL file will be extracted. This
    can be overridden using the suffixes keyword, which
    should be a list of suffix names or regular expressions
    (or None).
    """
    if suffixes is None:
        suffixes = []

    # Remove possible suffix '.sol' if any, treating sol_filename as dir_sol/modelName, dir_sol/dataName
    if sol_filename.endswith(SOL_EXT):
        sol_filename = sol_filename[:-len(SOL_EXT)]
    # symbol_map_filename = symbol_filename + ".symbol_map.pickle"
    sol_filename = sol_filename + ".sol"

    # parse the SOL file
    with ReaderFactory(ResultsFormat.sol) as reader:
        results = reader(sol_filename, suffixes=suffixes)

    # # regenerate the symbol_map for this model
    # with open(symbol_map_filename, "rb") as f:
    #     symbol_cuid_pairs = pickle.load(f)
    # symbol_map = SymbolMap()
    # symbol_map.addSymbols((cuid.find_component(model), symbol)
    #                       for symbol, cuid in symbol_cuid_pairs)

    # tag the results object with the symbol_map passed as smap
    results._smap = smap

    return results
Ejemplo n.º 5
0
def read_sol(model, sol_filename, symbol_map_filename, suffixes=[".*"]):
    """
    Reads the solution from the SOL file and generates a
    results object with an appropriate symbol map for
    loading it into the given Pyomo model. By default all
    suffixes found in the NL file will be extracted. This
    can be overridden using the suffixes keyword, which
    should be a list of suffix names or regular expressions
    (or None).
    """
    if suffixes is None:
        suffixes = []

    # parse the SOL file
    with ReaderFactory(ResultsFormat.sol) as reader:
        results = reader(sol_filename, suffixes=suffixes)

    # regenerate the symbol_map for this model
    with open(symbol_map_filename, "rb") as f:
        symbol_cuid_pairs = pickle.load(f)
    symbol_map = SymbolMap()
    symbol_map.addSymbols((cuid.find_component(model), symbol)
                          for symbol, cuid in symbol_cuid_pairs)

    # tag the results object with the symbol_map
    results._smap = symbol_map

    return results
Ejemplo n.º 6
0
 def test_iis_no_variable_values(self):
     with ReaderFactory("sol") as reader:
         if reader is None:
             raise IOError("Reader 'sol' is not registered")
         result = reader(join(currdir, "iis_no_variable_values.sol"),
                         suffixes=["iis"])
         soln = result.solution(0)
         self.assertEqual(len(list(soln.variable['v0'].keys())), 1)
         self.assertEqual(soln.variable['v0']['iis'], 1)
         self.assertEqual(len(list(soln.variable['v1'].keys())), 1)
         self.assertEqual(soln.variable['v1']['iis'], 1)
         self.assertEqual(len(list(soln.constraint['c0'].keys())), 1)
         self.assertEqual(soln.constraint['c0']['Iis'], 4)
         import pyomo.kernel as pmo
         m = pmo.block()
         m.v0 = pmo.variable()
         m.v1 = pmo.variable()
         m.c0 = pmo.constraint()
         m.iis = pmo.suffix(direction=pmo.suffix.IMPORT)
         from pyomo.core.expr.symbol_map import SymbolMap
         soln.symbol_map = SymbolMap()
         soln.symbol_map.addSymbol(m.v0, 'v0')
         soln.symbol_map.addSymbol(m.v1, 'v1')
         soln.symbol_map.addSymbol(m.c0, 'c0')
         m.load_solution(soln)
         pmo.pprint(m.iis)
         self.assertEqual(m.iis[m.v0], 1)
         self.assertEqual(m.iis[m.v1], 1)
         self.assertEqual(m.iis[m.c0], 4)
Ejemplo n.º 7
0
 def test_infeasible2(self):
     with ReaderFactory("sol") as reader:
         if reader is None:
             raise IOError("Reader 'sol' is not registered")
         soln = reader(join(currdir, "infeasible2.sol"))
         self.assertEqual(soln.solver.termination_condition,
                          TerminationCondition.infeasible)
         self.assertEqual(soln.solution.status, SolutionStatus.infeasible)
         self.assertEqual(soln.solver.status, SolverStatus.warning)
Ejemplo n.º 8
0
def hack_Solve(stab_file, Gr, symbol_map, nls):
    hackStab(stab_file + '.nl', Gr, nls)
    subprocess.check_call(co.SolverName + ' ' + stab_file + ".nl -AMPL" +
                          " \"option_file_name=" + tmpFileDir +
                          "peipopt.opt\"",
                          shell=True)
    with ReaderFactory(ResultsFormat.sol) as reader:
        results = reader(stab_file + ".sol")
    results._smap = symbol_map
    return results
Ejemplo n.º 9
0
 def test_conopt_optimal(self):
     with ReaderFactory("sol") as reader:
         if reader is None:
             raise IOError("Reader 'sol' is not registered")
         soln = reader(join(currdir, "conopt_optimal.sol"))
         self.assertEqual(soln.solver.termination_condition,
                          TerminationCondition.optimal)
         self.assertEqual(soln.solution.status, SolutionStatus.optimal)
         self.assertEqual(soln.solver.status, SolverStatus.ok)
         self.assertTrue(check_optimal_termination(soln))
         assert_optimal_termination(soln)
Ejemplo n.º 10
0
 def test_factory(self):
     with ReaderFactory("sol") as reader:
         if reader is None:
             raise IOError("Reader 'sol' is not registered")
         soln = reader(join(currdir, "test4_sol.sol"), suffixes=["dual"])
         _test = TempfileManager.create_tempfile('factory.txt')
         soln.write(filename=_test, format='json')
         with open(_test, 'r') as out, \
             open(join(currdir, "test4_sol.jsn"), 'r') as txt:
             self.assertStructuredAlmostEqual(json.load(txt),
                                              json.load(out),
                                              allow_second_superset=True)
Ejemplo n.º 11
0
    def test_infeasible1(self):
        with ReaderFactory("sol") as reader:
            if reader is None:
                raise IOError("Reader 'sol' is not registered")
            soln = reader(join(currdir, "infeasible1.sol"))
            self.assertEqual(soln.solver.termination_condition,
                             TerminationCondition.infeasible)
            self.assertEqual(soln.solution.status, SolutionStatus.infeasible)
            self.assertEqual(soln.solver.status, SolverStatus.warning)

            self.assertFalse(check_optimal_termination(soln))

            with self.assertRaises(RuntimeError):
                assert_optimal_termination(soln)
Ejemplo n.º 12
0
def makeStab_solve(stab_file, Gr, nls, nlEND, p_1_NoR, NoR, symbol_map):
    nl_mu = open(stab_file + ".nl", "w")  # Мастерим стаб для mu=1
    nl_mu.write(nls[0][0:p_1_NoR])  # до 1/sum_mu1
    sum_mu1 = sum(Gr.mu[s]() for s in Gr.F[0].sR)
    nl_mu.write("n" + str(1. / sum_mu1) + "\no54\n" + str(sum_mu1) + "\n")
    for i in range(1, NoR + 1):  # цикл по интерполяции
        if Gr.mu[i - 1]:
            # nl_mu.write("o2\nn1\no5\no54\n4\no0")
            nl_mu.write("o2\nn1\no5\n")
            nl_mu.write(nls[i])
    nl_mu.write(nlEND[1])  # остатки
    nl_mu.close()

    subprocess.check_call('ipopt -s ' + stab_file + ".nl", shell=True)

    with ReaderFactory(ResultsFormat.sol) as reader:
        results = reader(stab_file + ".sol")
    results._smap = symbol_map

    if results.solver.termination_condition != TerminationCondition.optimal:
        raise RuntimeError("Solver did not terminate with status = optimal")
    return results
Ejemplo n.º 13
0
def EXTERNAL_invoke_solve(worker,
                          working_directory,
                          subproblem_type,
                          logfile,
                          problem_list_filename,
                          executable,
                          output_solver_log,
                          io_options,
                          command_line_options,
                          options_filename,
                          suffixes=None):
    assert os.path.exists(working_directory)
    if suffixes is None:
        suffixes = [".*"]

    #
    # Write the NL files for the subproblems local to
    # this worker
    #

    filedata = {}
    write_time = {}
    load_function = None
    if subproblem_type == 'bundles':
        assert worker.scenario_tree.contains_bundles()
        load_function = worker._process_bundle_solve_result
        for bundle in worker.scenario_tree.bundles:
            start = time.time()
            filedata[bundle.name] = _write_bundle_nl(
                worker,
                bundle,
                working_directory,
                io_options)
            stop = time.time()
            write_time[bundle.name] = stop - start
    else:
        assert subproblem_type == 'scenarios'
        load_function = worker._process_scenario_solve_result
        for scenario in worker.scenario_tree.scenarios:
            start = time.time()
            filedata[scenario.name] = _write_scenario_nl(
                worker,
                scenario,
                working_directory,
                io_options)
            stop = time.time()
            write_time[scenario.name] = stop - start
    assert load_function is not None
    assert len(filedata) > 0

    args = []
    args.append(problem_list_filename)
    args.append("-AMPL")
    args.append("use_problem_file=yes")
    args.append("option_file_name="+options_filename)
    root_node = worker.scenario_tree.findRootNode()
    if hasattr(worker, "MPI"):
        args.append("mpi_spawn_mode=yes")
        if worker.mpi_comm_tree[root_node.name].rank == 0:
            args.append("output_file="+str(logfile))

    for key, val in command_line_options:
        key = key.strip()
        if key == "use_problem_file":
            raise ValueError(
                "Use of the 'use_problem_file' command-line "
                "option is disallowed.")
        elif key == "mpi_spawn_mode":
            raise ValueError(
                "Use of the 'mpi_spawn_mode' command-line "
                "option is disallowed.")
        elif key == "option_file_name":
            raise ValueError(
                "Use of the 'option_file_name' command-line "
                "option is disallowed.")
        elif key == "output_file":
            raise ValueError(
                "Use of the 'output_file' command-line "
                "option is disallowed.")
        elif key == '-AMPL':
            raise ValueError(
                "Use of the '-AMPL' command-line "
                "option is disallowed.")
        else:
            args.append(key+"="+str(val))

    start = time.time()
    if hasattr(worker, "MPI"):
        currdir = os.getcwd()
        try:
            os.chdir(working_directory)
            spawn = worker.mpi_comm_tree[root_node.name].Spawn(
                executable,
                args=args,
                maxprocs=worker.mpi_comm_tree[root_node.name].size)
            rc = None
            if worker.mpi_comm_tree[root_node.name].rank == 0:
                rc = array.array("i", [0])
                spawn.Reduce(sendbuf=None,
                             recvbuf=[rc, worker.MPI.INT],
                             op=worker.MPI.SUM,
                             root=worker.MPI.ROOT)
            rc = worker.mpi_comm_tree[root_node.name].bcast(rc, root=0)
            spawn.Disconnect()
            assert len(rc) == 1
            rc = rc[0]
        finally:
            os.chdir(currdir)
    else:
        rc, msg = pyutilib.subprocess.run(
            [executable]+args,
            cwd=working_directory,
            outfile=logfile,
            tee=output_solver_log)
    assert rc == 0, str(msg)

    stop = time.time()
    solve_time = stop - start

    #
    # Parse the SOL files for the subproblems local to
    # this worker and load the results
    #
    worker_results = {}
    with ReaderFactory(ResultsFormat.sol) as reader:
        for object_name in filedata:
            start = time.time()
            nl_filename, symbol_map = filedata[object_name]
            assert nl_filename.endswith(".nl")
            sol_filename = nl_filename[:-2]+"sol"
            results = reader(sol_filename, suffixes=suffixes)
            stop = time.time()
            # tag the results object with the symbol_map
            results._smap = symbol_map
            results.solver.time = solve_time
            results.pyomo_solve_time = (stop - start) + \
                                       solve_time + \
                                       write_time[object_name]
            if str(results.solver.termination_condition) == "infeasible":
                if len(results.solution) > 0:
                    results.solution.clear()
            # TODO: Re-architect ScenarioTreeManagerSolver
            #       to better support this
            worker_results[object_name] = \
                load_function(object_name, results)

    return worker_results
Ejemplo n.º 14
0
 def tearDown(self):
     TempfileManager.clear_tempfiles()
     ReaderFactory.unregister('rtest3')
     ReaderFactory.unregister('stest3')
     ReaderFactory.unregister('wtest3')
Ejemplo n.º 15
0
s.load_d_s(s.PlantSample)

s.ipopt.solve(s.SteadyRef, keepfiles=True)
finame = s.ipopt._soln_file
cwd = os.getcwd()
filename = "/home/dav0/nmpc_mhe_q/testing/ref_ss.sol"
# copyfile(finame, cwd + "/ref_ss.sol")
with open("file_a", "w") as file:
    for var in s.SteadyRef.component_data_objects(Var):
        var.set_value(0)
        val = var.value
        file.write(str(val))
        file.write('\n')
    file.close()
reader = ReaderFactory(ResultsFormat.sol)
results = reader(filename)
_, smapid = s.SteadyRef.write("whathevs.nl", format=ProblemFormat.nl)
smap = s.SteadyRef.solutions.symbol_map[smapid]
results._smap = smap
s.SteadyRef.solutions.load_from(results)
with open("file_b", "w") as file:
    for var in s.SteadyRef.component_data_objects(Var):
        val = var.value
        file.write(str(val))
        file.write('\n')
    file.close()

s.ipopt.solve(s.SteadyRef, tee=True, load_solutions=False, report_timing=True)
s.param_writer(s.SteadyRef, "gimmemyparams.json")
with open("params_a", "w") as file:
Ejemplo n.º 16
0
 def tearDown(self):
     ReaderFactory.unregister('rtest3')
     ReaderFactory.unregister('stest3')
     ReaderFactory.unregister('wtest3')
Ejemplo n.º 17
0
 def test_bad_objno(self):
     with ReaderFactory("sol") as reader:
         if reader is None:
             raise IOError("Reader 'sol' is not registered")
         with self.assertRaises(ValueError):
             soln = reader(join(currdir, "bad_objno.sol"))
Ejemplo n.º 18
0
def solveNlFileS(sym_maps, __peProblems, tmpFileDir, RunMo):
    if RunMo == 'S':  # RUN dist    #===== solve in parallel ===========
        theSession = SsopSession(
            name=co.TaskName,
            token=co.token,
            resources=[
                ssop_config.SSOP_RESOURCES["pool-scip-ipopt"]
                #ssop_config.SSOP_RESOURCES["hse"],
                #ssop_config.SSOP_RESOURCES["vvvolhome2"],
                #ssop_config.SSOP_RESOURCES["vvvoldell"],
                #ssop_config.SSOP_RESOURCES["ui4.kiae.vvvol"],
                #ssop_config.SSOP_RESOURCES["govorun.vvvol"],
                #ssop_config.SSOP_RESOURCES["vvvolhome"]
            ],
            workdir=tmpFileDir,
            debug=False)
        #            optFile = 'peipopt.opt'
        #           makeIpoptOptionsFile(co.tmpFileDir,  optFile)
        #print (__peProblems )
        solved, unsolved, jobId = theSession.runJob(
            __peProblems, co.optFile)  # by default solver = "ipopt"
        #            print("solved:   ", solved)
        print("unsolved: ", unsolved)
        print("Job %s is finished" % (jobId))

        #            theSession.deleteWorkFiles([".nl", ".sol", ".zip", ".plan"])

        # Delete jobs created to save disk space at Everest server , MAY BE
        #     if args.cleanjobs:
        #            theSession.deleteAllJobs()

        # CLOSE THE SESSION !!! MUST BE
        theSession.session.close()

    else:
        for pName in __peProblems:  # RUN Nl local
            print("solve", pName + ".nl")
            #            subprocess.check_call('ipopt3_11_1 -s ' + pName+".nl"+" \"option_file_name=peipopt.opt\"", shell=True)
            subprocess.check_call(
                co.SolverName + ' ' + tmpFileDir + '/' + pName + ".nl -AMPL" +
                " \"option_file_name=" + tmpFileDir + "peipopt.opt\"",
                shell=True)
    #def gatherNlFileS ( __peProblems, sym_maps ) :
    resultss = []
    for k, pName in enumerate(__peProblems):  #
        #            print (k, "gather", tmpFileDir + "/" +pName+".sol")
        with ReaderFactory(ResultsFormat.sol) as reader:
            results = reader(tmpFileDir + "/" + pName + ".sol")
        results._smap = sym_maps[k]
        if results.solver.termination_condition != TerminationCondition.optimal:
            print("results.solver.Message: ", results.solver.Message)
            print("TermCond:", results.solver.termination_condition, '\n')
            if (results.solver.Message.find(
                    'Search Direction becomes Too Small') >= 0
                    or results.solver.Message.find(
                        'Maximum Number of Iterations Exceeded') >= 0):
                results.solver.termination_condition = TerminationCondition.optimal
                results.solver.status = pyomo.opt.SolverStatus.warning
            else:
                raise RuntimeError(
                    "Solver did not terminate with status = optimal")
        resultss.append(results)
    if RunMo == 'S':
        theSession.deleteWorkFiles([".nl", ".sol", ".zip", ".plan"])
    return resultss