Skip to main content

partical swarm optimization constraint optimization solver

Project description

Particle Swarm Optimization Constraint Optimization Solver

Arguments

Name Type Default Value
particle_size int 2000
max_iter int 1000
sol_size int 7
fitness function null
constraints a list of functions null

Usage

equation

Transform constraints, it becomes:

equation

Note: In order to faster search optimal solutions, please initialize solutions with specific low and high.

import psoco
import math 

def objective(x):
    '''create objectives based on inputs x as 2D array'''
    return (x[:, 0] - 2) ** 2 + (x[:, 1] - 1) ** 2 


def constraints1(x):
    '''create constraint1 based on inputs x as 2D array'''
    return x[:, 0] - 2 * x[:, 1] + 1 


def constraints2(x):
    '''create constraint2 based on inputs x as 2D array'''
    return - (x[:, 0] - 2 * x[:, 1] + 1)


def constraints3(x):
    '''create constraint3 based on inputs x as 2D array'''
    return x[:, 0] ** 2 / 4. + x[:, 1] ** 2 - 1

def new_penalty_func(k):
    '''Easy Problem can use \sqrt{k}'''
    return math.sqrt(k)
    
constraints = [constraints1, constraints2, constraints3]
num_runs = 10
# random parameters lead to variations, so run several time to get mean
for _ in range(num_runs):
    pso = psoco.PSOCO(sol_size=2, fitness=objective, constraints=constraints)
    pso.h = new_penalty_func
    pso.init_Population(low=0, high=1) # x并集的上下限,默认为0和1
    pso.solve()
    # best solutions
    x = pso.gbest.reshape((1, -1))

Reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for psoco, version 0.0.8
Filename, size File type Python version Upload date Hashes
Filename, size psoco-0.0.8.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page