Skip to content

Commit

Permalink
format codes
Browse files Browse the repository at this point in the history
  • Loading branch information
guofei9987 committed Jan 15, 2022
1 parent 1f0b768 commit 885d31b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ with github? Here are the steps you need to take.
The easiest way to do this is to either enable
[Travis-CI](https://travis-ci.org/) on your fork, or to make a pull
request and check there.
4. If it is a big, new feature please submit an example.
4. If it is a big, new feature please submit an example to path `/examples/` or `/tests/` and add it to `.travis.yml`.
5. [Submit a pull
request](https://help.github.com/articles/using-pull-requests)

Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from setuptools import setup, find_packages
from os import path as os_path
import os
import sko

this_directory = os_path.abspath(os_path.dirname(__file__))
this_directory = os.path.abspath(os.path.dirname(__file__))


# 读取文件内容
def read_file(filename):
with open(os_path.join(this_directory, filename), encoding='utf-8') as f:
with open(os.path.join(this_directory, filename), encoding='utf-8') as f:
long_description = f.read()
return long_description

Expand Down
9 changes: 6 additions & 3 deletions sko/GA.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def chrom2x(self, Chrom):

return self


class RCGA(GeneticAlgorithmBase):
"""real-coding genetic algorithm
Expand All @@ -277,12 +278,13 @@ class RCGA(GeneticAlgorithmBase):
ub : array_like
The upper bound of every variables of func
"""

def __init__(self, func, n_dim,
size_pop=50, max_iter=200,
prob_mut=0.001,
prob_cros=0.9,
lb=-1, ub=1,
):
):
super().__init__(func, n_dim, size_pop, max_iter, prob_mut)
self.lb, self.ub = np.array(lb) * np.ones(self.n_dim), np.array(ub) * np.ones(self.n_dim)
self.prob_cros = prob_cros
Expand All @@ -293,7 +295,7 @@ def crtbp(self):
self.Chrom = np.random.random([self.size_pop, self.n_dim])
return self.Chrom

def chrom2x(self,Chrom):
def chrom2x(self, Chrom):
X = self.lb + (self.ub - self.lb) * self.Chrom
return X

Expand Down Expand Up @@ -338,7 +340,7 @@ def mutation(self):
:return:
'''
#
size_pop, n_dim, Chrom= self.size_pop, self.n_dim, self.Chrom
size_pop, n_dim, Chrom = self.size_pop, self.n_dim, self.Chrom
for i in range(size_pop):
for j in range(n_dim):
r = np.random.random()
Expand Down Expand Up @@ -368,6 +370,7 @@ def mutation(self):
crossover = crossover_SBX
mutation = mutation


class GA_TSP(GeneticAlgorithmBase):
"""
Do genetic algorithm to solve the TSP (Travelling Salesman Problem)
Expand Down
39 changes: 21 additions & 18 deletions tests/test_demo_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# -*- coding: utf-8 -*-
# @Time : 2019/10/15
# @Author : github.com/Agrover112
from sko.demo_func import ackley, cigar, function_for_TSP, rastrigrin, rosenbrock, sixhumpcamel, sphere, schaffer, zakharov, shubert, griewank, rastrigrin, rosenbrock, sixhumpcamel, zakharov, ackley, cigar
from sko.demo_func import ackley, cigar, function_for_TSP, rastrigrin, rosenbrock, sixhumpcamel, sphere, schaffer, \
zakharov, shubert, griewank, rastrigrin, rosenbrock, sixhumpcamel, zakharov, ackley, cigar


class TestDemoFunc(unittest.TestCase):
Expand All @@ -11,41 +12,43 @@ def test_function_for_TSP(self):
pass

def test_sphere(self):
self.assertEqual(sphere((0, 0)), 0.0,msg="sphere failed for 2 arguments ")
self.assertEqual(sphere((0, 0)), 0.0, msg="sphere failed for 2 arguments")

def test_schaffer(self):
self.assertEqual(schaffer((0, 0)), 0.0,msg="schaffer failed for 2 arguments ")
self.assertEqual(schaffer((0, 0)), 0.0, msg="schaffer failed for 2 arguments")

def test_shubert(self):
self.assertEqual(
shubert((-7.08350643, -7.70831395)), -186.7309088309155 ,msg="shubert failed for 2 arguments ")
shubert((-7.08350643, -7.70831395)), -186.7309088309155, msg="shubert failed for 2 arguments")

def test_griewank(self):
self.assertEqual(griewank((0, 0)), 0.0,msg="griewank failed for 2 arguments ")
self.assertEqual(griewank((0, 0, 0)), 0.0,msg="griewank failed for 3 arguments ")
self.assertEqual(griewank((0, 0)), 0.0, msg="griewank failed for 2 arguments ")
self.assertEqual(griewank((0, 0, 0)), 0.0, msg="griewank failed for 3 arguments ")

def test_rastrigrin(self):
self.assertEqual(rastrigrin((0, 0)), 0.0,msg="rastrigrin failed for 2 arguments ")
self.assertEqual(rastrigrin((0, 0, 0)), 0.0,msg="rastrigrin failed for 3 arguments ")
self.assertEqual(rastrigrin((0, 0)), 0.0, msg="rastrigrin failed for 2 arguments ")
self.assertEqual(rastrigrin((0, 0, 0)), 0.0, msg="rastrigrin failed for 3 arguments ")

def test_rosenbrock(self):
self.assertEqual(rosenbrock((1, 1, 1)), 0.0,msg="rosenbrock failed for 3 arguments ")
self.assertEqual(rosenbrock((1, 1)), 0.0,msg="rosenbrock failed for 2 arguments ")
self.assertEqual(rosenbrock((1, 1, 1)), 0.0, msg="rosenbrock failed for 3 arguments ")
self.assertEqual(rosenbrock((1, 1)), 0.0, msg="rosenbrock failed for 2 arguments ")

def test_zakharov(self):
self.assertEqual(zakharov((0, 0)), 0.0,msg="zakharov failed for 2 arguments ")
self.assertEqual(zakharov((0, 0, 0)), 0.0,msg="zakharov failed for 3 arguments ")
self.assertEqual(zakharov((0, 0)), 0.0, msg="zakharov failed for 2 arguments ")
self.assertEqual(zakharov((0, 0, 0)), 0.0, msg="zakharov failed for 3 arguments ")

def test_ackley(self):
self.assertEqual(ackley((0, 0)),-200.0,msg="ackley failed for 2 arguments ")
self.assertEqual(ackley((0, 0)), -200.0, msg="ackley failed for 2 arguments ")

def test_cigar(self):
self.assertEqual(cigar((0, 0)), 0.0,msg="cigar failed for 2 arguments ")
self.assertEqual(cigar((0, 0, 0, 0)), 0.0,msg="cigar failed for 4 arguments ")
self.assertEqual(cigar((0, 0)), 0.0, msg="cigar failed for 2 arguments ")
self.assertEqual(cigar((0, 0, 0, 0)), 0.0, msg="cigar failed for 4 arguments ")

def test_sixhumpcamel(self):
self.assertEqual(sixhumpcamel(
(-0.08984201368301331, 0.7126564032704135)), -1.0316284534898774,msg="sixhumpcamel failed for 2 arguments ")

(-0.08984201368301331, 0.7126564032704135)), -1.0316284534898774,
msg="sixhumpcamel failed for 2 arguments ")


if __name__ == '__main__':
unittest.main()
unittest.main()
5 changes: 2 additions & 3 deletions tests/test_x2gray.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ def test_x2gray(self):

value = ga.chrom2x(ga.Chrom)
chrom2 = x2gray(x=value, n_dim=n_dim, lb=lb, ub=ub, precision=precision)
self.assertTrue(np.allclose(ga.Chrom, chrom2),msg='x2gray error')

self.assertTrue(np.allclose(ga.Chrom, chrom2), msg='x2gray error')


if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 885d31b

Please sign in to comment.