-
Notifications
You must be signed in to change notification settings - Fork 1
/
fx.py
48 lines (40 loc) · 1.23 KB
/
fx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
This file contains objective functions for asynchronous racos.
Author:
Yu-Ren Liu
"""
from random import Random
import numpy as np
def sphere(solution):
"""
Sphere function for continuous optimization.
In order to simulate CPU-bound tasks, extra 100 thousand for loops are added
to extend evalution.
:param solution: a data structure containing x and fx
:return: value of fx
"""
a = 0
rd = Random()
for i in range(100000):
a += rd.uniform(0, 1)
x = solution.get_x()
value = sum([(i-0.2)*(i-0.2) for i in x])
return value
def ackley(solution):
"""
Ackley function for continuous optimization
In order to simulate CPU-bound tasks, extra 100 thousand for loops are added
to extend evalution.
:param solution: a data structure containing x and fx
:return: value of fx
"""
# a = 0
# rd = Random()
# for i in range(100000):
# a += rd.uniform(0, 1)
x = solution.get_x()
bias = 0.2
ave_seq = sum([(i - bias) * (i - bias) for i in x]) / len(x)
ave_cos = sum([np.cos(2.0 * np.pi * (i - bias)) for i in x]) / len(x)
value = -20 * np.exp(-0.2 * np.sqrt(ave_seq)) - np.exp(ave_cos) + 20.0 + np.e
return value