-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcostFunctions.py
More file actions
51 lines (40 loc) · 986 Bytes
/
costFunctions.py
File metadata and controls
51 lines (40 loc) · 986 Bytes
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
49
50
51
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 2 12:00:05 2022
@author: user
"""
import math
import numpy as np
def costFunction(x, problem = 'test'):
if problem == 'test':
return test(x)
if problem == 'kursawe':
return kursawe(x)
if problem == 'sch':
return kursawe(x)
# is not problem defined reurn none
return None
#%%
#x = [0.5,0.2,0.33,0.1]
def test(x):
n = len(x)
y = np.subtract(x,np.divide(1, math.sqrt(n)))
z = -np.sum(np.power(y,2))
f1 = 1 - math.exp(z)
#print(z1)
y = np.add(x,np.divide(1, math.sqrt(n)))
z = -np.sum(np.power(y,2))
f2 = 1 - math.exp(z)
return [f1, f2]
def kursawe(x):
s = 0
for i in range(len(x)-1):
s += -10*math.exp(-0.2*math.sqrt(x[i]**2 + x[i+1]**2))
f1 = s
s = 0
for i in range(len(x)):
s += abs(x[i])**0.8 + 5*math.sin(x[i]**3)
f2 = s
return [f1, f2]
def sch(x):
return [x**2, (x-2)**2]