Exemple #1
0
def run_gvns_demo(problem_name: str, Instance, Solution, default_inst_file: str, own_settings=None):
    """Run GVNS for any of the demo problems specified by the parameters."""
    parser = get_settings_parser()
    parser.add("--inst_file", type=str, default=default_inst_file,
               help='problem instance file')
    parser.add("--meths_ch", type=int, default=1,
               help='number of construction heuristics to be used in OC')
    parser.add("--meths_li", type=int, default=1,
               help='number of local improvement methods to be used in OC')
    parser.add("--meths_sh", type=int, default=5,
               help='number of shaking methods to be used in OC')
    # parser.set_defaults(seed=3)

    parse_settings()
    init_logger()
    logger = logging.getLogger("mhlib")
    logger.info(f"mhlib demo for solving {problem_name}")
    logger.info(get_settings_as_str())
    instance = Instance(settings.inst_file)
    logger.info(f"{problem_name} instance read:\n" + str(instance))
    solution = Solution(instance)
    # solution.initialize(0)

    logger.info(f"Solution: {solution}, obj={solution.obj()}\n")

    gvns = GVNS(solution,
                [Method(f"ch{i}", Solution.construct, i) for i in range(settings.meths_ch)],
                [Method(f"li{i}", Solution.local_improve, i) for i in range(1, settings.meths_li + 1)],
                [Method(f"sh{i}", Solution.shaking, i) for i in range(1, settings.meths_sh + 1)],
                own_settings)
    gvns.run()
    logger.info("")
    gvns.method_statistics()
    gvns.main_results()
Exemple #2
0
def main():
    """Test for the DD classes."""
    import logging
    from mhlib.demos.common import data_dir
    parser = get_settings_parser()
    parser.add("--inst_file", type=str, default=data_dir+'mknap-small.txt', help='problem instance file')
    # parser.set_defaults(seed=3)

    parse_settings()
    init_logger()
    logger = logging.getLogger("mhlib")
    logger.info(f"mhlib demo for using decision diagrams for the MKP")
    logger.info(get_settings_as_str())
    instance = MKPInstance(settings.inst_file)
    logger.info(f"MKP instance read:\n" + str(instance))

    # solution = MKPSolution(instance)
    # solution.initialize(0)
    dd = MKPDecisionDiag(instance)
    dd.expand_all('relaxed')
    logger.info(dd)
    sol = dd.derive_solution()
    # sol.check()
    logger.info(f"Solution: {sol}, obj={sol.obj()}")
Exemple #3
0
in some way repeatedly applied to candidate solutions.
"""

from typing import Callable, List, Any, Optional
from dataclasses import dataclass
from abc import ABC, abstractmethod
import random
import time
import logging
from math import log10

from mhlib.settings import settings, get_settings_parser, OwnSettings
from mhlib.solution import Solution, TObj


parser = get_settings_parser()
parser.add("--mh_titer", type=int, default=100,
           help='maximum number of iterations (<0: turned off)')
parser.add("--mh_tciter", type=int, default=-1,
           help='maximum number of iterations without improvement (<0: turned off)')
parser.add("--mh_ttime", type=int, default=-1,
           help='time limit [s] (<0: turned off)')
parser.add("--mh_tctime", type=int, default=-1,
           help='maximum time [s] without improvement (<0: turned off)')
parser.add("--mh_tobj", type=float, default=-1,
           help='objective value at which should be terminated when reached (<0: turned off)')
parser.add("--mh_lnewinc", default=True, action='store_true',
           help='write iteration log if new incumbent solution')
parser.add("--no_mh_lnewinc", dest='mh_lnewinc', action='store_false')
parser.add("--mh_lfreq", type=int, default=0,
           help='frequency of writing iteration logs (0: none, >0: number of iterations, -1: iteration 1,2,5,10,20,...')