Example #1
0
def preprocess_scenario_instance(scenario_instance,
                                 instance_variables_fixed,
                                 instance_variables_freed,
                                 instance_user_constraints_modified,
                                 instance_ph_constraints_modified,
                                 instance_ph_constraints,
                                 instance_objective_modified,
                                 preprocess_fixed_variables,
                                 solver):

    persistent_solver_in_use = isinstance(solver, PersistentSolver)
    if (not instance_objective_modified) and \
       (not instance_variables_fixed) and \
       (not instance_variables_freed) and \
       (not instance_ph_constraints_modified) and \
       (not instance_user_constraints_modified):

        # the condition of "nothing modified" should only be triggered
        # at PH iteration 0. instances are already preprocessed
        # following construction, and there isn't any augmentation of
        # the objective function yet.
        return

    if instance_objective_modified:
        # if only the objective changed, there is minimal work to do.

        if solver.problem_format() == ProblemFormat.nl:
            ampl_preprocess_block_objectives(scenario_instance)
        else:

            canonical_preprocess_block_objectives(scenario_instance)

        if persistent_solver_in_use and solver.instance_compiled():
            solver.compile_objective(scenario_instance)

    if (instance_variables_fixed or instance_variables_freed) and \
       (preprocess_fixed_variables):

        if solver.problem_format() == ProblemFormat.nl:
            ampl_expression_preprocessor({}, model=scenario_instance)
        else:
            canonical_expression_preprocessor({}, model=scenario_instance)

        # We've preprocessed the entire instance, no point in checking
        # anything else
        return

    if (instance_variables_fixed or instance_variables_freed) and \
       (persistent_solver_in_use):
        # it can be the case that the solver plugin no longer has an
        # instance compiled, depending on what state the solver plugin
        # is in relative to the instance.  if this is the case, just
        # don't compile the variable bounds.
        if solver.instance_compiled():
            variables_to_change = \
                instance_variables_fixed + instance_variables_freed
            solver.compile_variable_bounds(scenario_instance,
                                           vars_to_update=variables_to_change)

    if instance_user_constraints_modified:
        if solver.problem_format() == ProblemFormat.nl:
            idMap = {}
            for block in scenario_instance.block_data_objects(active=True,
                                                              descend_into=True):
                ampl_preprocess_block_constraints(block,
                                                  idMap=idMap)
        else:
            idMap = {}
            for block in scenario_instance.block_data_objects(active=True,
                                                              descend_into=True):
                canonical_preprocess_block_constraints(block,
                                                       idMap=idMap)

    elif instance_ph_constraints_modified:

        # only pre-process the piecewise constraints
        if solver.problem_format() == ProblemFormat.nl:
            idMap = {}
            for constraint_name in instance_ph_constraints:
                ampl_preprocess_constraint(
                    scenario_instance,
                    getattr(scenario_instance, constraint_name),
                    idMap=idMap)
        else:
            idMap = {}
            for constraint_name in instance_ph_constraints:
                canonical_preprocess_constraint(
                    scenario_instance,
                    getattr(scenario_instance, constraint_name),
                    idMap=idMap)
Example #2
0
def preprocess_scenario_instance(
        scenario_instance, instance_variables_fixed, instance_variables_freed,
        instance_user_constraints_modified, instance_ph_constraints_modified,
        instance_ph_constraints, instance_objective_modified,
        preprocess_fixed_variables, solver):

    persistent_solver_in_use = isinstance(solver, PersistentSolver)
    if (not instance_objective_modified) and \
       (not instance_variables_fixed) and \
       (not instance_variables_freed) and \
       (not instance_ph_constraints_modified) and \
       (not instance_user_constraints_modified):

        # the condition of "nothing modified" should only be triggered
        # at PH iteration 0. instances are already preprocessed
        # following construction, and there isn't any augmentation of
        # the objective function yet.
        return

    if instance_objective_modified:
        # if only the objective changed, there is minimal work to do.

        if solver.problem_format() == ProblemFormat.nl:
            ampl_preprocess_block_objectives(scenario_instance)
        else:

            canonical_preprocess_block_objectives(scenario_instance)

        if persistent_solver_in_use and solver.instance_compiled():
            solver.compile_objective(scenario_instance)

    if (instance_variables_fixed or instance_variables_freed) and \
       (preprocess_fixed_variables):

        if solver.problem_format() == ProblemFormat.nl:
            ampl_expression_preprocessor({}, model=scenario_instance)
        else:
            canonical_expression_preprocessor({}, model=scenario_instance)

        # We've preprocessed the entire instance, no point in checking
        # anything else
        return

    if (instance_variables_fixed or instance_variables_freed) and \
       (persistent_solver_in_use):
        # it can be the case that the solver plugin no longer has an
        # instance compiled, depending on what state the solver plugin
        # is in relative to the instance.  if this is the case, just
        # don't compile the variable bounds.
        if solver.instance_compiled():
            variables_to_change = \
                instance_variables_fixed + instance_variables_freed
            solver.compile_variable_bounds(scenario_instance,
                                           vars_to_update=variables_to_change)

    if instance_user_constraints_modified:
        if solver.problem_format() == ProblemFormat.nl:
            idMap = {}
            for block in scenario_instance.block_data_objects(
                    active=True, descend_into=True):
                ampl_preprocess_block_constraints(block, idMap=idMap)
        else:
            idMap = {}
            for block in scenario_instance.block_data_objects(
                    active=True, descend_into=True):
                canonical_preprocess_block_constraints(block, idMap=idMap)

    elif instance_ph_constraints_modified:

        # only pre-process the piecewise constraints
        if solver.problem_format() == ProblemFormat.nl:
            idMap = {}
            for constraint_name in instance_ph_constraints:
                ampl_preprocess_constraint(scenario_instance,
                                           getattr(scenario_instance,
                                                   constraint_name),
                                           idMap=idMap)
        else:
            idMap = {}
            for constraint_name in instance_ph_constraints:
                canonical_preprocess_constraint(scenario_instance,
                                                getattr(
                                                    scenario_instance,
                                                    constraint_name),
                                                idMap=idMap)
Example #3
0
def preprocess_scenario_instance(
        scenario_instance, instance_variables_fixed, instance_variables_freed,
        instance_user_constraints_modified, instance_ph_constraints_modified,
        instance_ph_constraints, instance_objective_modified,
        preprocess_fixed_variables, solver):
    # TODO: Does this import need to be delayed because
    #       it is in a plugins subdirectory
    from pyomo.solvers.plugins.solvers.persistent_solver import \
        PersistentSolver

    persistent_solver_in_use = isinstance(solver, PersistentSolver)
    if (not instance_objective_modified) and \
       (not instance_variables_fixed) and \
       (not instance_variables_freed) and \
       (not instance_ph_constraints_modified) and \
       (not instance_user_constraints_modified):

        # the condition of "nothing modified" should only be triggered
        # at PH iteration 0. instances are already preprocessed
        # following construction, and there isn't any augmentation of
        # the objective function yet.
        return

    if instance_objective_modified:
        # if only the objective changed, there is minimal work to do.

        if solver.problem_format() == ProblemFormat.nl:
            ampl_preprocess_block_objectives(scenario_instance)
        else:

            canonical_preprocess_block_objectives(scenario_instance)

        if persistent_solver_in_use and solver.has_instance():
            obj_count = 0
            for obj in scenario_instance.component_data_objects(
                    ctype=Objective, descend_into=True, active=True):
                obj_count += 1
                if obj_count > 1:
                    raise RuntimeError(
                        'Persistent solver interface only supports a single objective.'
                    )
                solver.set_objective(obj)

    if (instance_variables_fixed or instance_variables_freed) and \
       (preprocess_fixed_variables):

        if solver.problem_format() == ProblemFormat.nl:
            ampl_expression_preprocessor({}, model=scenario_instance)
        else:
            canonical_expression_preprocessor({}, model=scenario_instance)

        # We've preprocessed the entire instance, no point in checking
        # anything else
        return

    if (instance_variables_fixed or instance_variables_freed) and \
       (persistent_solver_in_use):
        # it can be the case that the solver plugin no longer has an
        # instance compiled, depending on what state the solver plugin
        # is in relative to the instance.  if this is the case, just
        # don't compile the variable bounds.
        if solver.has_instance():
            variables_to_change = \
                instance_variables_fixed + instance_variables_freed
            for var_name, var_index in variables_to_change:
                solver.update_var(
                    scenario_instance.find_component(var_name)[var_index])

    if instance_user_constraints_modified:
        if solver.problem_format() == ProblemFormat.nl:
            idMap = {}
            for block in scenario_instance.block_data_objects(
                    active=True, descend_into=True):
                ampl_preprocess_block_constraints(block, idMap=idMap)
        else:
            idMap = {}
            for block in scenario_instance.block_data_objects(
                    active=True, descend_into=True):
                canonical_preprocess_block_constraints(block, idMap=idMap)

    elif instance_ph_constraints_modified:

        # only pre-process the piecewise constraints
        if solver.problem_format() == ProblemFormat.nl:
            idMap = {}
            for constraint_name in instance_ph_constraints:
                ampl_preprocess_constraint(scenario_instance,
                                           getattr(scenario_instance,
                                                   constraint_name),
                                           idMap=idMap)
        else:
            idMap = {}
            for constraint_name in instance_ph_constraints:
                canonical_preprocess_constraint(scenario_instance,
                                                getattr(
                                                    scenario_instance,
                                                    constraint_name),
                                                idMap=idMap)