Skip to content

Commit

Permalink
[COMBOPT] fix TR centers and hamming distance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Grosnit 80055266 committed Jul 25, 2022
1 parent 7802bc8 commit aa35fdc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self,
tr_manager.register_radius('numeric', 0, 1, 1)
tr_manager.register_radius('nominal', min_radius=0, max_radius=search_space.num_nominal + 1,
init_radius=search_space.num_nominal + 1)
tr_manager.set_center(center=search_space.transform(search_space.sample()))

assert search_space.num_cont + search_space.num_disc + search_space.num_nominal == search_space.num_dims, \
'Interleaved Search only supports continuous, discrete and nominal variables'
Expand Down Expand Up @@ -129,8 +130,8 @@ def _optimize(self,
dtype, device = model.dtype, model.device

# Sample initialisation

x0, numeric_lb, numeric_ub = sample_numeric_and_nominal_within_tr(x_centre=x,
x_centre = x.clone()
x0, numeric_lb, numeric_ub = sample_numeric_and_nominal_within_tr(x_centre=x_centre,
search_space=self.search_space,
tr_manager=self.tr_manager,
n_points=self.n_restarts,
Expand Down Expand Up @@ -191,8 +192,7 @@ def _optimize(self,
while not is_valid:

neighbour_nominal = self._mutate_nominal(x_nominal)
if 0 <= hamming_distance(x_[self.search_space.nominal_dims], neighbour_nominal,
# TODO: Hamming distance should be computed w.r.t. center (self.tr_manager.center)
if 0 <= hamming_distance(x_centre[self.search_space.nominal_dims], neighbour_nominal,
normalize=False) <= self.tr_manager.radii['nominal']:
is_valid = True
else:
Expand Down
2 changes: 1 addition & 1 deletion combopt/comb_opt/trust_region/tr_manager_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def restart_tr(self):
for var_type in self.variable_types:
self.radii[var_type] = self.init_radii[var_type]

self.center = None
self.set_center(None)

@abstractmethod
def restart(self):
Expand Down
4 changes: 2 additions & 2 deletions combopt/tests/test_boils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import sys
from pathlib import Path

from comb_opt.optimizers import BOiLS

ROOT_PROJECT = str(Path(os.path.realpath(__file__)).parent.parent)
sys.path[0] = ROOT_PROJECT

from comb_opt.optimizers import BOiLS

import torch


Expand Down
27 changes: 27 additions & 0 deletions combopt/tests/test_casmopolitan.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# Copyright (C) 2022. Huawei Technologies Co., Ltd. All rights reserved. Redistribution and use in source and binary
# forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import sys
from pathlib import Path

ROOT_PROJECT = str(Path(os.path.realpath(__file__)).parent.parent)
sys.path[0] = ROOT_PROJECT

import os.path
from pathlib import Path

Expand Down

0 comments on commit aa35fdc

Please sign in to comment.