Пример #1
0
def test_init_correct_dimensions_best_scores():
    """Test if the initialized best scores of each particle have the correct dimensions."""
    pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)

    assert len(pso._score_all) == 20
Пример #2
0
def test_init_correct_dimensions_best_score_glob():
    """Test if the initialized best score of all particles have the correct dimension."""
    pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)
    print('best score', pso.score)
    assert np.shape(pso.score) == ()
Пример #3
0
def test_init_correct_dimensions_best_coords_glob():
    """Test if the initialized best coordinates of all particles combined have the correct dimensions."""
    pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)

    assert pso.coords.shape == (2,)
Пример #4
0
def test_init_correct_dimensions_coords():
    """Test if the initialized coordinates have the correct dimension."""
    pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)

    assert pso._coords_all.shape == (20, 2)
Пример #5
0
def test_coord_history_correct_dimension():
    """Test if the saved particles coordinate history has the correct dimensions."""
    pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.optimize(params, iterations=20)

    history = pso.coords_history

    assert len(history) == 21
    assert all(history[i].shape == (20, 2) for i in range(20))
Пример #6
0
def test_init_different_random_state():
    """Test if the initialized coordinates are not deterministic if random state is not fixed."""
    pso = ParticleSwarmOptimizer(func=opt_func, maximize=False, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)
    coords0 = pso._coords_all
    pso.init(params=params, random_state=2)
    coords1 = pso._coords_all

    assert any(val0 != val1 for row0, row1 in zip(coords0, coords1) for val0, val1 in zip(row0, row1))
Пример #7
0
def test_update_monotonic_best_score_glob_maximize():
    """Test if the particle swarm optimizer monotonically converges for maximization problems."""
    pso = ParticleSwarmOptimizer(func=opt_func_inv, maximize=True, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)

    scores = [pso.score]
    for i in range(100):
        pso.update(params)
        scores.append(pso.score)

    assert all(scores[i+1] >= scores[i] for i in range(len(scores)-1))
Пример #8
0
def test_update_monotonic_best_scores_maximize():
    """Test if each particle of the particle swarm optimizer monotonically converges for maximization problems."""
    pso = ParticleSwarmOptimizer(func=opt_func_inv, maximize=True, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)

    scores = {p: [pso._score_all[p]] for p in range(20)}
    for i in range(100):
        pso.update(params)
        for particle in range(20):
            scores[particle] = scores[particle] + [pso._score_all[particle]]

    assert all(all(scores[particle][i+1] >= scores[particle][i] for i in range(len(scores[particle])-1))
               for particle in range(20))
Пример #9
0
# !/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np

from mlopt.optimization import ParticleSwarmOptimizer
from examples.visualization.gif import create_gif


def ackley_func(x, y):
    return -20*np.exp(-0.2*np.sqrt(0.5*(x**2+y**2)))-np.exp(0.5*(np.cos(2*np.pi*x)+np.cos(2*np.pi*y)))+np.exp(1)+20


if __name__ == '__main__':

    linspace = np.linspace(-4.0, 4.0, 80)

    bso = ParticleSwarmOptimizer(func=ackley_func, maximize=False, particles=20)
    bso.optimize(params={'x': (-4.0, 4.0), 'y': (-4.0, 4.0)}, inertia=0.8, c_cog=2, c_soc=2,
                 learning_rate=0.01, random_state=None, iterations=300)
    lst_coords = bso.coords_history

    create_gif(obj_func=ackley_func, linspace=linspace, coords=lst_coords, xlim=(-4, 4), ylim=(-4, 4))
Пример #10
0
import numpy as np

from mlopt.optimization import ParticleSwarmOptimizer
from examples.visualization.gif import create_gif


def obj_func(x, y):
    return (0.25 * np.sin(3 * np.pi * x) +
            np.sin(3 * np.pi * y)) - np.abs(1 - x) * np.abs(1 - y)


if __name__ == '__main__':

    linspace = np.linspace(0.0, 2, 41)

    bso = ParticleSwarmOptimizer(func=obj_func, maximize=True, particles=20)
    bso.optimize(params={
        'x': (0, 2),
        'y': (0, 2)
    },
                 inertia=0.8,
                 c_cog=2,
                 c_soc=2,
                 learning_rate=0.02,
                 random_state=None,
                 iterations=300)
    lst_coords = bso.coords_history

    create_gif(obj_func=obj_func,
               linspace=linspace,
               coords=lst_coords,
Пример #11
0
from mlopt.optimization import ParticleSwarmOptimizer


if __name__ == '__main__':
    iris = load_iris()

    df = pd.DataFrame(iris.data, columns=iris.feature_names)
    df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
    df = df.loc[(df.species == 'virginica') | (df.species == 'versicolor'), :]
    df['is_train'] = np.random.uniform(0, 1, len(df)) <= .75

    X_train, X_test = df[df['is_train'] == True], df[df['is_train'] == False]
    features = df.columns[:4]
    y_train, y_test = pd.factorize(X_train['species'])[0], pd.factorize(X_test['species'])[0]

    def get_score(max_depth, min_samples_leaf):
        clf = RandomForestClassifier(random_state=1, max_depth=int(max_depth), min_samples_leaf=int(min_samples_leaf))
        clf.fit(X_train[features], y_train)
        preds_test = clf.predict_proba(X_test[features])[:, 1]
        score = roc_auc_score(y_test, preds_test)
        print('AUC: {:0.4f}, max depth: {:0.0f}, min_samples_leaf: {:0.0f}'.format(score, max_depth, min_samples_leaf))
        return score

    bso = ParticleSwarmOptimizer(func=get_score, maximize=True, particles=10)
    params = {'max_depth': (1, 20), 'min_samples_leaf': (1, 20)}
    bso.optimize(params=params, random_state=1, iterations=10)

    print('Best AUC: {:0.4f}, max_depth: {}, min_samples_leaf: {}'.format(bso.score,
                                                                          int(bso.coords[0]),
                                                                          int(bso.coords[1])))