def importAction(self): try: sys.path.append(os.path.dirname(self.inputline.text())) mod = imp.load_source(os.path.splitext(path_leaf(self.inputline.text()))[0], self.inputline.text()) # Load module class_ = None try: class_ = getattr(mod, os.path.splitext(path_leaf(self.inputline.text()))[0]) except: self.printMessage("Import failed: Expected class named " + os.path.splitext(path_leaf(self.inputline.text()))[0] + " containing the optimization problem\n", "red") return None self.data = class_() check_opt_prob(self.data) # Check if the objective function is external if not hasattr(self.data, "objfunction"): self.external = True self.objfun = getattr(mod, "objfunction") else: self.external = False self.objfun = None self.printMessage("Import successful\n", "green") self.datainp = True self.inevChange(self.inevline.text()) # Print info self.info1.setText(str("Problem name: " + os.path.splitext(path_leaf(self.inputline.text()))[0])) self.info1.adjustSize() self.info2.setText(str(self.data.dim) + " dimensions") self.info2.adjustSize() self.info3.setText(str(len(self.data.integer)) + " integer variables") self.info3.adjustSize() self.info4.setText("Non-box constraints: " + str(hasattr(self.data, 'eval_ineq_constraints'))) self.info4.adjustSize() if self.external: self.info5.setText("External objective function") else: self.info5.setText("Python objective function") self.info5.adjustSize() except Exception as err: self.printMessage("Import failed: " + err.args[0] + "\n", "red") self.data = None self.datainp = False self.external = False self.objfun = None # Reset info self.info1.setText("") self.info1.adjustSize() self.info2.setText("") self.info2.adjustSize() self.info3.setText("") self.info3.adjustSize() self.info4.setText("") self.info4.adjustSize() self.info5.setText("") self.info5.adjustSize()
def __init__(self, dim=10): self.xlow = -5 * np.ones(dim) self.xup = 5 * np.ones(dim) self.dim = dim self.min = 0.0 self.info = str(dim)+"-dimensional Levy function \n" +\ "Global optimum: ?" self.integer = [] self.continuous = np.arange(0, dim) check_opt_prob(self)
def __init__(self, dim=10): self.xlow = np.zeros(dim) self.xup = 5 * np.ones(dim) self.dim = dim self.min = -0.835 self.integer = [] self.continuous = np.arange(0, dim) self.info = str(dim)+"-dimensional Keane bump function \n" +\ "Global optimum: -0.835 for large n" check_opt_prob(self)
def __init__(self, dim=10): self.xlow = -10.24 * np.ones(dim) self.xup = 10.24 * np.ones(dim) self.dim = dim self.info = str(dim)+"-dimensional Whitley function \n" +\ "Global optimum: f(1,1,...,1) = 0" self.min = 0 self.integer = [] self.continuous = np.arange(0, dim) check_opt_prob(self)
def __init__(self, dim=10): self.xlow = -100 * np.ones(dim) self.xup = 100 * np.ones(dim) self.dim = dim self.info = str(dim)+"-dimensional SchafferF7 function \n" +\ "Global optimum: f(0,0,...,0) = 0" self.min = 0 self.integer = [] self.continuous = np.arange(0, dim) check_opt_prob(self)
def __init__(self, dim=10): self.xlow = -512 * np.ones(dim) self.xup = 512 * np.ones(dim) self.dim = dim self.info = str(dim)+"-dimensional Schwefel function \n" +\ "Global optimum: f(420.968746,...,420.968746) = 0" self.min = 0 self.integer = [] self.continuous = np.arange(0, dim) check_opt_prob(self)
def __init__(self, dim=10): self.xlow = -5.12 * np.ones(dim) self.xup = 5.12 * np.ones(dim) self.dim = dim self.info = str(dim)+"-dimensional Exponential function \n" +\ "Global optimum: f(-5.12,-5.12,...,-5.12) = 0" self.min = 0 self.integer = [] self.continuous = np.arange(0, dim) check_opt_prob(self)
def __init__(self, dim=10): self.xlow = np.zeros(dim) self.xup = np.pi * np.ones(dim) self.dim = dim self.info = str(dim)+"-dimensional Michalewicz function \n" +\ "Global optimum: ??" self.min = np.NaN self.integer = [] self.continuous = np.arange(0, dim) check_opt_prob(self)
def __init__(self, dim=3): self.xlow = np.zeros(3) self.xup = np.ones(3) self.dim = 3 self.info = "3-dimensional Hartman function \nGlobal optimum: " +\ "f(0.114614,0.555649,0.852547) = -3.86278" self.min = -3.86278 self.integer = [] self.continuous = np.arange(0, 3) check_opt_prob(self)
def __init__(self, dim=6): self.xlow = np.zeros(6) self.xup = np.ones(6) self.dim = 6 self.info = "6-dimensional Hartman function \nGlobal optimum: " + \ "f(0.20169,0.150011,0.476874,0.275332,0.311652,0.6573) = -3.32237" self.min = -3.32237 self.integer = [] self.continuous = np.arange(0, 6) check_opt_prob(self)
def __init__(self, dim=10): self.xlow = -5 * np.ones(dim) self.xup = 5 * np.ones(dim) self.dim = dim self.info = str(dim)+"-dimensional Styblinski-Tang function \n" +\ "Global optimum: f(-2.903534,...,-2.903534) = " +\ str(-39.16599*dim) self.min = -39.16599 * dim self.integer = [] self.continuous = np.arange(0, dim) check_opt_prob(self)
def __init__(self, dim=5): self.xlow = np.zeros(5) self.xup = np.array([10, 10, 10, 1, 1]) self.dim = 5 self.min = -1 self.integer = np.arange(0, 3) self.continuous = np.arange(3, 5) self.info = str(self.dim)+"-dimensional Linear MI \n" +\ "Global optimum: f(1,0,0,0,0) = -1\n" +\ str(len(self.integer)) + " integer variables" check_opt_prob(self)
def __init__(self, dim=10): self.xlow = -1.28 * np.ones(dim) self.xup = 1.28 * np.ones(dim) self.dim = dim self.info = str(dim)+"-dimensional Quartic function \n" +\ "Global optimum: f(0,0,...,0) = 0+noise" self.min = 0 self.integer = [] self.continuous = np.arange(0, dim) self.prng = random.Random() self.prng.seed(time()) check_opt_prob(self)
def __init__(self, max_evals, opt_prob): check_opt_prob(opt_prob) if not isinstance(max_evals, int) and max_evals > 0: raise ValueError("max_evals must be an integer >= exp_des.num_pts") self.opt_prob = opt_prob self.max_evals = max_evals self.retry = RetryStrategy() for _ in range(max_evals): # Generate the random points x = np.random.uniform(low=opt_prob.lb, high=opt_prob.ub) proposal = self.propose_eval(x) self.retry.rput(proposal)
def check_input(self): """Check the inputs to the optimization strategt. """ if not isinstance(self.surrogate, Surrogate): raise ValueError("surrogate must implement Surrogate") if not isinstance(self.exp_design, ExperimentalDesign): raise ValueError("exp_design must implement ExperimentalDesign") if not isinstance(self.opt_prob, OptimizationProblem): raise ValueError("opt_prob must implement OptimizationProblem") check_opt_prob(self.opt_prob) if not self.asynchronous and self.batch_size is None: raise ValueError("You must specify batch size in synchronous mode " "(use 1 for serial)") if not isinstance(self.max_evals, int) and self.max_evals > 0 and \ self.max_evals >= self.exp_design.num_pts: raise ValueError("max_evals must be an integer >= exp_des.num_pts")
def test_all(): module = importlib.import_module("pySOT.optimization_problems") for name, obj in inspect.getmembers(pySOT.optimization_problems): if inspect.isclass(obj) and name != "OptimizationProblem": opt = getattr(module, name) opt = opt() assert (isinstance(opt, OptimizationProblem)) if hasattr(opt, 'minimum'): val = opt.eval(opt.minimum) assert(abs(val - opt.min) < 1e-3) else: val = opt.eval(np.zeros(opt.dim)) with pytest.raises(ValueError): # This should raise an exception opt.eval(np.zeros(opt.dim + 1)) # Sanity check all methods check_opt_prob(opt)
def test_all(): module = importlib.import_module("pySOT.optimization_problems") for name, obj in inspect.getmembers(pySOT.optimization_problems): if inspect.isclass(obj) and name != "OptimizationProblem": opt = getattr(module, name) opt = opt() assert isinstance(opt, OptimizationProblem) if hasattr(opt, "minimum"): val = opt.eval(opt.minimum) assert abs(val - opt.min) < 1e-3 else: val = opt.eval(np.zeros(opt.dim)) with pytest.raises(ValueError): # This should raise an exception opt.eval(np.zeros(opt.dim + 1)) # Sanity check all methods check_opt_prob(opt)