Skip to content

Question about copy_var_list_values function in GDPOpt #2653

Open
@ZedongPeng

Description

@ZedongPeng

Description

I just noticed the change of copy_var_list_values function in GDPOpt.util.

The 'ignore_integrality' argument no longer has any functionality.

Previously, when we copy the value of variable A1 to integer variable A2, if A1 equals 4.000001, we will do a rounding and set A2=4. I think now the function will ignore integrality forever. Does this mean that we need to check the value before calling copy_var_list_values function? Or is there any need to do the rounding for integer variables? @emma58

Previous copy_var_list_values function

def copy_var_list_values(from_list, to_list, config,
                         skip_stale=False, skip_fixed=True,
                         ignore_integrality=False):
    """Copy variable values from one list to another.
    Rounds to Binary/Integer if necessary
    Sets to zero for NonNegativeReals if necessary
    """
    for v_from, v_to in zip(from_list, to_list):
        if skip_stale and v_from.stale:
            continue  # Skip stale variable values.
        if skip_fixed and v_to.is_fixed():
            continue  # Skip fixed variables.
        try:
            # NOTE: PEP 2180 changes the var behavior so that domain /
            # bounds violations no longer generate exceptions (and
            # instead log warnings).  This means that the following will
            # always succeed and the ValueError should never be raised.
            v_to.set_value(value(v_from, exception=False))
            if skip_stale:
                v_to.stale = False
        except ValueError as err:
            err_msg = getattr(err, 'message', str(err))
            var_val = value(v_from)
            rounded_val = int(round(var_val))
            # Check to see if this is just a tolerance issue
            if ignore_integrality and v_to.is_integer():
                v_to.set_value(var_val, skip_validation=True)
            elif v_to.is_integer() and (fabs(var_val - rounded_val) <=
                                        config.integer_tolerance):
                v_to.set_value(rounded_val)
            elif abs(var_val) <= config.zero_tolerance and 0 in v_to.domain:
                v_to.set_value(0)
            else:
                config.logger.error(
                    'Unknown validation domain error setting variable %s', (v_to.name,))
                raise

Current copy_var_list_values function

def copy_var_list_values(from_list, to_list, config,
                         skip_stale=False, skip_fixed=True,
                         ignore_integrality=False):
    """Copy variable values from one list to another.

    Rounds to Binary/Integer if necessary
    Sets to zero for NonNegativeReals if necessary
    """
    if ignore_integrality:
        deprecation_warning("The 'ignore_integrality' argument no longer "
                            "has any functionality.", version="6.4.2")

    if len(from_list) != len(to_list):
        raise ValueError('The lengths of from_list and to_list do not match.')

    for v_from, v_to in zip(from_list, to_list):
        if skip_stale and v_from.stale:
            continue  # Skip stale variable values.
        if skip_fixed and v_to.is_fixed():
            continue  # Skip fixed variables.
        v_to.set_value(value(v_from, exception=False), skip_validation=True)

Additional information

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions