Skip to content

Commit

Permalink
rename copy_var_value to set_var_value
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedongPeng committed Nov 28, 2023
1 parent 51be801 commit d9d29bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
10 changes: 8 additions & 2 deletions pyomo/contrib/mindtpy/single_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pyomo.contrib.mindtpy.util import (
get_integer_solution,
copy_var_list_values,
copy_var_value,
set_var_value,
)
from pyomo.contrib.gdpopt.util import get_main_elapsed_time, time_code
from pyomo.opt import TerminationCondition as tc
Expand Down Expand Up @@ -62,7 +62,13 @@ def copy_lazy_var_list_values(
if skip_fixed and v_to.is_fixed():
continue # Skip fixed variables.
v_val = self.get_values(opt._pyomo_var_to_solver_var_map[v_from])
copy_var_value(v_from, v_to, v_val, config, ignore_integrality=False)
set_var_value(
v_to,
v_val,
config.integer_tolerance,
config.zero_tolerance,
ignore_integrality=False,
)

def add_lazy_oa_cuts(
self,
Expand Down
32 changes: 21 additions & 11 deletions pyomo/contrib/mindtpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,13 @@ def copy_var_list_values_from_solution_pool(
elif config.mip_solver == 'gurobi_persistent':
solver_model.setParam(gurobipy.GRB.Param.SolutionNumber, solution_name)
var_val = var_map[v_from].Xn
copy_var_value(v_from, v_to, var_val, config, ignore_integrality)
set_var_value(
v_to,
var_val,
config.integer_tolerance,
config.zero_tolerance,
ignore_integrality,
)


class GurobiPersistent4MindtPy(GurobiPersistent):
Expand Down Expand Up @@ -973,10 +979,16 @@ def copy_var_list_values(
if skip_fixed and v_to.is_fixed():
continue # Skip fixed variables.
var_val = value(v_from, exception=False)
copy_var_value(v_from, v_to, var_val, config, ignore_integrality)
set_var_value(
v_to,
var_val,
config.integer_tolerance,
config.zero_tolerance,
ignore_integrality,
)


def copy_var_value(v_from, v_to, var_val, config, ignore_integrality):
def set_var_value(v_to, var_val, integer_tolerance, zero_tolerance, ignore_integrality):
"""This function copies variable value from one to another.
Rounds to Binary/Integer if necessary.
Sets to zero for NonNegativeReals if necessary.
Expand All @@ -988,14 +1000,14 @@ def copy_var_value(v_from, v_to, var_val, config, ignore_integrality):
Parameters
----------
v_from : Var
The variable that provides the values to copy from.
v_to : Var
The variable that needs to set value.
var_val : float
The value of v_to variable.
config : ConfigBlock
The specific configurations for MindtPy.
integer_tolerance: float
Tolerance on integral values.
zero_tolerance: float
Tolerance on variable equal to zero.
ignore_integrality : bool, optional
Whether to ignore the integrality of integer variables, by default False.
Expand All @@ -1021,11 +1033,9 @@ def copy_var_value(v_from, v_to, var_val, config, ignore_integrality):
v_to.set_value(v_to.ub)
elif ignore_integrality and v_to.is_integer():
v_to.set_value(var_val, skip_validation=True)
elif v_to.is_integer() and (
math.fabs(var_val - rounded_val) <= config.integer_tolerance
):
elif v_to.is_integer() and (math.fabs(var_val - rounded_val) <= integer_tolerance):
v_to.set_value(rounded_val)
elif abs(var_val) <= config.zero_tolerance and 0 in v_to.domain:
elif abs(var_val) <= zero_tolerance and 0 in v_to.domain:
v_to.set_value(0)
else:
raise ValueError(
Expand Down

0 comments on commit d9d29bf

Please sign in to comment.