def test_maxsat_par_alns(self): seed_random_generators(42) settings.inst_file = data_dir + "maxsat-adv1.cnf" settings.alg = 'par_alns' settings.mh_titer = 600 solution = run_optimization('MAXSAT', MAXSATInstance, MAXSATSolution, embedded=True)
def test_vertex_cover_gvns(self): seed_random_generators(42) settings.inst_file = data_dir + "frb40-19-1.mis" settings.alg = 'gvns' settings.mh_titer = 100 solution = run_optimization('Vertex Cover', VertexCoverInstance, VertexCoverSolution, embedded=True) self.assertEqual(solution.obj(), 726)
def test_qap_gvns(self): seed_random_generators(42) settings.inst_file = data_dir + 'bur26a.dat' settings.alg = 'gvns' settings.mh_titer = 1000 solution = run_optimization('QAP', QAPInstance, QAPSolution, embedded=True) self.assertEqual(solution.obj(), 5426670)
def test_mkp_gvns(self): seed_random_generators(42) settings.inst_file = data_dir + "mknapcb5-01.txt" settings.alg = 'gvns' settings.mh_titer = 70 solution = run_optimization('MKP', MKPInstance, MKPSolution, embedded=True) self.assertEqual(solution.obj(), 55610)
def test_misp_pbig(self): seed_random_generators(42) settings.inst_file = data_dir + "frb40-19-1.mis" settings.alg = 'pbig' settings.mh_titer = 500 solution = run_optimization('MISP', MISPInstance, MISPSolution, embedded=True) self.assertEqual(solution.obj(), 32)
def test_graph_coloring_gvns(self): seed_random_generators(42) settings.inst_file = data_dir + "fpsol2.i.1.col" settings.alg = 'gvns' settings.mh_titer = 500 solution = run_optimization('Graph Coloring', GCInstance, GCSolution, embedded=True) self.assertEqual(solution.obj(), 1634)
def test_tsp_ssga(self): seed_random_generators(42) settings.inst_file = data_dir + "xqf131.tsp" settings.alg = 'ssga' settings.mh_titer = 500 solution = run_optimization('TSP', TSPInstance, TSPSolution, embedded=True) self.assertEqual(solution.obj(), 1376)
def test_maxsat_gvns(self): seed_random_generators(42) settings.inst_file = data_dir + "maxsat-adv1.cnf" settings.alg = 'gvns' settings.mh_titer = 100 solution = run_optimization('MAXSAT', MAXSATInstance, MAXSATSolution, embedded=True) self.assertEqual(solution.obj(), 769)
s.add(u) self.remove_redundant() def initialize(self, k): """Initialize solution by taking all nodes and applying local_improve.""" super().initialize(0) self.greedy_construction(k == 0) # self.two_approximation_construction() # self.remove_redundant() self.check() def random_move_delta_eval(self) -> Tuple[int, TObj]: """Choose a random move and perform delta evaluation for it, return (move, delta_obj).""" raise NotImplementedError def apply_neighborhood_move(self, pos: int): """This method applies a given neighborhood move accepted by SA, without updating the obj_val or invalidating, since obj_val is updated incrementally by the SA scheduler.""" raise NotImplementedError def crossover(self, other: 'VertexCoverSolution') -> 'VertexCoverSolution': raise NotImplementedError if __name__ == '__main__': from pymhlib.demos.common import run_optimization from pymhlib.settings import get_settings_parser parser = get_settings_parser() run_optimization('Minimum Vertex Cover', VertexCoverInstance, VertexCoverSolution, "gnm-1000-2000")
i: len(set(self.inst.graph.neighbors(i)) - covered) for i, n in enumerate(self.covered) if n == 0 } return candidates def restricted_candidate_list_k(self, cl: dict, par): rcl = list() candidates = {k: v for k, v in cl.items()} k = min(len(candidates), par) for i in range(k): key = min(candidates, key=candidates.get) rcl.append(key) candidates.pop(key) return np.array(rcl) def restricted_candidate_list_alpha(self, cl: dict, par): mini = min(cl.values()) maxi = max(cl.values()) rcl = [k for k, v in cl.items() if v <= mini + par * (maxi - mini)] return np.array(rcl) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import get_settings_parser parser = get_settings_parser() run_optimization('MISP', MISPInstance, MISPSolution, data_dir + "frb40-19-1.mis")
(a[p2, p1] - a[p2, p2]) * (b[x[p2], x[p2]] - b[x[p2], x[p1]]) d += np.inner(a[p1, :] - a[p2, :], b[x[p2], x] - b[x[p1], x]) - \ (a[p1, p1] - a[p2, p1]) * (b[x[p2], x[p1]] - b[x[p1], x[p1]]) - \ (a[p1, p2] - a[p2, p2]) * (b[x[p2], x[p2]] - b[x[p1], x[p2]]) d += (a[p1, p1] - a[p2, p2]) * (b[x[p2], x[p2]] - b[x[p1], x[p1]]) + \ (a[p1, p2] - a[p2, p1]) * (b[x[p2], x[p1]] - b[x[p1], x[p2]]) return d def random_move_delta_eval(self) -> Tuple[Any, TObj]: """Choose a random move and perform delta evaluation for it, return (move, delta_obj).""" return self.random_two_exchange_move_delta_eval() def apply_neighborhood_move(self, move): """This method applies a given neighborhood move accepted by SA, without updating the obj_val or invalidating, since obj_val is updated incrementally by the SA scheduler.""" p1, p2 = move x = self.x x[p1], x[p2] = x[p2], x[p1] def crossover(self, other: 'QAPSolution') -> 'QAPSolution': """Perform cycle crossover.""" # return self.partially_mapped_crossover(other) return self.cycle_crossover(other) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import get_settings_parser parser = get_settings_parser() run_optimization('QAP', QAPInstance, QAPSolution, data_dir + 'bur26a.dat')
prev = (p1 - 1) % n nxt = (p2 + 1) % n x_p1 = self.x[p1] x_p2 = self.x[p2] x_prev = self.x[prev] x_next = self.x[nxt] d = self.inst.distances delta = d[x_prev][x_p2] + d[x_p1][x_next] - d[x_prev][x_p1] - d[x_p2][ x_next] return delta def random_move_delta_eval(self) -> Tuple[Any, TObj]: """Choose a random move and perform delta evaluation for it, return (move, delta_obj).""" return self.random_two_opt_move_delta_eval() def apply_neighborhood_move(self, move): """This method applies a given neighborhood move accepted by SA, without updating the obj_val or invalidating, since obj_val is updated incrementally by the SA scheduler.""" self.apply_two_opt_move(*move) def crossover(self, other: 'TSPSolution') -> 'TSPSolution': """Perform edge recombination.""" return self.edge_recombination(other) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import get_settings_parser parser = get_settings_parser() run_optimization('TSP', TSPInstance, TSPSolution, data_dir + "xqf131.tsp")
# Prevent this vertex from getting changed again under_conflict.remove(u) def initialize(self, _k): """Initialize solution vector with random colors.""" self.x = np.random.randint(self.inst.colors, size=len(self.x)) self.invalidate() def random_move_delta_eval(self) -> Tuple[int, int, TObj]: """Choose a random move and perform delta evaluation for it, return (move, delta_obj).""" raise NotImplementedError def apply_neighborhood_move(self, pos, color: int): """This method applies a given neighborhood move accepted by SA, without updating the obj_val or invalidating, since obj_val is updated incrementally by the SA scheduler.""" self.x[pos] = color def crossover(self, other: 'GCSolution') -> 'GCSolution': """ Preform uniform crossover.""" return self.uniform_crossover(other) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import settings, get_settings_parser settings.mh_maxi = False run_optimization('Graph Coloring', GCInstance, GCSolution, data_dir + "fpsol2.i.1.col")
d += (a[p1, p1] - a[p2, p2]) * (b[x[p2], x[p2]] - b[x[p1], x[p1]]) + \ (a[p1, p2] - a[p2, p1]) * (b[x[p2], x[p1]] - b[x[p1], x[p2]]) return d def random_move_delta_eval(self) -> Tuple[Any, TObj]: """Choose a random move and perform delta evaluation for it, return (move, delta_obj).""" return self.random_two_exchange_move_delta_eval() def apply_neighborhood_move(self, move): """This method applies a given neighborhood move accepted by SA, without updating the obj_val or invalidating, since obj_val is updated incrementally by the SA scheduler.""" p1, p2 = move x = self.x x[p1], x[p2] = x[p2], x[p1] def crossover(self, other: 'QAPSolution') -> 'QAPSolution': """Perform cycle crossover.""" # return self.partially_mapped_crossover(other) return self.cycle_crossover(other) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import get_settings_parser parser = get_settings_parser() TAKE = "xu3600.dat" for filename in os.listdir(data_dir): if filename == TAKE: run_optimization('QAP', QAPInstance, QAPSolution, data_dir + filename)
feasible = np.all(y_new <= self.inst.b) if allow_infeasible or feasible: # accept self.y = y_new if update_obj_val: self.obj_val += self.inst.p[elem] return feasible # revert self.sel -= 1 return False def random_move_delta_eval(self) -> Tuple[int, TObj]: """Choose a random move and perform delta evaluation for it, return (move, delta_obj).""" raise NotImplementedError def apply_neighborhood_move(self, pos: int): """This method applies a given neighborhood move accepted by SA, without updating the obj_val or invalidating, since obj_val is updated incrementally by the SA scheduler.""" raise NotImplementedError def crossover(self, other: 'MKPSolution') -> 'MKPSolution': """Apply subset_crossover.""" return self.subset_crossover(other) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import get_settings_parser parser = get_settings_parser() run_optimization('MKP', MKPInstance, MKPSolution, data_dir + "mknapcb5-01.txt")
self.invalidate() def destroy(self, par: Any, _result: Result): """Destroy operator for ALNS selects par*ALNS.get_number_to_destroy positions uniformly at random for removal. Selected positions are stored with the solution in list self.destroyed. """ num = min(ALNS.get_number_to_destroy(len(self.x)) * par, len(self.x)) self.destroyed = np.random.choice(range(len(self.x)), num, replace=False) self.invalidate() def repair(self, _par: Any, _result: Result): """Repair operator for ALNS assigns new random values to all positions in self.destroyed.""" assert self.destroyed is not None for p in self.destroyed: self.x[p] = random.randrange(2) self.destroyed = None self.invalidate() def crossover(self, other: 'JuliaMAXSAT2Solution'): """ Perform uniform crossover as crossover.""" return self.uniform_crossover(other) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import get_settings_parser parser = get_settings_parser() parser.set_defaults(mh_titer=1000) run_optimization('MAXSAT', Main.JuliaMAXSAT.JuliaMAXSATInstance, JuliaMAXSAT2Solution, data_dir+"maxsat-adv1.cnf")
rcl = list() candidates = {k: v for k, v in cl.items()} k = min(len(candidates), par) for i in range(k): key = max(candidates, key=candidates.get) rcl.append(key) candidates.pop(key) return np.array(rcl) def restricted_candidate_list_alpha(self, cl: dict, par): maximum = max(cl.values()) minimum = min(cl.values()) rcl = [ k for k, v in cl.items() if v >= v >= maximum - par * (maximum - minimum) ] return np.array(rcl) if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir from pymhlib.settings import get_settings_parser parser = get_settings_parser() parser.set_defaults(mh_titer=1000) run_optimization('MAXSAT', MAXSATInstance, MAXSATSolution, data_dir + "maxsat-adv1.cnf")
s.add(u) self.remove_redundant() def initialize(self, k): """Initialize solution by taking all nodes and applying local_improve.""" super().initialize(0) self.greedy_construction(k == 0) # self.two_approximation_construction() # self.remove_redundant() self.check() def random_move_delta_eval(self) -> Tuple[int, TObj]: """Choose a random move and perform delta evaluation for it, return (move, delta_obj).""" raise NotImplementedError def apply_neighborhood_move(self, pos: int): """This method applies a given neighborhood move accepted by SA, without updating the obj_val or invalidating, since obj_val is updated incrementally by the SA scheduler.""" raise NotImplementedError def crossover(self, other: 'VertexCoverSolution') -> 'VertexCoverSolution': """Abstract crossover function.""" raise NotImplementedError if __name__ == '__main__': from pymhlib.demos.common import run_optimization, data_dir parser = get_settings_parser() run_optimization('Minimum Vertex Cover', VertexCoverInstance, VertexCoverSolution, data_dir + "frb40-19-1.mis")