Esempio n. 1
0
def GLOA_with_disjunctive_bounds(pyomo_model):
    job_result = _JobResult()
    pyomo_results = SolverFactory('gdpopt').solve(
        pyomo_model,
        tee=True,
        strategy='GLOA',
        mip_solver='gams',
        mip_solver_args=dict(solver='cplex',
                             add_options=get_base_gams_options_list()),
        nlp_solver='gams',
        nlp_solver_args=dict(solver='baron',
                             add_options=get_base_gams_options_list()),
        minlp_solver='gams',
        minlp_solver_args=dict(solver='baron',
                               add_options=get_base_gams_options_list()),
        iterlim=300,
        calc_disjunctive_bounds=True,
        time_limit=options.time_limit)
    job_result.solver_run_time = pyomo_results.solver.timing.total
    job_result.pyomo_solver_status = pyomo_results.solver.status
    job_result.iterations = pyomo_results.solver.iterations
    job_result.termination_condition = pyomo_results.solver.termination_condition
    job_result.LB = pyomo_results.problem.lower_bound
    job_result.UB = pyomo_results.problem.upper_bound
    return job_result
Esempio n. 2
0
def DICOPT(pyomo_model):
    job_result = _JobResult()
    try:
        pyomo_results = SolverFactory('gams').solve(
            pyomo_model,
            tee=True,
            # keepfiles=True,
            solver='dicopt',
            add_options=get_base_gams_options_list() +
            [f'option reslim={options.time_limit};'])
    except ValueError as e:
        # Handle GAMS interface complaining about using DICOPT for MIPs
        if 'GAMS writer passed solver (dicopt) unsuitable for model type (mip)' in str(
                e):
            pyomo_results = SolverFactory('gams').solve(
                pyomo_model,
                tee=True,
                solver='cplex',
                add_options=get_base_gams_options_list() +
                [f'option reslim={options.time_limit};'])
        else:
            raise
    job_result.solver_run_time = pyomo_results.solver.user_time
    job_result.pyomo_solver_status = pyomo_results.solver.status
    job_result.termination_condition = pyomo_results.solver.termination_condition
    job_result.LB = pyomo_results.problem.lower_bound
    job_result.UB = pyomo_results.problem.upper_bound
    return job_result
Esempio n. 3
0
def _get_job_result(run_dir: Path, model: str, solver: str):
    with run_dir.joinpath(solver, model,
                          job_result_filename).open('r') as result_file:
        _stored_result = yaml.safe_load(result_file)
    if not _stored_result:
        return _JobResult()
    job_result = _JobResult(**_stored_result)
    if 'termination_condition' in job_result:
        if job_result.termination_condition not in [None, "None"]:
            job_result.termination_condition = pyomo_tc(
                job_result.termination_condition)
        else:
            job_result.termination_condition = pyomo_tc('unknown')
    if 'pyomo_solver_status' in job_result:
        job_result.pyomo_solver_status = SolverStatus(
            job_result.pyomo_solver_status)
    return job_result
Esempio n. 4
0
def SCIP(pyomo_model):
    job_result = _JobResult()
    pyomo_results = SolverFactory('gams').solve(
        pyomo_model,
        tee=True,
        # keepfiles=True,
        solver='scip',
        add_options=get_base_gams_options_list() +
        [f'option reslim={options.time_limit};'])
    job_result.solver_run_time = pyomo_results.solver.user_time
    job_result.pyomo_solver_status = pyomo_results.solver.status
    job_result.termination_condition = pyomo_results.solver.termination_condition
    job_result.LB = pyomo_results.problem.lower_bound
    job_result.UB = pyomo_results.problem.upper_bound
    return job_result
Esempio n. 5
0
def OA(pyomo_model):
    job_result = _JobResult()
    pyomo_results = SolverFactory('mindtpy').solve(
        pyomo_model,
        strategy='OA',
        tee=True,
        mip_solver='gams',
        mip_solver_args=dict(solver='cplex',
                             add_options=get_base_gams_options_list()),
        nlp_solver='ipopt',
        # nlp_solver_args=dict(solver='ipopth', add_options=get_base_gams_options_list()),
        iteration_limit=300,
        time_limit=options.time_limit)
    job_result.solver_run_time = pyomo_results.solver.timing.total
    job_result.pyomo_solver_status = pyomo_results.solver.status
    job_result.iterations = pyomo_results.solver.iterations
    job_result.termination_condition = pyomo_results.solver.termination_condition
    job_result.LB = pyomo_results.problem.lower_bound
    job_result.UB = pyomo_results.problem.upper_bound
    return job_result
Esempio n. 6
0
def run_test_case():
    # Load test job configuration
    with open(runner_config_filename) as file:
        runner_options = yaml.safe_load(file)
        model_name = runner_options["model name"]
        solver_name = runner_options["solver name"]
        # Time limit must be updated before solver library import.
        time_limit = runner_options["time_limit"]
        options.time_limit = time_limit
    # Get model and solver objects
    from pysperf.model_library import models
    from pysperf.solver_library import solvers
    test_model = models[model_name]
    test_solver = solvers[solver_name]
    job_result = _JobResult()
    # Build the model
    job_result.model_build_start_time = get_formatted_time_now()
    pyomo_model = test_model.build_function()
    job_result.model_build_end_time = get_formatted_time_now()
    Path(job_model_built_filename).touch()
    # Run the solver
    job_result.solver_start_time = get_formatted_time_now()
    solve_result = test_solver.solve_function(pyomo_model)
    job_result.solver_end_time = get_formatted_time_now()
    Path(job_solve_done_filename).touch()
    # Update results object
    job_result.update(solve_result)
    # Write result to file
    with open(job_result_filename, 'w') as result_file:
        if 'termination_condition' in job_result:
            job_result.termination_condition = str(
                job_result.termination_condition)
        if 'pyomo_solver_status' in job_result:
            job_result.pyomo_solver_status = str(
                job_result.pyomo_solver_status)
        yaml.safe_dump(dict(**job_result), result_file)
Esempio n. 7
0
def create_paver_tracefile(run_number: Optional[int] = None):
    this_run_dir = get_run_dir(run_number)
    _load_run_config(this_run_dir)
    # Create trace file
    trace_header = """\
        * Trace Record Definition
        * GamsSolve
        * InputFileName,ModelType,SolverName,NLP,MIP,JulianDate,Direction
        *  ,NumberOfEquations,NumberOfVariables,NumberOfDiscreteVariables
        *  ,NumberOfNonZeros,NumberOfNonlinearNonZeros,OptionFile
        *  ,ModelStatus,SolverStatus,ObjectiveValue,ObjectiveValueEstimate
        *  ,SolverTime,NumberOfIterations,NumberOfDomainViolations,NumberOfNodes,#empty1
        """
    trace_data = []
    for model_name, solver_name in this_run_config.jobs_run - this_run_config.jobs_failed:
        with this_run_dir.joinpath(
                solver_name, model_name,
                "pysperf_result.log").open('r') as resultfile:
            job_result = _JobResult(**yaml.safe_load(resultfile))
        _validate_job_result(job_result)
        test_model = models[model_name]
        test_solver = solvers[solver_name]
        trace_line = [
            model_name,  # Model Name
            'MINLP',  # LP, MIP, NLP, etc.
            solver_name,  # ...
            test_solver.nlp,  # default NLP solver
            test_solver.milp,  # default MIP solver
            get_julian_datetime(
                datetime.strptime(job_result.model_build_start_time,
                                  time_format)),  # start day/time of job
            0 if test_model.objective_sense == "minimize" else
            1,  # direction 0=min, 1=max
            test_model.constraints,  # total number of equations
            test_model.variables,  # total number of variables
            # total number of discrete variables
            test_model.binary_variables + test_model.integer_variables,
            '',  # 'nznum?',  # number of nonzeros
            '',  # 'nlz?',  # number of nonlinear nonzeros
            0,  # 1= optfile included
            termination_condition_to_gams_format(
                job_result.termination_condition),
            # GAMS model return status - see the GAMS return codes section.
            # GAMS solver return status - see the GAMS return codes section.
            solver_status_to_gams(pyo.SolverStatus.ok),
            job_result.UB,  # value of objective function
            job_result.
            UB,  # objective function estimate # TODO I think this only works for minimize?
            job_result.solver_run_time,  # resource time used (sec)
            job_result.iterations,  # number of solver iterations
            0,  # dom used
            0,  # nodes used
            '# automatically generated by pysperf'
        ]
        trace_data.append(trace_line)

    with outputdir.joinpath("results.trc").open('w') as tracefile:
        tracefile.write(textwrap.dedent(trace_header))
        tracefile.write('*\n')
        csvwriter = csv.writer(tracefile)
        csvwriter.writerows(trace_data)