# pip install pyswarms
from pyswarms.single.global_best import GlobalBestPSO
import numpy as np
# Define characteristics of variables:
x_min = [0, 0]
x_max = [1, 1]
bounds = (x_min, x_max)
dim = len(x_min)
# Define settings of the algorithm:
pop = 100
iterations = 250
options = {'c1': 0.5, 'c2': 0.4, 'w': 0.9}
# Define your optimization model:
def MyOptProb(x):
    y = 0 + x[:, 1] * (1.29 - 0)
    x = np.round(0 + x[:, 0] * (2 - 0))
    g1 = 5 / 10 * x + 3 / 10 * y - 1
    g2 = 2 / 9 * x + 7 / 9 * y - 1
    penalty = np.amax(np.array([np.zeros(pop), g1, g2]))
    return -(2 * x + 5 * y) + 150 * penalty ** 2
# Define a solution retriever:
def Results(obj, variables):
    x = round(0 + variables[0] * (2 - 0))
    y = 0 + variables[0] * (1.29 - 0)
    g1 = 5 * x + 3 * y - 10
    g2 = 2 * x + 7 * y - 9
    if g1 > 10e-6 or g2 > 10e-6:
        print ('infeasible')
    else:
        print ('feasible')
    print ('x:', x)
    print ('y:', y)
    print ('obj:', 2 * x + 5 * y)
# Model and solve the problem:
optimizer = GlobalBestPSO(n_particles=pop, dimensions=dim,options=options, bounds=bounds)
(obj, variables) = optimizer.optimize(MyOptProb, iterations)
# Display results:
Results(obj, variables)
# Results:
# 2023-06-28 00:54:33,837 - pyswarms.single.global_best - INFO - Optimize for 250 iters with {'c1': 0.5, 'c2': 0.4, 'w': 0.9}
# pyswarms.single.global_best: 100%||250/250, best_cost=-1.41
# 2023-06-28 00:54:33,995 - pyswarms.single.global_best - INFO - Optimization finished | best cost: -1.4144860266496924, best pos: [0.52082104 0.99006058]
# feasible
# x: 1
# y: 0.6718591376856157
# obj: 5.3592956884280785

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading