Skip to content

Instance Modifiers

Modifiers wrap any instance generator and post-process the model before it reaches the environment.

EmbedObjective

EmbedObjective

Bases: InstanceGenerator

This class is a instance generator modifier. It embeds an objective function into the generated instances. It modifies the instance generator to include a variable representing the objective function, and adds a constraint that ensures the new variable is equal to the old objective function. It then modifies the objective to be the new variable, effectively embedding the objective into the instance.

Source code in gyozas/instances/modifiers/embed_objective.py
class EmbedObjective(InstanceGenerator):
    """
    This class is a instance generator modifier. It embeds an objective function into the generated instances.
    It modifies the instance generator to include a variable representing the objective function,
    and adds a constraint that ensures the new variable is equal to the old objective function.
    It then modifies the objective to be the new variable, effectively embedding the objective into the instance.
    """

    def __init__(self, instance_generator: InstanceGenerator, replace_fobj=False) -> None:
        """
        Initializes the EmbedObjective with an instance generator.

        :param instance_generator: The instance generator to be embedded in the system.
        """
        self.instance_generator = instance_generator
        self.replace_fobj = replace_fobj

    def seed(self, seed) -> None:
        self.instance_generator.seed(seed)

    def generate_instance(self, *args, **kwargs) -> Model:
        """
        Generates an instance using the embedded instance generator.

        :param args: Positional arguments for the instance generator.
        :param kwargs: Keyword arguments for the instance generator.
        :return: The generated instance.
        """
        return self.instance_generator.generate_instance(*args, **kwargs)

    def __next__(self) -> Model:
        model = self.instance_generator.__next__()
        # Capture original objective sense and coefficients before adding _fobj_
        sense = "minimize" if model.getObjectiveSense() == "minimize" else "maximize"
        variables = model.getVars()
        obj_terms = [(var, var.getObj()) for var in variables if var.getObj() != 0]
        _fobj_ = model.addVar(vtype="C", lb=None, ub=None, name="_fobj_")
        # Constrain _fobj_ to equal the original objective
        model.addCons(
            quicksum(coef * var for var, coef in obj_terms) == _fobj_,
            name="objective_function_constraint",
        )
        if self.replace_fobj:
            model.setObjective(_fobj_, sense=sense)
        return model

__init__(instance_generator, replace_fobj=False)

Initializes the EmbedObjective with an instance generator.

:param instance_generator: The instance generator to be embedded in the system.

Source code in gyozas/instances/modifiers/embed_objective.py
def __init__(self, instance_generator: InstanceGenerator, replace_fobj=False) -> None:
    """
    Initializes the EmbedObjective with an instance generator.

    :param instance_generator: The instance generator to be embedded in the system.
    """
    self.instance_generator = instance_generator
    self.replace_fobj = replace_fobj

generate_instance(*args, **kwargs)

Generates an instance using the embedded instance generator.

:param args: Positional arguments for the instance generator. :param kwargs: Keyword arguments for the instance generator. :return: The generated instance.

Source code in gyozas/instances/modifiers/embed_objective.py
def generate_instance(self, *args, **kwargs) -> Model:
    """
    Generates an instance using the embedded instance generator.

    :param args: Positional arguments for the instance generator.
    :param kwargs: Keyword arguments for the instance generator.
    :return: The generated instance.
    """
    return self.instance_generator.generate_instance(*args, **kwargs)

SetParameters

SetParameters

Bases: InstanceGenerator

This class is a instance generator modifier. It sets SCIP solver parameters into the generated instances.

Source code in gyozas/instances/modifiers/set_parameters.py
class SetParameters(InstanceGenerator):
    """
    This class is a instance generator modifier. It sets SCIP solver parameters into the generated instances.
    """

    def __init__(self, instance_generator: InstanceGenerator, parameters: dict[str, Any]) -> None:
        """
        Initializes the SetParameters with an instance generator and the parameters dictionnary.

        :param instance_generator: The instance generator.
        :param parameters: The parameters to be set in the SCIP solver.
        """
        self.instance_generator = instance_generator
        self.parameters = parameters

    def seed(self, seed) -> None:
        self.instance_generator.seed(seed)

    def set_parameters(self, instance: Model) -> Model:
        logger = getLogger(__name__)
        for k, v in self.parameters.items():
            try:
                instance.setParam(k, v)
            except Exception as e:
                logger.warning(f"Parameter {k} could not be set due to {str(e)}")
        return instance

    def generate_instance(self, *args, **kwargs) -> Model:
        """
        Generates an instance and set parameters in the SCIP solver.

        :param args: Positional arguments for the instance generator.
        :param kwargs: Keyword arguments for the instance generator.
        :return: The generated instance.
        """
        return self.set_parameters(self.instance_generator.generate_instance(*args, **kwargs))

    def __next__(self) -> Model:
        model = self.instance_generator.__next__()
        return self.set_parameters(model)

__init__(instance_generator, parameters)

Initializes the SetParameters with an instance generator and the parameters dictionnary.

:param instance_generator: The instance generator. :param parameters: The parameters to be set in the SCIP solver.

Source code in gyozas/instances/modifiers/set_parameters.py
def __init__(self, instance_generator: InstanceGenerator, parameters: dict[str, Any]) -> None:
    """
    Initializes the SetParameters with an instance generator and the parameters dictionnary.

    :param instance_generator: The instance generator.
    :param parameters: The parameters to be set in the SCIP solver.
    """
    self.instance_generator = instance_generator
    self.parameters = parameters

generate_instance(*args, **kwargs)

Generates an instance and set parameters in the SCIP solver.

:param args: Positional arguments for the instance generator. :param kwargs: Keyword arguments for the instance generator. :return: The generated instance.

Source code in gyozas/instances/modifiers/set_parameters.py
def generate_instance(self, *args, **kwargs) -> Model:
    """
    Generates an instance and set parameters in the SCIP solver.

    :param args: Positional arguments for the instance generator.
    :param kwargs: Keyword arguments for the instance generator.
    :return: The generated instance.
    """
    return self.set_parameters(self.instance_generator.generate_instance(*args, **kwargs))

Convenience Presets

Pre-built SetParameters instances for common configurations:

Name Effect
SetNoCuts Disables all cutting plane separators
SetNoHeuristics Disables all primal heuristics
SetNoDisplay Silences SCIP solver output
SetDFSNodeSelection Forces depth-first node selection
SetBFSNodeSelection Forces breadth-first node selection