-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathproblem.py
More file actions
86 lines (67 loc) · 2.1 KB
/
problem.py
File metadata and controls
86 lines (67 loc) · 2.1 KB
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
"""
Implementation of the abstract optimization problem class
Authors: Tucker Hartland <hartland1@llnl.gov>
Nai-Yuan Chiang <chiang7@llnl.gov>
"""
import numpy as np
from numpy.random import uniform
from scipy.stats import qmc
# define the general optimization problem class
class Problem:
def __init__(self, ndim, xlimits, name=None):
self.ndim = ndim
self.xlimits = xlimits
assert self.xlimits.shape[0] == ndim
if name is None:
self.name = " "
else:
self.name = name
self.sampler = qmc.LatinHypercube(ndim)
def _evaluate(self, x: np.ndarray) -> np.ndarray:
"""
problem evaluation y = f(x) of
a scalar valued function f
Parameters
---------
x : ndarray[n, nx]
Returns
-------
ndarray[n, 1]
Function values
"""
raise NotImplementedError("Child class of hiopProblem should implement method _evaluate")
def evaluate(self, x: np.ndarray) -> np.ndarray:
"""
problem callback y = f(x) of
the scalar valued function f
Parameters
---------
x : ndarray[n, nx]
Returns
-------
ndarray[n, 1]
Function values (cast to reals)
"""
y = np.ndarray((x.shape[0], 1))
y[:,:] = self._evaluate(x)
return y
def sample(self, nsample: int) -> np.ndarray:
"""
generate nsample samples from domain defined
by xlimits
Parameters
-------
nsample : int
Returns
-------
ndarray[nsample, nx]
Samples from domain defined by xlimits
"""
# uniform
# xsample = np.zeros((nsample, self.ndim))
# for j in range(self.ndim):
# xsample[:, j] = uniform(self.xlimits[j][0], self.xlimits[j][1], size=nsample)
# from predefined sampler
xsample = self.sampler.random(nsample)
xsample = self.xlimits[:,0] + (self.xlimits[:,1] - self.xlimits[:,0]) * xsample
return xsample