forked from stijnvanhoey/Optimization_SCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCE_functioncall.py
165 lines (138 loc) · 4.9 KB
/
SCE_functioncall.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
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
#-------------------------------------------------------------------------------
# Name: SCE_Python_shared version - helpfunctions
# This is the implementation for the SCE algorithm,
# written by Q.Duan, 9/2004 - converted to python by Van Hoey S. 2011
# Purpose:
#
# Author: VHOEYS
#
# Created: 11/10/2011
# Copyright: (c) VHOEYS 2011
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import sys
import numpy as np
##print sys.path[0]
##sys.path.append('D:\Modellen\Snippets_for_all\SCE')
################################################################################
## Modrun is the user part of the script:
## gets number of parameters and an array x with these parameters
## let your function or model run/evaluate and give the evaluation value back
##------------------------------------------------------------------------------
##def Modrun(npar,x):
## '''
## User has to put in his model to run
## '''
##
## return f
################################################################################
################################################################################
## Sampling called from SCE
################################################################################
def SampleInputMatrix(nrows,npars,bu,bl,iseed,distname='randomUniform'):
'''
Create inputparameter matrix for nrows simualtions,
for npars with bounds ub and lb (np.array from same size)
distname gives the initial sampling ditribution (currently one for all parameters)
returns np.array
'''
np.random.seed(iseed)
x=np.zeros((nrows,npars))
bound = bu-bl
for i in range(nrows):
## x[i,:]= bl + DistSelector([0.0,1.0,npars],distname='randomUniform')*bound #only used in full Vhoeys-framework
x[i,:]= bl + np.random.rand(1,npars)*bound
return x
################################################################################
## TESTFUNCTIONS TO CHECK THE IMPLEMENTATION OF THE SCE ALGORITHM
################################################################################
def testfunctn1(nopt,x):
'''
This is the Goldstein-Price Function
Bound X1=[-2,2], X2=[-2,2]
Global Optimum: 3.0,(0.0,-1.0)
'''
x1 = x[0]
x2 = x[1]
u1 = (x1 + x2 + 1.0)**2
u2 = 19. - 14.*x1 + 3.*x1**2 - 14.*x2 + 6.*x1*x2 +3.*x2**2
u3 = (2.*x1 - 3.*x2)**2
u4 = 18. - 32.*x1 + 12.*x1**2 + 48.*x2 -36.*x1*x2 + 27.*x2**2
u5 = u1 * u2
u6 = u3 * u4
f = (1. + u5) * (30. + u6)
return f
def testfunctn2(nopt,x):
'''
% This is the Rosenbrock Function
% Bound: X1=[-5,5], X2=[-2,8]; Global Optimum: 0,(1,1)
bl=[-5 -5]; bu=[5 5]; x0=[1 1];
'''
x1 = x[0]
x2 = x[1]
a = 100.0
f = a * (x2 - x1**2)**2 + (1 - x1)**2
return f
def testfunctn3(nopt,x):
'''3
% This is the Six-hump Camelback Function.
% Bound: X1=[-5,5], X2=[-5,5]
% True Optima: -1.031628453489877, (-0.08983,0.7126), (0.08983,-0.7126)
'''
x1 = x[0]
x2 = x[1]
f = (4 - 2.1*x1**2 + x1**4/3)*x1**2 + x1*x2 + (-4 + 4*x2**2)*x2**2
return f
def testfunctn4(nopt,x):
'''4
% This is the Rastrigin Function
% Bound: X1=[-1,1], X2=[-1,1]
% Global Optimum: -2, (0,0)
'''
x1 = x[0]
x2 = x[1]
f = x1**2 + x2**2 - np.cos(18.0*x1) - np.cos(18.0*x2)
return f
def testfunctn5(nopt,x):
'''
This is the Griewank Function (2-D or 10-D)
Bound: X(i)=[-600,600], for i=1,2,...,10
Global Optimum: 0, at origin
'''
if nopt==2:
d = 200.0
else:
d = 4000.0
u1 = 0.0
u2 = 1.0
for j in range(nopt):
u1 = u1 + x[j]**2/d
u2 = u2 * np.cos(x[j]/np.sqrt(float(j+1)))
f = u1 - u2 + 1
return f
################################################################################
## FUNCTION CALL FROM SCE-ALGORITHM !!
################################################################################
def EvalObjF(npar,x,testcase=True,testnr=1):
'''
The SCE algorithm calls this function which calls the model itself
(minimalisation of function output or evaluation criterium coming from model)
and returns the evaluation function to the SCE-algorithm
If testcase =True, one of the example tests are run
'''
## print 'testnummer is %d' %testnr
if testcase==True:
if testnr==1:
return testfunctn1(npar,x)
if testnr==2:
return testfunctn2(npar,x)
if testnr==3:
return testfunctn3(npar,x)
if testnr==4:
return testfunctn4(npar,x)
if testnr==5:
return testfunctn5(npar,x)
else:
return Modrun(npar,x) #Welk model/welke objfunctie/welke periode/.... users keuze!