Пример #1
0
def _lazy_ptdf_dcopf_model_solve_loop(m, md, solver, timelimit, solver_tee=True, symbolic_solver_labels=False, iteration_limit=100000):
    from pyomo.solvers.plugins.solvers.persistent_solver import PersistentSolver

    PTDF = m._PTDF

    ptdf_options = m._ptdf_options

    rel_flow_tol = ptdf_options['rel_flow_tol']
    abs_flow_tol = ptdf_options['abs_flow_tol']

    branch_limits = PTDF.branch_limits_array

    ## only enforce the relative and absolute, within tollerance
    PTDF.enforced_branch_limits = np.maximum(branch_limits*(1+rel_flow_tol), branch_limits+abs_flow_tol)

    persistent_solver = isinstance(solver, PersistentSolver)

    for i in range(iteration_limit):

        PFV, viol_num, mon_viol_num, gt_viol_lazy, lt_viol_lazy = lpu.check_violations(m, md, PTDF, ptdf_options['max_violations_per_iteration'])

        iter_status_str = "iteration {0}, found {1} violation(s)".format(i,viol_num)
        if mon_viol_num:
            iter_status_str += ", {} of which are already monitored".format(mon_viol_num)

        print(iter_status_str)

        if viol_num <= 0:
            ## in this case, there are no violations!
            ## load the duals now too, if we're using a persistent solver
            if persistent_solver:
                solver.load_duals()
            return lpu.LazyPTDFTerminationCondition.NORMAL

        elif viol_num == mon_viol_num:
            print('WARNING: Terminating with monitored violations!')
            print('         Result is not transmission feasible.')
            if persistent_solver:
                solver.load_duals()
            return lpu.LazyPTDFTerminationCondition.FLOW_VIOLATION

        lpu.add_violations(gt_viol_lazy, lt_viol_lazy, PFV, m, md, solver, ptdf_options, PTDF)

        #m.ineq_pf_branch_thermal_lb.pprint()
        #m.ineq_pf_branch_thermal_ub.pprint()

        if persistent_solver:
            solver.solve(m, tee=solver_tee, load_solutions=False, save_results=False)
            solver.load_vars()
        else:
            solver.solve(m, tee=solver_tee, symbolic_solver_labels=symbolic_solver_labels)

    else: # we hit the iteration limit
        print('WARNING: Exiting on maximum iterations for lazy PTDF model.')
        print('         Result is not transmission feasible.')
        if persistent_solver:
            solver.load_duals()
        return lpu.LazyPTDFTerminationCondition.ITERATION_LIMIT
Пример #2
0
def _lazy_ptdf_dcopf_model_solve_loop(m,
                                      md,
                                      solver,
                                      solver_tee=True,
                                      symbolic_solver_labels=False,
                                      iteration_limit=100000):
    '''
    The lazy PTDF DCOPF solver loop. This function iteratively
    adds violated transmission constraints until either the result is
    transmission feasible or we're tracking every violated constraint
    in the model

    Parameters
    ----------
    m : pyomo.environ.ConcreteModel
        An egret DCOPF model with no transmission constraints
    md : egret.data.ModelData
        An egret ModelData object
    solver : pyomo.opt.solver
        A pyomo solver object
    solver_tee : bool (optional)
        For displaying the solver log (default is True)
    symbolic_solver_labels : bool (optional)
        Use symbolic solver labels when writing to the solver (default is False)
    iteration_limit : int (optional)
        Number of iterations before a hard termination (default is 100000)

    Returns
    -------
    egret.common.lazy_ptdf_utils.LazyPTDFTerminationCondition : the termination status
    pyomo.opt.results.SolverResults : The results object from the pyomo solver
    int : The number of iterations before termination

    '''
    from pyomo.solvers.plugins.solvers.persistent_solver import PersistentSolver

    PTDF = m._PTDF

    ptdf_options = m._ptdf_options

    persistent_solver = isinstance(solver, PersistentSolver)

    for i in range(iteration_limit):

        PFV, PFV_I, viol_num, mon_viol_num, viol_lazy, int_viol_lazy \
                = lpu.check_violations(m, md, PTDF, ptdf_options['max_violations_per_iteration'])

        iter_status_str = "iteration {0}, found {1} violation(s)".format(
            i, viol_num)
        if mon_viol_num:
            iter_status_str += ", {} of which are already monitored".format(
                mon_viol_num)

        logger.info(iter_status_str)

        if viol_num <= 0:
            ## in this case, there are no violations!
            ## load the duals now too, if we're using a persistent solver
            if persistent_solver:
                solver.load_duals()
            return lpu.LazyPTDFTerminationCondition.NORMAL

        elif viol_num == mon_viol_num:
            logger.warning(
                'WARNING: Terminating with monitored violations! Result is not transmission feasible.'
            )
            if persistent_solver:
                solver.load_duals()
            return lpu.LazyPTDFTerminationCondition.FLOW_VIOLATION

        lpu.add_violations(viol_lazy, int_viol_lazy, PFV, PFV_I, m, md, solver,
                           ptdf_options, PTDF)
        total_flow_constr_added = len(viol_lazy) + len(int_viol_lazy)
        logger.info("iteration {0}, added {1} flow constraint(s)".format(
            i, total_flow_constr_added))

        if persistent_solver:
            solver.solve(m,
                         tee=solver_tee,
                         load_solutions=False,
                         save_results=False)
            solver.load_vars()
        else:
            solver.solve(m,
                         tee=solver_tee,
                         symbolic_solver_labels=symbolic_solver_labels)

    else:  # we hit the iteration limit
        logger.warning(
            'WARNING: Exiting on maximum iterations for lazy PTDF model. Result is not transmission feasible.'
        )
        if persistent_solver:
            solver.load_duals()
        return lpu.LazyPTDFTerminationCondition.ITERATION_LIMIT
Пример #3
0
def _lazy_ptdf_dcopf_model_solve_loop(m,
                                      md,
                                      solver,
                                      timelimit,
                                      solver_tee=True,
                                      symbolic_solver_labels=False,
                                      iteration_limit=100000):
    from pyomo.solvers.plugins.solvers.persistent_solver import PersistentSolver

    PTDF = m._PTDF

    ptdf_options = m._ptdf_options

    lazy_rel_flow_tol = ptdf_options['lazy_rel_flow_tol']

    rel_flow_tol = ptdf_options['rel_flow_tol']
    abs_flow_tol = ptdf_options['abs_flow_tol']

    branch_limits = PTDF.branch_limits_array

    ## if lazy_rel_flow_tol < 0, this narrows the branch limits
    PTDF.lazy_branch_limits = branch_limits * (1 + lazy_rel_flow_tol)

    ## only enforce the relative and absolute, within tollerance
    PTDF.enforced_branch_limits = np.maximum(
        branch_limits * (1 + rel_flow_tol), branch_limits + abs_flow_tol)

    persistent_solver = isinstance(solver, PersistentSolver)

    for i in range(iteration_limit):

        PFV, viol_num, viols_tup = lpu.check_violations(m, PTDF)

        print("iteration {0}, found {1} violation(s)".format(i, viol_num))

        if viol_num <= 0:
            ## in this case, there are no violations!
            ## load the duals now too, if we're using a persistent solver
            if persistent_solver:
                solver.load_duals()
            return lpu.LazyPTDFTerminationCondition.NORMAL

        all_viol_in_model = lpu.add_violations(viols_tup, PFV, m, md, solver,
                                               ptdf_options, PTDF)

        if all_viol_in_model:
            print('WARNING: Terminating with monitored violations!')
            print('         Result is not transmission feasible.')
            if persistent_solver:
                solver.load_duals()
            return lpu.LazyPTDFTerminationCondition.FLOW_VIOLATION

        #m.ineq_pf_branch_thermal_lb.pprint()
        #m.ineq_pf_branch_thermal_ub.pprint()

        if persistent_solver:
            solver.solve(m,
                         tee=solver_tee,
                         load_solutions=False,
                         save_results=False)
            solver.load_vars()
        else:
            solver.solve(m,
                         tee=solver_tee,
                         symbolic_solver_labels=symbolic_solver_labels)
    else:
        print('WARNING: Exiting on maximum iterations for lazy PTDF model.')
        print('         Result is not transmission feasible.')
        if persistent_solver:
            solver.load_duals()
        return lpu.LazyPTDFTerminationCondition.ITERATION_LIMIT