File size: 4,774 Bytes
524f6a3
 
 
 
 
2b3bceb
 
 
 
 
 
 
 
 
 
524f6a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7695e7
 
524f6a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892d6e9
 
 
 
 
 
 
 
 
524f6a3
 
 
 
 
 
 
 
 
 
 
 
 
 
b7695e7
 
 
524f6a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892d6e9
 
 
9a3e965
 
 
524f6a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy as np

# ------- BEGIN FUNCTIONS -------

# 2d Camel3
def testfunc_2d(x):
    if len(x.shape) == 2:
        N, n = x.shape
        axis = 1
    else:
        n = len(x)
        axis = 0
    y = np.sum(x**2, axis=axis, keepdims=True)
    return y

def camel3_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    return (2*X[:, 0]**2 - 1.05*X[:, 0]**4 + (X[:, 0]**6)/6 + X[:, 0]*X[:, 1] + X[:, 1]**2).reshape(-1, 1)

# 2d Camel16 
def camel16_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    return (4*X[:, 0]**2 - 2.1*X[:, 0]**4 + (X[:, 0]**6)/3 + X[:, 0]*X[:, 1] - 4*X[:, 1]**2 + 4*X[:, 1]**4).reshape(-1, 1)

# 2d Treccani
def treccani_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    return (X[:, 0]**4 + 4*X[:, 0]**3 + 4*X[:, 0]**2 + X[:, 1]**2).reshape(-1, 1)

# 2d Goldstein
def goldstein_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    x1 = X[:, 0]
    x2 = X[:, 1]
    
    term1 = 1 + (x1 + x2 + 1)**2 * (19 - 14*x1 + 3*x1**2 - 14*x2 + 6*x1*x2 + 3*x2**2)
    term2 = 30 + (2*x1 - 3*x2)**2 * (16 - 32*x1 + 12*x1**2 + 48*x2 - 36*x1*x2 + 27*x2**2)
    
    return (term1 * term2).reshape(-1, 1)

# 2d Branin
def branin_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    x1 = X[:, 0]
    x2 = X[:, 1]
    return ((x2 - (5.1 / (4 * np.pi**2)) * x1**2 + (5 / np.pi) * x1 - 6)**2 + (10 * (1 - (1 / (8 * np.pi))) * np.cos(x1)) + 10).reshape(-1, 1)

# 2d Rosenbrock
def rosenbrock_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    return (0.5 * (X[:, 0]**2 - X[:, 1])**2 + (X[:, 0] - 1)**2).reshape(-1, 1)

# 2d Ackley
def ackley_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    x1 = X[:, 0]
    x2 = X[:, 1]
    return (20 + np.exp(1) - 20 * np.exp(-0.2 * np.sqrt(0.5 * (x1**2 + x2**2))) - np.exp(0.5 * (np.cos(2 * np.pi * x1) + np.cos(2 * np.pi * x2)))).reshape(-1, 1)

# 10d Rosenbrock
def rosenbrock_10d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    return (np.sum(100 * (X[:, :-1]**2 - X[:, 1:])**2 + (X[:, :-1] - 1)**2, axis=1)).reshape(-1, 1)

# 10d Griewank
def griewank_10d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    d = X.shape[1]
    sum_sq = np.sum((X[:, 1:] - 100)**2, axis=1)
    prod_cos = np.prod(np.cos((X - 100) / np.sqrt(np.arange(1, d + 1))), axis=1)
    return (sum_sq / 4000 - prod_cos + 1).reshape(-1, 1)

# 10d Rastrigin
def rastrigin_10d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    return (np.sum(X**2 - 10 * np.cos(2 * np.pi * X) + 10, axis=1)).reshape(-1, 1)

# 2d Rastrigin
def rastrigin_2d(X):
    try:
        X.shape[1]
    except:
        X = np.array([X])
    return (np.sum(X**2 - 10 * np.cos(2 * np.pi * X) + 10, axis=1)).reshape(-1, 1)

# ------- END FUNCTIONS -------

if __name__ == "__main__":
    import sys
    import matplotlib.pyplot as plt

    if len(sys.argv) != 2:
        print("Usage: python functions.py [function_name]")
        sys.exit(1)

    function_name = sys.argv[1]
    # Evaluate the function based on the provided name
    f = globals().get(function_name)

    if f == camel3_2d or f == camel16_2d or f == treccani_2d:
        x1_min = x2_min = -5
        x1_max = x2_max = 5
    elif f == goldstein_2d:
        x1_min = x2_min = -2
        x1_max = x2_max = 2
    elif f == branin_2d:
        x1_min, x1_max = (-5, 10)
        x2_min, x2_max = (0, 15)
    elif f == rosenbrock_2d or f == rosenbrock_10d:
        x1_min = x2_min = -2.048
        x1_max = x2_max = 2.048
    elif f == ackley_2d:
        # normally it was (-15, 30) but the plot was bad in that case
        x1_min = x2_min = -30 
        x1_max = x2_max = 30
    elif f == griewank_10d:
        x1_min = x2_min = -600
        x1_max = x2_max = 600
    elif f == rastrigin_10d:
        x1_min = x2_min = -5.12
        x1_max = x2_max = 5.12
    elif f == rastrigin_2d:
        x1_min = x2_min = -5.12
        x1_max = x2_max = 5.12
    elif f == testfunc_2d:
        x1_min = x2_min = -5
        x1_max = x2_max = 5
    

    N = 100
    x1 = np.linspace(x1_min, x1_max, N)
    x2 = np.linspace(x2_min, x2_max, N)
    # x2 = np.random.uniform(0, 1, N) * x1

    X1, X2 = np.meshgrid(x1, x2)
    Y = f(np.column_stack((X1.ravel(), X2.ravel()))).reshape(X1.shape)

    fig = plt.figure(figsize=(11, 7))
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(X1, X2, Y, cmap='jet')

    ax.set_xlabel('x1')
    ax.set_ylabel('x2')
    ax.set_zlabel('y')
    plt.show()