File: TasmanianGradientDescent.py

package info (click to toggle)
tasmanian 8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,852 kB
  • sloc: cpp: 34,523; python: 7,039; f90: 5,080; makefile: 224; sh: 64; ansic: 8
file content (281 lines) | stat: -rw-r--r-- 17,345 bytes parent folder | download | duplicates (3)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright (c) 2022, Miroslav Stoyanov & Weiwei Kong
#
# This file is part of
# Toolkit for Adaptive Stochastic Modeling And Non-Intrusive ApproximatioN: TASMANIAN
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
#    and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
#    or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# UT-BATTELLE, LLC AND THE UNITED STATES GOVERNMENT MAKE NO REPRESENTATIONS AND DISCLAIM ALL WARRANTIES, BOTH EXPRESSED AND
# IMPLIED. THERE ARE NO EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF
# THE SOFTWARE WILL NOT INFRINGE ANY PATENT, COPYRIGHT, TRADEMARK, OR OTHER PROPRIETARY RIGHTS, OR THAT THE SOFTWARE WILL
# ACCOMPLISH THE INTENDED RESULTS OR THAT THE SOFTWARE OR ITS USE WILL NOT RESULT IN INJURY OR DAMAGE. THE USER ASSUMES
# RESPONSIBILITY FOR ALL LIABILITIES, PENALTIES, FINES, CLAIMS, CAUSES OF ACTION, AND COSTS AND EXPENSES, CAUSED BY, RESULTING
# FROM OR ARISING OUT OF, IN WHOLE OR IN PART THE USE, STORAGE OR DISPOSAL OF THE SOFTWARE.

from ctypes import c_char_p, c_int, c_double, c_void_p, POINTER, CDLL, cast, CFUNCTYPE, Structure, RTLD_GLOBAL
from numpy.ctypeslib import as_ctypes
import numpy as np
import sys

from TasmanianConfig import __path_libdream__
from TasmanianConfig import TasmanianInputError as InputError

type_optim_obj_fn_single  = CFUNCTYPE(c_double, c_int, POINTER(c_double), POINTER(c_int))
type_optim_grad_fn_single = CFUNCTYPE(None, c_int, POINTER(c_double), POINTER(c_double), POINTER(c_int))
type_optim_proj_fn_single = CFUNCTYPE(None, c_int, POINTER(c_double), POINTER(c_double), POINTER(c_int))

# OptimizationStatus class (mirrors the C++ struct).
class optimization_status(Structure):
    pass
optimization_status._fields_ = [("performed_iterations", c_int), ("residual", c_double)]
def clean_status(input_status):
    return dict((field, getattr(input_status, field)) for field, _ in input_status._fields_)

pLibDTSG = CDLL(__path_libdream__, mode = RTLD_GLOBAL)

pLibDTSG.tsgGradientDescentState_Construct.restype = c_void_p
pLibDTSG.tsgGradientDescentState_Construct.argtypes = [c_int, POINTER(c_double), c_double]

pLibDTSG.tsgGradientDescentState_Destruct.argtypes = [c_void_p]

pLibDTSG.tsgGradientDescentState_GetNumDimensions.restype = c_int
pLibDTSG.tsgGradientDescentState_GetNumDimensions.argtypes = [c_void_p]
pLibDTSG.tsgGradientDescentState_GetAdaptiveStepsize.restype = c_double
pLibDTSG.tsgGradientDescentState_GetAdaptiveStepsize.argtypes = [c_void_p]
pLibDTSG.tsgGradientDescentState_GetX.argtypes = [c_void_p, POINTER(c_double)]

pLibDTSG.tsgGradientDescentState_SetAdaptiveStepsize.argtypes = [c_void_p, c_double]
pLibDTSG.tsgGradientDescentState_SetX.argtypes = [c_void_p, POINTER(c_double)]

pLibDTSG.tsgGradientDescent_AdaptProj.argtypes = [type_optim_obj_fn_single, type_optim_grad_fn_single, type_optim_proj_fn_single,
                                                  c_double, c_double, c_int, c_double, c_void_p, POINTER(c_int)]
pLibDTSG.tsgGradientDescent_AdaptProj.restype = optimization_status
pLibDTSG.tsgGradientDescent_Adapt.argtypes = [type_optim_obj_fn_single, type_optim_grad_fn_single, c_double, c_double, c_int,
                                              c_double, c_void_p, POINTER(c_int)]
pLibDTSG.tsgGradientDescent_Adapt.restype = optimization_status
pLibDTSG.tsgGradientDescent_Const.argtypes = [type_optim_grad_fn_single, c_double, c_int, c_double, c_void_p, POINTER(c_int)]
pLibDTSG.tsgGradientDescent_Const.restype = optimization_status

class GradientDescentState:
    '''
    Wrapper to class TasOptimization::GradientDescentState
    '''
    def __init__(self, aX0, fInitialStepsize):
        '''
        Constructs a new state with a given number of dimensions and particles.

        aX0              : a one-dimensional NumPy array representing the starting point.
        fInitialStepsize : initial adaptive stepsize.
        '''
        self.TasmanianGradientDescentState = True
        if (len(aX0.shape) != 1):
            raise InputError("aX0", "aX0 should be one-dimensional")
        self.pStatePntr = c_void_p(pLibDTSG.tsgGradientDescentState_Construct(aX0.shape[0], as_ctypes(aX0), fInitialStepsize))

    def __del__(self):
        '''
        Deletes an instance of the particle swarm state.
        '''
        pLibDTSG.tsgGradientDescentState_Destruct(self.pStatePntr)

    def getNumDimensions(self):
        '''
        Return the number of dimensions.
        '''
        return pLibDTSG.tsgGradientDescentState_GetNumDimensions(self.pStatePntr)

    def getAdaptiveStepsize(self):
        '''
        Return the adaptive stepsize.
        '''
        return pLibDTSG.tsgGradientDescentState_GetAdaptiveStepsize(self.pStatePntr)

    def getX(self):
        '''
        Return the current candidate point.
        '''
        iNumDims = self.getNumDimensions()
        aResult = np.zeros((iNumDims,), np.float64)
        pLibDTSG.tsgGradientDescentState_GetX(self.pStatePntr, as_ctypes(aResult))
        return aResult

    def setAdaptiveStepsize(self, fNewStepsize):
        '''
        Set new adaptive stepsize

        fNewStepsize : a double representing the new adaptive stepsize.
        '''
        pLibDTSG.tsgGradientDescentState_SetAdaptiveStepsize(self.pStatePntr, fNewStepsize)

    def setX(self, aXNew):
        '''
        Set new particle positions from a NumPy array.

        aXNew : a one-dimensional NumPy array representing the new candidate point.
        '''
        iNumDims = self.getNumDimensions()
        if (len(aXNew.shape) != 1):
            raise InputError("aXNew", "aXNew should be one-dimensional")
        if (aXNew.shape[0] != iNumDims):
            raise InputError("aXNew", "aXNew.shape[0] should match the number of dimensions")
        pLibDTSG.tsgGradientDescentState_SetX(self.pStatePntr, as_ctypes(aXNew))

# Helper methods for converting Python lambdas to C lambdas.
def convert_py_obj_fn_single(pObjectiveFunction, sCallerName):
    def cpp_obj_fn(num_dim, x_single_ptr, err_arr):
        err_arr[0] = 1
        aX = np.ctypeslib.as_array(x_single_ptr, (num_dim, ))
        aResult = pObjectiveFunction(aX)
        if not isinstance(aResult, float):
            print("ERROR: incorrect output from the objective function given to " + sCallerName +
                  "(), should be a 'float' but received '" + type(aResult).__name__ + "'")
            return 0
        err_arr[0] = 0
        return aResult
    return cpp_obj_fn

def convert_py_grad_fn_single(pGradientFunction, sCallerName):
    def cpp_grad_fn(num_dim, x_single_ptr, grad_ptr, err_arr):
        err_arr[0] = 1
        aX = np.ctypeslib.as_array(x_single_ptr, (num_dim,))
        aResult = pGradientFunction(aX)
        if aResult.shape != (num_dim, ):
            print("ERROR: incorrect output from the gradient function given to " + sCallerName +
                  "(), should be a NumPy array with shape (iNumDims,)")
            return
        aGrad = np.ctypeslib.as_array(grad_ptr, (num_dim,))
        aGrad[0:num_dim] = aResult[0:num_dim]
        err_arr[0] = 0
    return cpp_grad_fn

def convert_py_proj_fn_single(pProjectionFunction, sCallerName):
    def cpp_proj_fn(num_dim, x_single_ptr, proj_ptr, err_arr):
        err_arr[0] = 1
        aX = np.ctypeslib.as_array(x_single_ptr, (num_dim, ))
        aResult = pProjectionFunction(aX)
        if aResult.shape != (num_dim, ):
            print("ERROR: incorrect output from the projection function given to " + sCallerName +
                  "(), should be a NumPy array with shape (iNumDims,)")
            return
        aProj =  np.ctypeslib.as_array(proj_ptr, (num_dim,))
        aProj[0:num_dim] = aResult[0:num_dim]
        err_arr[0] = 0
    return cpp_proj_fn

# Main algorithms.
def AdaptiveProjectedGradientDescent(pObjectiveFunction, pGradientFunction, pProjectionFunction, fIncreaseCoeff, fDecreaseCoeff,
                                     iMaxIterations, fTolerance, oGradientDescentState):
    '''
    Wrapper around TasOptimization::GradientDescent().

    Runs at most iMaxIterations of the adaptive stepsize projected gradient descent algorithm on an input state oGradientDescentState
    to minimize the function pObjectiveFunction with gradient pGradientFunction and projection function pProjectionFunction. The
    parameters fIncreaseCoeff and fDecreaseCoeff control the evolution of the algorithm. The parameter fTolerance is a first-order
    stationarity tolerance used for early termination (see the C++ documentation for more details).

    Returns a dictionary containing the results of the run.

    pObjectiveFunction    : a Python lambda representing the objective function; it should take in a one-dimensional NumPy array
                            (x_single) and produce a double representing be the result of evaluating the objective function on
                            x_single; it is expected that x_single.shape[0] = .getNumDimensions().
    pGradientFunction     : a Python lambda representing the gradient of the objective function; it should take in a
                            one-dimensional NumPy array (x_single) and produce a one-dimensional NumPy representing the gradient
                            (grad) at x_single; it is expected that x_single.shape[0] = grad.shape[0] = .getNumDimensions().
    pProjectionFunction   : a Python lambda representing the domain projection function; it should take in a one-dimensional NumPy
                            array (x_single) and produce a one-dimensional NumPy representing the projection (proj) of x_single
                            onto the domain; it is expected that x_single.shape[0] = proj.shape[0] = .getNumDimensions().
    fIncreaseCoeff        : a positive double controlling how fast the stepsize is increased; it is expected to be greater than 1.
    fDecreaseCoeff        : a positive double controlling how fast the stepsize is decreased; it is expected to be greater than 1.
    iMaxIterations        : a positive integer representing the maximum number iterations the algorithm is run.
    fTolerance            : a positive double representing the stationarity tolerance for early tolerance.
    oGradientDescentState : an instance of the Python GradientDescentState class; it will contain the results of applying the algorithm.
    '''
    cpp_obj_fn = convert_py_obj_fn_single(pObjectiveFunction, "GradientDescent")
    cpp_grad_fn = convert_py_grad_fn_single(pGradientFunction, "GradientDescent")
    cpp_proj_fn = convert_py_proj_fn_single(pProjectionFunction, "GradientDescent")
    pErrorCode = (c_int * 1)()
    oStatus = pLibDTSG.tsgGradientDescent_AdaptProj(type_optim_obj_fn_single(cpp_obj_fn), type_optim_grad_fn_single(cpp_grad_fn),
                                                    type_optim_proj_fn_single(cpp_proj_fn), c_double(fIncreaseCoeff), c_double(fDecreaseCoeff),
                                                    c_int(iMaxIterations), c_double(fTolerance), oGradientDescentState.pStatePntr, pErrorCode)
    if pErrorCode[0] != 0:
        raise InputError("GradientDescent", "An error occurred during the call to Tasmanian.")
    return clean_status(oStatus)

def AdaptiveGradientDescent(pObjectiveFunction, pGradientFunction, fIncreaseCoeff, fDecreaseCoeff, iMaxIterations, fTolerance,
                            oGradientDescentState):
    '''
    Wrapper around TasOptimization::GradientDescent().

    Runs at most iMaxIterations of the adaptive stepsize (unconstrained) gradient descent algorithm on an input state oGradientDescentState
    to minimize the function pObjectiveFunction with gradient pGradientFunction and projection function pProjectionFunction. The
    parameters fIncreaseCoeff and fDecreaseCoeff control the evolution of the algorithm. The parameter fTolerance is a first-order
    stationarity tolerance used for early termination (see the C++ documentation for more details).

    Returns a dictionary containing the results of the run.

    pObjectiveFunction    : a Python lambda representing the objective function; it should take in a one-dimensional NumPy array
                            (x_single) and produce a double representing be the result of evaluating the objective function on
                            x_single; it is expected that x_single.shape[0] = .getNumDimensions().
    pGradientFunction     : a Python lambda representing the gradient of the objective function; it should take in a
                            one-dimensional NumPy array (x_single) and produce a one-dimensional NumPy representing the gradient
                            (grad) at x_single; it is expected that x_single.shape[0] = grad.shape[0] = .getNumDimensions().
    fIncreaseCoeff        : a positive double controlling how fast the stepsize is increased; it is expected to be greater than 1.
    fDecreaseCoeff        : a positive double controlling how fast the stepsize is decreased; it is expected to be greater than 1.
    iMaxIterations        : a positive integer representing the maximum number iterations the algorithm is run.
    fTolerance            : a positive double representing the stationarity tolerance for early tolerance.
    oGradientDescentState : an instance of the Python GradientDescentState class; it will contain the results of applying the algorithm.
    '''
    cpp_obj_fn = convert_py_obj_fn_single(pObjectiveFunction, "GradientDescent")
    cpp_grad_fn = convert_py_grad_fn_single(pGradientFunction, "GradientDescent")
    pErrorCode = (c_int * 1)()
    oStatus = pLibDTSG.tsgGradientDescent_Adapt(type_optim_obj_fn_single(cpp_obj_fn), type_optim_grad_fn_single(cpp_grad_fn),
                                                c_double(fIncreaseCoeff), c_double(fDecreaseCoeff), c_int(iMaxIterations),
                                                c_double(fTolerance), oGradientDescentState.pStatePntr, pErrorCode)
    if pErrorCode[0] != 0:
        raise InputError("GradientDescent", "An error occurred during the call to Tasmanian.")
    return clean_status(oStatus)

def GradientDescent(pGradientFunction, fStepsize, iMaxIterations, fTolerance, oGradientDescentState):
    '''
    Wrapper around TasOptimization::GradientDescent().

    Runs at most iMaxIterations of the constant stepsize (unconstrained) gradient descent algorithm on an input state oGradientDescentState
    to minimize the function pObjectiveFunction with gradient pGradientFunction and projection function pProjectionFunction.
    The parameter fTolerance is a first-order stationarity tolerance used for early termination (see the C++ documentation for
    more details).

    Returns a dictionary containing the results of the run.

    pGradientFunction     : a Python lambda representing the gradient of the objective function; it should take in a
                            one-dimensional NumPy array (x_single) and produce a one-dimensional NumPy representing the gradient
                            (grad) at x_single; it is expected that x_single.shape[0] = grad.shape[0] = .getNumDimensions().
    fStepsize             : a positive double representing the stepsize.
    iMaxIterations        : a positive integer representing the maximum number iterations the algorithm is run.
    fTolerance            : a positive double representing the stationarity tolerance for early tolerance.
    oGradientDescentState : an instance of the Python GradientDescentState class; it will contain the results of applying the algorithm.
    '''
    cpp_grad_fn = convert_py_grad_fn_single(pGradientFunction, "GradientDescent")
    pErrorCode = (c_int * 1)()
    oStatus = pLibDTSG.tsgGradientDescent_Const(type_optim_grad_fn_single(cpp_grad_fn), c_double(fStepsize), c_int(iMaxIterations),
                                                c_double(fTolerance), oGradientDescentState.pStatePntr, pErrorCode)
    if pErrorCode[0] != 0:
        raise InputError("GradientDescent", "An error occurred during the call to Tasmanian.")
    return clean_status(oStatus)