Skip to content

Commit

Permalink
optimization driver changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aerialhedgehog committed Jun 4, 2015
1 parent ac2a705 commit b6a14ed
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 337 deletions.
210 changes: 105 additions & 105 deletions tests/optimize/driver_findiff.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
# example_test_script.py
#
# Created: Your Name, Jun 2014
# Modified:


# ----------------------------------------------------------------------
# Imports
# ----------------------------------------------------------------------

import VyPy
import numpy as np

import VyPy.optimize as opt

from VyPy.data import obunch


# ----------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------

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

#: def main()


# ----------------------------------------------------------------------
# Call Main
# ----------------------------------------------------------------------

if __name__ == '__main__':

main()
# example_test_script.py
#
# Created: Your Name, Jun 2014
# Modified:


# ----------------------------------------------------------------------
# Imports
# ----------------------------------------------------------------------

import VyPy
import numpy as np

import VyPy.optimize as opt

from VyPy.data import obunch


# ----------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------

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

#: def main()


# ----------------------------------------------------------------------
# Call Main
# ----------------------------------------------------------------------

if __name__ == '__main__':

main()
Loading

0 comments on commit b6a14ed

Please sign in to comment.