예제 #1
0
    def gradient(self, inputs):

        # unpack inputs
        x1 = inputs.x1
        x2 = inputs['x2']
        x3 = inputs.x3

        # prep outputs
        grads = obunch()
        grads.f = obunch()
        grads.c = obunch()
        grads.c2 = obunch()

        # the math
        grads.f.x1 = 2 * x1
        grads.f.x2 = 2 * x2
        grads.f.x3 = 2 * x3
        grads.c.x1 = 1.
        grads.c.x2 = 1.
        grads.c.x3 = np.array([1., 0, 0])
        grads.c2.x1 = np.zeros([3, 1])
        grads.c2.x2 = np.zeros([3, 1])
        grads.c2.x3 = np.eye(3)

        return grads
예제 #2
0
 def gradient(self,inputs):
     
     # unpack inputs
     x1 = inputs.x1
     x2 = inputs['x2']
     x3 = inputs.x3  
     
     # prep outputs
     grads = obunch()
     grads.f  = obunch()
     grads.c  = obunch()
     grads.c2 = obunch()
     
     # the math
     grads.f.x1 = 2*x1
     grads.f.x2 = 2*x2 
     grads.f.x3 = 2*x3
     grads.c.x1 = 1.
     grads.c.x2 = 1.
     grads.c.x3 = np.array([1.,0,0])
     grads.c2.x1 = np.zeros([3,1])
     grads.c2.x2 = np.zeros([3,1])
     grads.c2.x3 = np.eye(3)
     
     return grads
예제 #3
0
    def __call__(self, variables):

        step = self.step

        variables = deepcopy(variables)

        if not isinstance(variables, obunch):
            variables = obunch(variables)

        # arrayify variables
        values_init = variables.pack_array('vector')
        values_init = atleast_2d_row(values_init)
        nx = values_init.shape[1]

        # prepare step
        if not isinstance(step, (array_type, list, tuple)):
            step = [step] * nx
        step = atleast_2d_row(step)
        if not step.shape[0] == 1:
            step = step.T

        values_run = np.vstack(
            [values_init,
             np.tile(values_init, [nx, 1]) + np.diag(step[0])])
        variables_run = [
            deepcopy(variables).unpack_array(val) for val in values_run
        ]

        # run the function
        #for i in variables_run: print i
        results = self.function(variables_run)
        #for i in results: print i

        results_values = np.vstack(
            [atleast_2d_row(res.pack_array('vector')) for res in results])

        # pack results
        gradients_values = (results_values[1:, :] -
                            results_values[None, 0, :]) / step.T
        gradients_values = np.ravel(gradients_values)

        variables = variables.unpack_array(values_init[0] * 0.0)

        gradients = deepcopy(results[0])
        if not isinstance(gradients, obunch):
            gradients = obunch(gradients)

        for i, g in gradients.items():
            if isinstance(g, array_type):
                g = atleast_2d_col(g)
            gradients[i] = deepcopy(variables)
            for j, v in gradients[i].items():
                if isinstance(v, array_type):
                    v = atleast_2d_row(v)
                gradients[i][j] = g * v

        gradients.unpack_array(gradients_values)

        return gradients
예제 #4
0
 def __call__(self,variables):
     
     step = self.step
     
     variables = deepcopy(variables)
     
     if not isinstance(variables,obunch):
         variables = obunch(variables)
         
     # arrayify variables
     values_init = variables.pack_array('vector')
     values_init = atleast_2d_row(values_init)
     nx = values_init.shape[1]
     
     # prepare step
     if not isinstance(step,(array_type,list,tuple)):
         step = [step] * nx
     step = atleast_2d_row(step)
     if not step.shape[0] == 1:
         step = step.T
     
     values_run = np.vstack([ values_init , 
                              np.tile(values_init,[nx,1]) + np.diag(step[0]) ])
     variables_run = [ deepcopy(variables).unpack_array(val) for val in values_run ]
         
     # run the function
     #for i in variables_run: print i
     results = self.function(variables_run)
     #for i in results: print i
     
     results_values = np.vstack([ atleast_2d_row( res.pack_array('vector') ) for res in results ])
     
     # pack results
     gradients_values = ( results_values[1:,:] - results_values[None,0,:] ) / step.T
     gradients_values = np.ravel( gradients_values )
     
     variables = variables.unpack_array( values_init[0] * 0.0 )
     
     gradients = deepcopy(results[0])
     if not isinstance(gradients,obunch):
         gradients = obunch(gradients)
         
     for i,g in gradients.items():
         if isinstance(g,array_type):
             g = atleast_2d_col(g)
         gradients[i] = deepcopy(variables)
         for j,v in gradients[i].items():
             if isinstance(v,array_type):
                 v = atleast_2d_row(v)
             gradients[i][j] = g * v
     
     gradients.unpack_array(gradients_values)
     
     return gradients
     
예제 #5
0
def build_surrogate(X,F,DF,XB,dLim=-2,nAS=None,**hypers):
    
    from VyPy.regression import active_subspace, gpr
    
    # learn the active subpace by gradients
    print 'Learn Active Subspaces ...'
    W,d = as_learn.gradient(DF)
    
    # pick number of dimensions
    nLim = np.sum( d > 10**dLim )
    if nAS is None:
        nAS = nLim
    else:
        if nAS > nLim:
            nAS = nLim
    
    print '  Number of dimensions = %i' % nAS
    
    # down select the number of dimensions
    U = W[:,0:nAS]
    
    # forward map training data to active space
    Y = as_project.simple(X,U)
    DFDY = as_project.simple(DF,U)
    YB = np.vstack([ np.min(Y,axis=0) , 
                     np.max(Y,axis=0) ]).T * 1.3
    
    # build the surrogate
    M_Y = gpr.library.Gaussian(YB,Y,F,DFDY, **hypers )
    #M_Y = gpr.library.Gaussian(YB,Y,F,None, **hypers )
    
    # convenience function handles
    g_y = M_Y.predict_YI
    g_x = Active_Subspace_Surrogate(g_y,U)
    
    # pack results
    results = obunch()
    results.tag = 'active subspace model'
    results.XB  = XB
    results.X   = X
    results.F   = F
    results.DF  = DF
    
    results.W   = W
    results.d   = d
    results.U   = U
    results.Y   = Y
    results.YB  = YB
    
    results.M_Y = M_Y
    results.g_x = g_x
    results.g_y = g_y
    
    return results
예제 #6
0
def test_func(inputs):
    x1 = inputs.x1
    x2 = inputs['x2']
    x3 = inputs.x3

    #print inputs

    f = x1**2 + x2**2 + np.sum(x3)**2
    c = x1 + x2 + x3[0]
    c2 = x3 - 1.

    outputs = obunch()
    outputs.f = f
    outputs.c = c
    outputs.c2 = c2

    #print outputs

    return outputs
예제 #7
0
 def function(self,inputs):
     
     # unpack inputs
     x1 = inputs.x1
     x2 = inputs['x2']
     x3 = inputs.x3
     
     # the math
     f = x1**2 + x2**2 + np.sum(x3)**2
     c = x1 + x2 + x3[0]
     c2 = x3 - 1.
     
     # pack outputs
     outputs = obunch()
     outputs.f  = f
     outputs.c  = c
     outputs.c2 = c2
     
     return outputs
예제 #8
0
def test_func(inputs):
    x1 = inputs.x1
    x2 = inputs['x2']
    x3 = inputs.x3
    
    #print inputs
    
    f = x1**2 + x2**2 + np.sum(x3)**2
    c = x1 + x2 + x3[0]
    c2 = x3 - 1.
    
    outputs = obunch()
    outputs.f  = f
    outputs.c  = c
    outputs.c2 = c2
    
    #print outputs
    
    return outputs
예제 #9
0
    def function(self, inputs):

        # unpack inputs
        x1 = inputs.x1
        x2 = inputs['x2']
        x3 = inputs.x3

        # the math
        f = x1**2 + x2**2 + np.sum(x3)**2
        c = x1 + x2 + x3[0]
        c2 = x3 - 1.

        # pack outputs
        outputs = obunch()
        outputs.f = f
        outputs.c = c
        outputs.c2 = c2

        return outputs
 def __call__(self,variables):
     
     step = self.step
     
     variables = deepcopy(variables)
     
     if not isinstance(variables,obunch):
         variables = obunch(variables)
         
     # arrayify variables
     values_init = variables.pack_array('vector')
     values_init = atleast_2d_row(values_init)
     nx = values_init.shape[1]
     
     # prepare step
     if not isinstance(step,(array_type,list,tuple)):
         step = [step] * nx
     step = atleast_2d_col(step)
     if not step.shape[1] == 1:
         step = step.T
     
     values_run = np.hstack([ values_init , 
                              np.tile(values_init,[nx,1]) + np.diag(step) ])
         
     # run the function
     results = self.function(values_run)
     
     # pack results
     gradients_values = ( results[1:,:] - results[None,0,:] ) / step
     gradients_values = np.ravel(gradients_values)
     
     variables.unpack_array( values_init * 0.0 )
     
     gradients = deepcopy(results[0])
     for k in gradients.keys():
         gradients[k] = deepcopy(variables)
     
     gradients.unpack_array(gradients_values)
     
     return gradients
     
예제 #11
0
    def __init__(self):

        self.verbose = True
        self.other_options = obunch()
예제 #12
0
def main():
    """ test the SLSQP Optimizer, with a test problem function """
    
    # ------------------------------------------------------------------
    #   Get the problem
    # ------------------------------------------------------------------
    
    #from problem_function import setup_problem
    from problem_evaluator import setup_problem
    problem = setup_problem()
    
    
    # Gradients
    evaluator = problem.objectives['f'].evaluator
    
    variables = obunch()
    variables.x1 = 0.
    variables.x2 = 0.
    variables.x3 = np.array([0.,0.,0.])
    
    grad_1 = evaluator.gradient(variables)
    
    print grad_1
    
    evaluator.gradient = opt.gradients.FiniteDifference(evaluator.function)
    
    grad_2 = evaluator.gradient(variables)
    
    
    print grad_2
    
    return
    
    # ------------------------------------------------------------------
    #   Setup Driver
    # ------------------------------------------------------------------    
    
    driver = opt.drivers.SLSQP()
    driver.max_iterations = 1000
    driver.verbose = True
    
    # ------------------------------------------------------------------
    #   Run the Problem
    # ------------------------------------------------------------------        

    results = driver.run(problem)
    
    print 'Results:'
    print results

    
    # ------------------------------------------------------------------
    #   Check Results
    # ------------------------------------------------------------------
    
    # the expected results
    truth = problem.truth
    
    # the checking function
    def check(a,b):
        return np.abs(a-b)
    
    delta = truth.do_recursive(check,results)
    
    print 'Error to Expected:'
    print delta
    
    assert np.all( delta.pack_array() < 1e-6 )
    assert len( delta.pack_array() ) == 10
    
    # done!
    return
예제 #13
0
def setup_problem():

    # initialize the problem
    problem = opt.Problem()

    # variables, list style
    problem.variables = [
        #   [ 'tag' , x0, (lb ,ub) , scl ],
        ['x1', 0., (-2., 2.), 1.0],
        ['x2', 0., (-2., 2.), 1.0],
    ]

    # variables, array style
    var = opt.Variable()
    var.tag = 'x3'
    var.initial = np.array([10., 10., 10.])
    ub = np.array([30.] * 3)
    lb = np.array([-1.] * 3)
    var.bounds = (lb, ub)
    var.scale = opt.scaling.Linear(scale=4.0, center=10.0)
    problem.variables.append(var)

    # objectives
    problem.objectives = [
        #   [ func   , 'tag', scl ],
        [test_func, 'f', 1.0],
    ]

    # constraints, list style
    problem.constraints = [
        #   [ func , ('tag' ,'><=', val), scl] ,
        [test_func, ('c', '=', 1.), 1.0],
    ]

    # constraints, array style
    con = opt.Equality()
    con.evaluator = test_func
    con.tag = 'c2'
    con.sense = '='
    con.edge = np.array([3., 3., 3.])
    problem.constraints.append(con)

    # print
    print problem

    # expected answer
    truth = obunch()
    truth.variables = obunch()
    truth.objectives = obunch()
    truth.equalities = obunch()

    truth.variables.x1 = -1.5
    truth.variables.x2 = -1.5
    truth.variables.x3 = np.array([4., 4., 4.])

    truth.objectives.f = 148.5

    truth.equalities.c = 1.0
    truth.equalities.c2 = np.array([3., 3., 3.])

    problem.truth = truth

    # done!
    return problem
예제 #14
0
 def __init__(self):
     
     self.verbose = True
     self.other_options = obunch()
예제 #15
0
def setup_problem():
    
    # initialize the problem
    problem = opt.Problem()
    
    # setup variables, list style
    problem.variables = [
    #   [ 'tag' , x0, (lb ,ub) , scl ],
        [ 'x1'  , 0., (-2.,2.) , 1.0 ],
        [ 'x2'  , 0., (-2.,2.) , 1.0 ], 
    ]
    
    # setup variables, arrays
    var = opt.Variable()
    var.tag     = 'x3'
    var.initial = np.array([10., 10., 10.])
    ub = np.array([30.]*3)
    lb = np.array([-1.]*3)
    var.bounds  = (lb,ub)
    var.scale   = opt.scaling.Linear(scale=4.0,center=10.0)
    problem.variables.append(var)
    
    # initialize evaluator
    test_eval = Test_Evaluator()
    
    # setup objective
    problem.objectives = [
    #   [ func   , 'tag', scl ],
        [ test_eval, 'f', 1.0 ],
    ]
    
    # setup constraint, list style
    problem.constraints = [
    #   [ func , ('tag' ,'><=', val), scl] ,
        [ test_eval, ('c','=',1.), 1.0 ],
    ]
    
    # setup constraint, array style
    con = opt.Equality()
    con.evaluator = test_eval
    con.tag       = 'c2'
    con.sense     = '='
    con.edge      = np.array([3.,3.,3.])
    problem.constraints.append(con)
  
    # print
    print problem  
    
    # expected answer
    truth = obunch()
    truth.variables  = obunch()
    truth.objectives = obunch()
    truth.equalities = obunch()
    
    truth.variables.x1 = -1.5
    truth.variables.x2 = -1.5
    truth.variables.x3 = np.array([ 4., 4., 4.])
    
    truth.objectives.f = 148.5
    
    truth.equalities.c  = 1.0
    truth.equalities.c2 = np.array([ 3., 3., 3.])
    
    problem.truth = truth    
  
    # done!
    return problem
예제 #16
0

from VyPy.data import obunch, odict
import matplotlib
from matplotlib.colors import hex2color

colors = obunch()

colors.sky_blue = odict()
colors.sky_blue[-2] = hex2color('#cadee5')
colors.sky_blue[-1] = hex2color('#84cfeb')
colors.sky_blue[0] = hex2color('#18aae0')
colors.sky_blue[1] = hex2color('#197192')
colors.sky_blue[2] = hex2color('#112128')

colors.hot_purple = odict()
colors.hot_purple[-2] = hex2color('#e5cadd')
colors.hot_purple[-1] = hex2color('#e77ac7')
colors.hot_purple[0] = hex2color('#e018a5')
colors.hot_purple[1] = hex2color('#841665')
colors.hot_purple[2] = hex2color('#281121')

colors.yellow_green = odict()
colors.yellow_green[-2] = hex2color('#dfe5ca')
colors.yellow_green[-1] = hex2color('#c7e77a')
colors.yellow_green[0] = hex2color('#a9e018')
colors.yellow_green[1] = hex2color('#678416')
colors.yellow_green[2] = hex2color('#222811')

예제 #17
0
def main():
    """ test the SLSQP Optimizer, with a test problem function """

    # ------------------------------------------------------------------
    #   Get the problem
    # ------------------------------------------------------------------

    #from problem_function import setup_problem
    from problem_evaluator import setup_problem
    problem = setup_problem()

    # Gradients
    evaluator = problem.objectives['f'].evaluator

    variables = obunch()
    variables.x1 = 0.
    variables.x2 = 0.
    variables.x3 = np.array([0., 0., 0.])

    grad_1 = evaluator.gradient(variables)

    print grad_1

    evaluator.gradient = opt.gradients.FiniteDifference(evaluator.function)

    grad_2 = evaluator.gradient(variables)

    print grad_2

    return

    # ------------------------------------------------------------------
    #   Setup Driver
    # ------------------------------------------------------------------

    driver = opt.drivers.SLSQP()
    driver.max_iterations = 1000
    driver.verbose = True

    # ------------------------------------------------------------------
    #   Run the Problem
    # ------------------------------------------------------------------

    results = driver.run(problem)

    print 'Results:'
    print results

    # ------------------------------------------------------------------
    #   Check Results
    # ------------------------------------------------------------------

    # the expected results
    truth = problem.truth

    # the checking function
    def check(a, b):
        return np.abs(a - b)

    delta = truth.do_recursive(check, results)

    print 'Error to Expected:'
    print delta

    assert np.all(delta.pack_array() < 1e-6)
    assert len(delta.pack_array()) == 10

    # done!
    return