https://github.com/agrover112/scikit-opt

Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)

https://github.com/agrover112/scikit-opt

Science Score: 33.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 3 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org, sciencedirect.com, springer.com, wiley.com, ieee.org, acm.org
  • Committers with academic emails
    2 of 17 committers (11.8%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.2%) to scientific vocabulary

Keywords from Contributors

networks
Last synced: 10 months ago · JSON representation

Repository

Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)

Basic Info
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Fork of guofei9987/scikit-opt
Created almost 5 years ago · Last pushed over 4 years ago
Metadata Files
Readme Contributing Funding License

README.md

scikit-opt

PyPI Build Status codecov License Python Platform fork Downloads Discussions

Swarm Intelligence in Python
(Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Algorithm, Immune Algorithm,Artificial Fish Swarm Algorithm in Python)

install

bash pip install scikit-opt

For the current developer version: bach git clone git@github.com:guofei9987/scikit-opt.git cd scikit-opt pip install .

Features

Feature1: UDF

UDF (user defined function) is available now!

For example, you just worked out a new type of selection function.
Now, your selection function is like this:
-> Demo code: examples/demogaudf.py#s1 ```python

step1: define your own operator:

def selectiontournament(algorithm, tournsize): FitV = algorithm.FitV selindex = [] for i in range(algorithm.sizepop): aspirantsindex = np.random.choice(range(algorithm.sizepop), size=tournsize) selindex.append(max(aspirantsindex, key=lambda i: FitV[i])) algorithm.Chrom = algorithm.Chrom[selindex, :] # next generation return algorithm.Chrom

```

Import and build ga
-> Demo code: examples/demogaudf.py#s2 ```python import numpy as np from sko.GA import GA, GA_TSP

demofunc = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2 ga = GA(func=demofunc, ndim=3, sizepop=100, maxiter=500, probmut=0.001, lb=[-1, -10, -5], ub=[2, 10, 2], precision=[1e-7, 1e-7, 1])

Regist your udf to GA -> Demo code: [examples/demo_ga_udf.py#s3](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L20) python ga.register(operatorname='selection', operator=selectiontournament, tourn_size=3) ```

scikit-opt also provide some operators
-> Demo code: examples/demogaudf.py#s4 ```python from sko.operators import ranking, selection, crossover, mutation

ga.register(operatorname='ranking', operator=ranking.ranking). \ register(operatorname='crossover', operator=crossover.crossover2point). \ register(operatorname='mutation', operator=mutation.mutation) Now do GA as usual -> Demo code: [examples/demo_ga_udf.py#s5](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L28) python bestx, besty = ga.run() print('bestx:', bestx, '\n', 'besty:', besty) ```

Until Now, the udf surport crossover, mutation, selection, ranking of GA scikit-opt provide a dozen of operators, see here

For advanced users:

-> Demo code: examples/demogaudf.py#s6 ```python class MyGA(GA): def selection(self, tournsize=3): FitV = self.FitV selindex = [] for i in range(self.sizepop): aspirantsindex = np.random.choice(range(self.sizepop), size=tournsize) selindex.append(max(aspirantsindex, key=lambda i: FitV[i])) self.Chrom = self.Chrom[sel_index, :] # next generation return self.Chrom

ranking = ranking.ranking

demofunc = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2 myga = MyGA(func=demofunc, ndim=3, sizepop=100, maxiter=500, lb=[-1, -10, -5], ub=[2, 10, 2], precision=[1e-7, 1e-7, 1]) bestx, besty = myga.run() print('bestx:', bestx, '\n', 'besty:', best_y) ```

feature2: continue to run

(New in version 0.3.6)
Run an algorithm for 10 iterations, and then run another 20 iterations base on the 10 iterations before: ```python from sko.GA import GA

func = lambda x: x[0] ** 2 ga = GA(func=func, n_dim=1) ga.run(10) ga.run(20) ```

feature3: 4-ways to accelerate

  • vectorization
  • multithreading
  • multiprocessing
  • cached

see https://github.com/guofei9987/scikit-opt/blob/master/examples/examplefunctionmodes.py

feature4: GPU computation

We are developing GPU computation, which will be stable on version 1.0.0
An example is already available: https://github.com/guofei9987/scikit-opt/blob/master/examples/demogagpu.py

Quick start

1. Differential Evolution

Step1:define your problem
-> Demo code: examples/demo_de.py#s1 ```python ''' min f(x1, x2, x3) = x1^2 + x2^2 + x3^2 s.t. x1x2 >= 1 x1x2 <= 5 x2 + x3 = 1 0 <= x1, x2, x3 <= 5 '''

def obj_func(p): x1, x2, x3 = p return x1 ** 2 + x2 ** 2 + x3 ** 2

constraint_eq = [ lambda x: 1 - x[1] - x[2] ]

constraint_ueq = [ lambda x: 1 - x[0] * x[1], lambda x: x[0] * x[1] - 5 ]

```

Step2: do Differential Evolution
-> Demo code: examples/demo_de.py#s2 ```python from sko.DE import DE

de = DE(func=objfunc, ndim=3, sizepop=50, maxiter=800, lb=[0, 0, 0], ub=[5, 5, 5], constrainteq=constrainteq, constraintueq=constraintueq)

bestx, besty = de.run() print('bestx:', bestx, '\n', 'besty:', besty)

```

2. Genetic Algorithm

Step1:define your problem
-> Demo code: examples/demo_ga.py#s1 ```python import numpy as np

def schaffer(p): ''' This function has plenty of local minimum, with strong shocks global minimum at (0,0) with value 0 https://en.wikipedia.org/wiki/Testfunctionsfor_optimization ''' x1, x2 = p part1 = np.square(x1) - np.square(x2) part2 = np.square(x1) + np.square(x2) return 0.5 + (np.square(np.sin(part1)) - 0.5) / np.square(1 + 0.001 * part2)

```

Step2: do Genetic Algorithm
-> Demo code: examples/demo_ga.py#s2 ```python from sko.GA import GA

ga = GA(func=schaffer, ndim=2, sizepop=50, maxiter=800, probmut=0.001, lb=[-1, -1], ub=[1, 1], precision=1e-7) bestx, besty = ga.run() print('bestx:', bestx, '\n', 'besty:', besty) ```

-> Demo code: examples/demo_ga.py#s3 ```python import pandas as pd import matplotlib.pyplot as plt

Yhistory = pd.DataFrame(ga.allhistoryY) fig, ax = plt.subplots(2, 1) ax[0].plot(Yhistory.index, Yhistory.values, '.', color='red') Yhistory.min(axis=1).cummin().plot(kind='line') plt.show() ```

Figure_1-1

2.2 Genetic Algorithm for TSP(Travelling Salesman Problem)

Just import the GA_TSP, it overloads the crossover, mutation to solve the TSP

Step1: define your problem. Prepare your points coordinate and the distance matrix.
Here I generate the data randomly as a demo:
-> Demo code: examples/demogatsp.py#s1 ```python import numpy as np from scipy import spatial import matplotlib.pyplot as plt

num_points = 50

pointscoordinate = np.random.rand(numpoints, 2) # generate coordinate of points distancematrix = spatial.distance.cdist(pointscoordinate, points_coordinate, metric='euclidean')

def caltotaldistance(routine): '''The objective function. input routine, return total distance. caltotaldistance(np.arange(numpoints)) ''' numpoints, = routine.shape return sum([distancematrix[routine[i % numpoints], routine[(i + 1) % numpoints]] for i in range(numpoints)])

```

Step2: do GA
-> Demo code: examples/demogatsp.py#s2 ```python

from sko.GA import GA_TSP

gatsp = GATSP(func=caltotaldistance, ndim=numpoints, sizepop=50, maxiter=500, probmut=1) bestpoints, bestdistance = gatsp.run()

```

Step3: Plot the result:
-> Demo code: examples/demogatsp.py#s3 python fig, ax = plt.subplots(1, 2) best_points_ = np.concatenate([best_points, [best_points[0]]]) best_points_coordinate = points_coordinate[best_points_, :] ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r') ax[1].plot(ga_tsp.generation_best_Y) plt.show()

GA_TPS

3. PSO(Particle swarm optimization)

3.1 PSO

Step1: define your problem:
-> Demo code: examples/demo_pso.py#s1 ```python def demo_func(x): x1, x2, x3 = x return x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2

```

Step2: do PSO
-> Demo code: examples/demo_pso.py#s2 ```python from sko.PSO import PSO

pso = PSO(func=demofunc, ndim=3, pop=40, maxiter=150, lb=[0, -1, 0.5], ub=[1, 1, 1], w=0.8, c1=0.5, c2=0.5) pso.run() print('bestx is ', pso.gbestx, 'besty is', pso.gbest_y)

```

Step3: Plot the result
-> Demo code: examples/demo_pso.py#s3 ```python import matplotlib.pyplot as plt

plt.plot(pso.gbestyhist) plt.show() ```

PSO_TPS

3.2 PSO with nonlinear constraint

If you need nolinear constraint like (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2<=0
Codes are like this: python constraint_ueq = ( lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2 , ) pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2] , constraint_ueq=constraint_ueq)

Note that, you can add more then one nonlinear constraint. Just add it to constraint_ueq

More over, we have an animation:
pso_ani
see examples/demopsoani.py

4. SA(Simulated Annealing)

4.1 SA for multiple function

Step1: define your problem
-> Demo code: examples/demo_sa.py#s1 ```python demo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2

**Step2**: do SA -> Demo code: [examples/demo_sa.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa.py#L3) python from sko.SA import SA

sa = SA(func=demofunc, x0=[1, 1, 1], Tmax=1, Tmin=1e-9, L=300, maxstaycounter=150) bestx, besty = sa.run() print('bestx:', bestx, 'besty', best_y)

```

Step3: Plot the result
-> Demo code: examples/demo_sa.py#s3 ```python import matplotlib.pyplot as plt import pandas as pd

plt.plot(pd.DataFrame(sa.bestyhistory).cummin(axis=0)) plt.show()

``` sa

Moreover, scikit-opt provide 3 types of Simulated Annealing: Fast, Boltzmann, Cauchy. See more sa

4.2 SA for TSP

Step1: oh, yes, define your problems. To boring to copy this step.

Step2: DO SA for TSP
-> Demo code: examples/demosatsp.py#s2 ```python from sko.SA import SA_TSP

satsp = SATSP(func=caltotaldistance, x0=range(numpoints), Tmax=100, Tmin=1, L=10 * numpoints)

bestpoints, bestdistance = satsp.run() print(bestpoints, bestdistance, caltotaldistance(bestpoints)) ```

Step3: plot the result
-> Demo code: examples/demosatsp.py#s3 ```python from matplotlib.ticker import FormatStrFormatter

fig, ax = plt.subplots(1, 2)

bestpoints = np.concatenate([bestpoints, [bestpoints[0]]]) bestpointscoordinate = pointscoordinate[bestpoints, :] ax[0].plot(satsp.bestyhistory) ax[0].setxlabel("Iteration") ax[0].setylabel("Distance") ax[1].plot(bestpointscoordinate[:, 0], bestpointscoordinate[:, 1], marker='o', markerfacecolor='b', color='c', linestyle='-') ax[1].xaxis.setmajorformatter(FormatStrFormatter('%.3f')) ax[1].yaxis.setmajorformatter(FormatStrFormatter('%.3f')) ax[1].setxlabel("Longitude") ax[1].setylabel("Latitude") plt.show()

``` sa

More: Plot the animation:

sa
see examples/demosatsp.py

5. ACA (Ant Colony Algorithm) for tsp

-> Demo code: examples/demoacatsp.py#s2 ```python from sko.ACA import ACA_TSP

aca = ACATSP(func=caltotaldistance, ndim=numpoints, sizepop=50, maxiter=200, distancematrix=distance_matrix)

bestx, besty = aca.run()

```

ACA

6. immune algorithm (IA)

-> Demo code: examples/demo_ia.py#s2 ```python

from sko.IA import IA_TSP

iatsp = IATSP(func=caltotaldistance, ndim=numpoints, sizepop=500, maxiter=800, probmut=0.2, T=0.7, alpha=0.95) bestpoints, bestdistance = iatsp.run() print('best routine:', bestpoints, 'bestdistance:', best_distance)

```

IA

7. Artificial Fish Swarm Algorithm (AFSA)

-> Demo code: examples/demo_afsa.py#s1 ```python def func(x): x1, x2 = x return 1 / x1 ** 2 + x1 ** 2 + 1 / x2 ** 2 + x2 ** 2

from sko.AFSA import AFSA

afsa = AFSA(func, ndim=2, sizepop=50, maxiter=300, maxtrynum=100, step=0.5, visual=0.3, q=0.98, delta=0.5) bestx, besty = afsa.run() print(bestx, best_y) ```

Projects using scikit-opt

Owner

  • Login: Agrover112
  • Kind: user

Humans trying to understand machines and people.

GitHub Events

Total
Last Year

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 279
  • Total Committers: 17
  • Avg Commits per committer: 16.412
  • Development Distribution Score (DDS): 0.233
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
guofei9987 g****7@f****m 214
郭飞 me@g****e 31
Ankit Grover a****2@g****m 9
zidong6344 5****4 4
ilikega 1****1@q****m 3
guofei9987 g****7@f****m 3
zhangxiao123qqq 4****q 3
samueljsluo x****h@g****m 2
Mingqing Liao l****7@1****m 2
Jialiang Yin j****n@i****e 1
Kutay Koralturk k****k@g****m 1
Tim Hatch t****m@t****m 1
王仔仔 5****a 1
xu-kq 5****q 1
lbiscuola 6****a 1
The Gitter Badger b****r@g****m 1
zidong6344 5****6@q****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: about 1 year ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels