コード例 #1
0
def init():
    print("\nInitializing run...")
    rbf = RBFInterpolant(dim=ackley.dim,
                         lb=ackley.lb,
                         ub=ackley.ub,
                         kernel=CubicKernel(),
                         tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim,
                                   num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = SRBFStrategy(max_evals=max_evals,
                                       opt_prob=ackley,
                                       exp_design=slhd,
                                       surrogate=rbf,
                                       asynchronous=True)

    print("Number of workers: 1")
    print("Maximum number of evaluations: {}".format(max_evals))
    print("Strategy: {}".format(controller.strategy.__class__.__name__))
    print("Experimental design: {}".format(slhd.__class__.__name__))
    print("Surrogate: {}".format(rbf.__class__.__name__))

    # Wrap controller in checkpoint object
    controller = CheckpointController(controller, fname=fname)
    result = controller.run()
    print("Best value found: {0}".format(result.value))
    print("Best solution found: {0}\n".format(
        np.array_str(result.params[0],
                     max_line_width=np.inf,
                     precision=5,
                     suppress_small=True)))
コード例 #2
0
def init():
    print("\nInitializing run...")
    rbf = RBFInterpolant(
        dim=ackley.dim, kernel=CubicKernel(),
        tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(
        dim=ackley.dim, num_pts=2*(ackley.dim+1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = SRBFStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd,
        surrogate=rbf, asynchronous=True)

    print("Number of workers: 1")
    print("Maximum number of evaluations: {}".format(max_evals))
    print("Strategy: {}".format(controller.strategy.__class__.__name__))
    print("Experimental design: {}".format(slhd.__class__.__name__))
    print("Surrogate: {}".format(rbf.__class__.__name__))

    # Wrap controller in checkpoint object
    controller = CheckpointController(controller, fname=fname)
    result = controller.run()
    print('Best value found: {0}'.format(result.value))
    print('Best solution found: {0}\n'.format(
        np.array_str(result.params[0], max_line_width=np.inf,
                     precision=5, suppress_small=True)))
    def run(self):
        print("\n[%s] Running %s  pySOTOptimizer algorithm" %
              (datetime.now().strftime('%H:%M:%S'), self.alg_name))

        for i in range(self.n_runs):
            print("\t[%s] Starting Run %s" %
                  (datetime.now().strftime('%H:%M:%S'), i + 1))
            self.reset_run_counters(i)

            controller = SerialController(self.data.obj_function)
            strategy = SyncStrategyNoConstraints(
                worker_id=0,
                data=self.data,
                maxeval=self.eval_limit,
                nsamples=1,
                exp_design=self.initial_sampling_alg,
                response_surface=self.surrogate,
                sampling_method=self.candidate_sampling_alg)
            controller.strategy = strategy

            self.start_time = time.time()
            # Run the optimization strategy
            result = controller.run()
            self.final_time = time.time() - self.start_time

            self.update_results()
            self.print_run_results()

        self.avg_best_result /= self.n_runs
        self.avg_eval_count /= self.n_runs
        self.avg_time /= self.n_runs
        self.print_final_results()
コード例 #4
0
ファイル: test_fixed_strategy.py プロジェクト: dbindel/POAP
def main():
    "Testing routine."
    samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
    controller = SerialController(lambda x: (x-0.123)*(x-0.123))
    controller.strategy = FixedSampleStrategy(samples)
    result = controller.run()
    print("Final: {0:.3e} @ {1}".format(result.value, result.params))
コード例 #5
0
def main():
    "Testing routine."
    samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
    controller = SerialController(lambda x: (x - 0.123) * (x - 0.123))
    controller.strategy = FixedSampleStrategy(samples)
    result = controller.run()
    print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
コード例 #6
0
def main():
    "Testing routine."
    controller = SerialController(lambda x: (x-0.123)*(x-0.123))
    strategies = [MaxEvalStrategy(controller, 100),
                  FixedSampleStrategy(random_generator())]
    controller.strategy = SimpleMergedStrategy(controller, strategies)
    result = controller.run()
    print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
コード例 #7
0
ファイル: test_maxterm_strategy.py プロジェクト: dbindel/POAP
def main():
    "Testing routine."
    controller = SerialController(lambda x: (x-0.123)*(x-0.123))
    strategies = [MaxEvalStrategy(controller, 100),
                  FixedSampleStrategy(random_generator())]
    controller.strategy = SimpleMergedStrategy(controller, strategies)
    result = controller.run()
    print("Final: {0:.3e} @ {1}".format(result.value, result.params))
コード例 #8
0
def main():
    "Testing routine."
    controller = SerialController(lambda x: (x-0.123)*(x-0.123), skip=True)
    strategy = ThreadStrategy(controller,
                              lambda f: golden_section(f.blocking_eval,
                                                       0.0, 1.0, 20))
    controller.strategy = strategy
    result = controller.run()
    print("Final: {0:.3e} @ {1}".format(result.value, result.params))
コード例 #9
0
def resume():
    print("Resuming run...\n")
    controller = SerialController(ackley.eval)

    # Wrap controller in checkpoint object
    controller = CheckpointController(controller, fname=fname)
    result = controller.resume()
    print('Best value found: {0}'.format(result.value))
    print('Best solution found: {0}\n'.format(
        np.array_str(result.params[0], max_line_width=np.inf,
                     precision=5, suppress_small=True)))
コード例 #10
0
def resume():
    print("Resuming run...\n")
    controller = SerialController(ackley.eval)

    # Wrap controller in checkpoint object
    controller = CheckpointController(controller, fname=fname)
    result = controller.resume()
    print("Best value found: {0}".format(result.value))
    print("Best solution found: {0}\n".format(
        np.array_str(result.params[0],
                     max_line_width=np.inf,
                     precision=5,
                     suppress_small=True)))
コード例 #11
0
def test_lcb_serial():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim)
    slhd = SymmetricLatinHypercube(
        dim=ackley.dim, num_pts=2*(ackley.dim+1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = LCBStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd,
        surrogate=gp, asynchronous=True)
    controller.run()

    check_strategy(controller)
コード例 #12
0
ファイル: test_strategies.py プロジェクト: dme65/pySOT
def test_lcb_serial():
    max_evals = 50
    gp = GPRegressor(dim=ackley.dim)
    slhd = SymmetricLatinHypercube(
        dim=ackley.dim, num_pts=2*(ackley.dim+1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = LCBStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd,
        surrogate=gp, asynchronous=True)
    controller.run()

    check_strategy(controller)
コード例 #13
0
ファイル: test_strategies.py プロジェクト: dme65/pySOT
def test_srbf_serial():
    max_evals = 200
    rbf = RBFInterpolant(
        dim=ackley.dim, kernel=CubicKernel(),
        tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(
        dim=ackley.dim, num_pts=2*(ackley.dim+1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = SRBFStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd,
        surrogate=rbf, asynchronous=True)
    controller.run()

    check_strategy(controller)
コード例 #14
0
ファイル: test_controller.py プロジェクト: dme65/pySOT
def init_serial():
    rbf = RBFInterpolant(
        dim=ackley.dim, kernel=CubicKernel(),
        tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(
        dim=ackley.dim, num_pts=2*(ackley.dim+1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = DYCORSStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd,
        surrogate=rbf, asynchronous=True)

    # Wrap controller in checkpoint object
    controller = CheckpointController(controller, fname=fname)
    controller.run()
コード例 #15
0
def test_srbf_serial():
    max_evals = 200
    rbf = RBFInterpolant(
        dim=ackley.dim, kernel=CubicKernel(),
        tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(
        dim=ackley.dim, num_pts=2*(ackley.dim+1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = SRBFStrategy(
        max_evals=max_evals, opt_prob=ackley, exp_design=slhd,
        surrogate=rbf, asynchronous=True)
    controller.run()

    check_strategy(controller)
コード例 #16
0
def main():
    if not os.path.exists("./logfiles"):
        os.makedirs("logfiles")
    if os.path.exists("./logfiles/test_multisearch.log"):
        os.remove("./logfiles/test_multisearch.log")
    logging.basicConfig(filename="./logfiles/test_multisearch.log",
                        level=logging.INFO)

    print("\nNumber of threads: 1")
    print("Maximum number of evaluations: 500")
    print(
        "Search strategy: CandidateDYCORS, Genetic Algorithm, Multi-Start Gradient"
    )
    print("Experimental design: Latin Hypercube")
    print("Surrogate: Cubic RBF")

    nthreads = 1
    maxeval = 500
    nsamples = nthreads

    data = Ackley(dim=10)
    print(data.info)

    # Create a strategy and a controller
    sampling_method = [
        CandidateDYCORS(data=data, numcand=100 * data.dim),
        GeneticAlgorithm(data=data),
        MultiStartGradient(data=data)
    ]
    controller = SerialController(data.objfunction)
    controller.strategy = \
        SyncStrategyNoConstraints(
            worker_id=0, data=data,
            maxeval=maxeval, nsamples=nsamples,
            response_surface=RBFInterpolant(surftype=CubicRBFSurface, maxp=maxeval),
            exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim + 1)),
            sampling_method=MultiSampling(sampling_method, [0, 1, 2]))

    result = controller.run()
    best, xbest = result.value, result.params[0]

    print('Best value: {0}'.format(best))
    print('Best solution: {0}\n'.format(
        np.array_str(xbest,
                     max_line_width=np.inf,
                     precision=5,
                     suppress_small=True)))
コード例 #17
0
def main():
    if not os.path.exists("./logfiles"):
        os.makedirs("logfiles")
    if os.path.exists("./logfiles/test_multisearch.log"):
        os.remove("./logfiles/test_multisearch.log")
    logging.basicConfig(filename="./logfiles/test_multisearch.log",
                        level=logging.INFO)

    print("\nNumber of threads: 1")
    print("Maximum number of evaluations: 500")
    print("Search strategy: CandidateDYCORS, Genetic Algorithm, Multi-Start Gradient")
    print("Experimental design: Latin Hypercube")
    print("Surrogate: Cubic RBF")

    nthreads = 1
    maxeval = 500
    nsamples = nthreads

    data = Ackley(dim=10)
    print(data.info)

    # Create a strategy and a controller
    sampling_method = [CandidateDYCORS(data=data, numcand=100*data.dim),
                       GeneticAlgorithm(data=data), MultiStartGradient(data=data)]
    controller = SerialController(data.objfunction)
    controller.strategy = \
        SyncStrategyNoConstraints(
            worker_id=0, data=data,
            maxeval=maxeval, nsamples=nsamples,
            response_surface=RBFInterpolant(surftype=CubicRBFSurface, maxp=maxeval),
            exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim + 1)),
            sampling_method=MultiSampling(sampling_method, [0, 1, 2]))

    result = controller.run()
    best, xbest = result.value, result.params[0]

    print('Best value: {0}'.format(best))
    print('Best solution: {0}\n'.format(
        np.array_str(xbest, max_line_width=np.inf,
                     precision=5, suppress_small=True)))
コード例 #18
0
def test_checkpoint_serial():
    if os.path.isfile(fname):
        os.remove(fname)

    # Run for 1 seconds and kill the controller
    p = multiprocessing.Process(target=init_serial, args=())
    p.start()
    time.sleep(3)
    p.terminate()
    p.join()

    # Resume the run
    controller = SerialController(ackley.eval)
    resume(controller)
コード例 #19
0
def init_serial():
    rbf = RBFInterpolant(dim=ackley.dim,
                         kernel=CubicKernel(),
                         tail=LinearTail(ackley.dim))
    slhd = SymmetricLatinHypercube(dim=ackley.dim,
                                   num_pts=2 * (ackley.dim + 1))

    # Create a strategy and a controller
    controller = SerialController(ackley.eval)
    controller.strategy = DYCORSStrategy(max_evals=max_evals,
                                         opt_prob=ackley,
                                         exp_design=slhd,
                                         surrogate=rbf,
                                         asynchronous=True)

    # Wrap controller in checkpoint object
    controller = CheckpointController(controller, fname=fname)
    controller.run()
コード例 #20
0
    def run(self):
        """
        Run the optimization
        @return: Nothing
        """
        self.it = 0
        n = len(self.circuit.buses)
        m = len(self.circuit.branches)
        self.xlow = zeros(n)  # lower bounds
        self.xup = ones(n)  # upper bounds
        self.progress_signal.emit(0.0)
        self.progress_text.emit('Running stochastic voltage collapse...')
        self.results = MonteCarloResults(n, m, self.max_eval)

        # (1) Optimization problem
        # print(data.info)

        # (2) Experimental design
        # Use a symmetric Latin hypercube with 2d + 1 samples
        exp_des = SymmetricLatinHypercube(dim=self.dim, npts=2 * self.dim + 1)

        # (3) Surrogate model
        # Use a cubic RBF interpolant with a linear tail
        surrogate = RBFInterpolant(kernel=CubicKernel,
                                   tail=LinearTail,
                                   maxp=self.max_eval)

        # (4) Adaptive sampling
        # Use DYCORS with 100d candidate points
        adapt_samp = CandidateDYCORS(data=self, numcand=100 * self.dim)

        # Use the serial controller (uses only one thread)
        controller = SerialController(self.objfunction)

        # (5) Use the sychronous strategy without non-bound constraints
        strategy = SyncStrategyNoConstraints(worker_id=0,
                                             data=self,
                                             maxeval=self.max_eval,
                                             nsamples=1,
                                             exp_design=exp_des,
                                             response_surface=surrogate,
                                             sampling_method=adapt_samp)
        controller.strategy = strategy

        # Run the optimization strategy
        result = controller.run()

        # Print the final result
        print('Best value found: {0}'.format(result.value))
        print('Best solution found: {0}'.format(
            np.array_str(result.params[0],
                         max_line_width=np.inf,
                         precision=5,
                         suppress_small=True)))
        self.solution = result.params[0]

        # Extract function values from the controller
        self.optimization_values = np.array(
            [o.value for o in controller.fevals])

        # send the finnish signal
        self.progress_signal.emit(0.0)
        self.progress_text.emit('Done!')
        self.done_signal.emit()
コード例 #21
0
#Keep note of tests done a
data1 = Ackley(dim=2)
data2 = Hartman3()
data3 = Hartman6()
data4 = Sphere(dim=2)
data5 = Quartic()

#1-----------------------------------------
exp_des = SymmetricLatinHypercube(dim=data1.dim, npts=2 * data1.dim + 1)
surrogate = GPRegression(20)
#surrogate = RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval)

adapt_samp = MultiStartGradient(data=data1)

controller = SerialController(data1.objfunction)

# (5) Use the sychronous strategy without non-bound constraints
strategy = SyncStrategyNoConstraints(worker_id=0,
                                     data=data1,
                                     maxeval=maxeval,
                                     nsamples=1,
                                     exp_design=exp_des,
                                     response_surface=surrogate,
                                     sampling_method=adapt_samp)
controller.strategy = strategy

# Run the optimization strategy
result = controller.run()

# Print the final result
コード例 #22
0
    def run(self):
        """
        Function that optimizes a MicroGrid Object
        Args:

        Returns:

        """
        self.iteration = 0
        self.raw_results = list()

        self.progress_signal.emit(0)
        self.progress_text_signal.emit(
            'Optimizing facility sizes by surrogate optimization...')

        # (1) Optimization problem
        # print(data.info)

        # (2) Experimental design
        # Use a symmetric Latin hypercube with 2d + 1 samples
        exp_des = SymmetricLatinHypercube(dim=self.dim, npts=2 * self.dim + 1)

        # (3) Surrogate model
        # Use a cubic RBF interpolant with a linear tail
        surrogate = RBFInterpolant(kernel=CubicKernel,
                                   tail=LinearTail,
                                   maxp=self.max_eval)

        # (4) Adaptive sampling
        # Use DYCORS with 100d candidate points
        adapt_samp = CandidateDYCORS(data=self, numcand=100 * self.dim)

        # Use the serial controller (uses only one thread)
        controller = SerialController(self.objfunction)

        # (5) Use the synchronous strategy without non-bound constraints
        strategy = SyncStrategyNoConstraints(worker_id=0,
                                             data=self,
                                             maxeval=self.max_eval,
                                             nsamples=1,
                                             exp_design=exp_des,
                                             response_surface=surrogate,
                                             sampling_method=adapt_samp)
        controller.strategy = strategy

        # Run the optimization strategy
        result = controller.run()

        # Print the final result
        print('Best value found: {0}'.format(result.value))
        print('Best solution found: {0}'.format(
            np.array_str(result.params[0],
                         max_line_width=np.inf,
                         precision=5,
                         suppress_small=True)))
        self.solution = result.params[0]

        # Extract function values from the controller
        self.optimization_values = np.array(
            [o.value for o in controller.fevals])

        # turn the results into a DataFrame
        data = np.array(self.raw_results)
        self.x_fx = pd.DataFrame(data=data[:, 1:],
                                 index=data[:, 0],
                                 columns=[
                                     'Solar', 'Wind', 'Battery', 'LCOE',
                                     'Grid energy', 'Investment'
                                 ])
        self.x_fx.sort_index(inplace=True)

        self.progress_text_signal.emit('Done!')
        self.done_signal.emit()
コード例 #23
0
def main():
    "Testing routine."
    controller = SerialController(lambda x: (x - 0.123) * (x - 0.123))
    controller.strategy = CoroutineStrategy(golden_coroutine(0.0, 1.0, 20))
    result = controller.run()
    print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
コード例 #24
0
def main():
    "Testing routine."
    controller = SerialController(lambda x: (x-0.123)*(x-0.123))
    controller.strategy = CoroutineStrategy(golden_coroutine(0.0, 1.0, 20))
    result = controller.run()
    print("Final: {0:.3e} @ {1}".format(result.value, result.params))
コード例 #25
0
print data.info

# (2) Experimental design
# Use a symmetric Latin hypercube with 2d + 1 samples
exp_des = SymmetricLatinHypercube(dim=data.dim, npts=2 * data.dim + 1)

# (3) Surrogate model
# Use a cubic RBF interpolant with a linear tail
surrogate = RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval)

# (4) Adaptive sampling
# Use DYCORS with 100d candidate points
adapt_samp = CandidateDYCORS(data=data, numcand=100 * data.dim)

# Use the serial controller (uses only one thread)
controller = SerialController(data.objfunction)

# (5) Use the sychronous strategy without non-bound constraints
strategy = SyncStrategyNoConstraints(worker_id=0,
                                     data=data,
                                     maxeval=maxeval,
                                     nsamples=1,
                                     exp_design=exp_des,
                                     response_surface=surrogate,
                                     sampling_method=adapt_samp)
controller.strategy = strategy

# Run the optimization strategy
result = controller.run()

# Print the final result
コード例 #26
0
    def fit(self, X, y=None, **kwargs):
        """Run training with cross validation.

        :param X: training data
        :param **: parameters to be passed to GridSearchCV
        """

        # wrap for pySOT
        class Target(OptimizationProblem):
            def __init__(self, outer):
                self.outer = outer
                param_def = outer.param_def
                self.lb = np.array([param['lb'] for param in param_def])
                self.ub = np.array([param['ub'] for param in param_def])
                self.dim = len(param_def)
                self.int_var = np.array([
                    idx for idx, param in enumerate(param_def)
                    if param['integer']
                ])
                self.cont_var = np.array([
                    idx for idx, param in enumerate(param_def)
                    if idx not in self.int_var
                ])

            def eval_(self, x):
                print('Eval {0} ...'.format(x))
                param_def = self.outer.param_def
                outer = self.outer
                # prepare parameters grid for gridsearchcv
                param_grid = ({
                    param['name']:
                    [int(x[idx]) if param['integer'] else x[idx]]
                    for idx, param in enumerate(param_def)
                })
                # create gridsearchcv to evaluate the cv
                gs = GridSearchCV(outer.estimator,
                                  param_grid,
                                  refit=False,
                                  **outer.kwargs)
                # never refit during iteration, refit at the end
                gs.fit(X, y=y, **kwargs)
                gs_score = gs.best_score_
                # gridsearchcv score is better when greater
                if not outer.best_score_ or gs_score > outer.best_score_:
                    outer.best_score_ = gs_score
                    outer.best_params_ = gs.best_params_
                # also record history
                outer.params_history_.append(x)
                outer.score_history_.append(gs_score)
                print('Eval {0} => {1}'.format(x, gs_score))
                # pySOT score is the lower the better, so return the negated
                return -gs_score

        # pySOT routine
        # TODO: make this configurable
        target = Target(self)
        rbf = SurrogateUnitBox(RBFInterpolant(dim=target.dim,
                                              kernel=CubicKernel(),
                                              tail=LinearTail(target.dim)),
                               lb=target.lb,
                               ub=target.ub)
        slhd = SymmetricLatinHypercube(dim=target.dim,
                                       num_pts=2 * (target.dim + 1))

        # Create a strategy and a controller
        controller = SerialController(objective=target.eval_)
        controller.strategy = SRBFStrategy(max_evals=self.n_iter,
                                           batch_size=1,
                                           opt_prob=target,
                                           exp_design=slhd,
                                           surrogate=rbf,
                                           asynchronous=False)

        print('Maximum number of evaluations: {0}'.format(self.n_iter))
        print('Strategy: {0}'.format(controller.strategy.__class__.__name__))
        print('Experimental design: {0}'.format(slhd.__class__.__name__))
        print('Surrogate: {0}'.format(rbf.__class__.__name__))

        # Run the optimization strategy
        result = controller.run()

        print('Best value found: {0}'.format(result.value))
        print('Best solution found: {0}\n'.format(
            np.array_str(result.params[0],
                         max_line_width=np.inf,
                         precision=5,
                         suppress_small=True)))