def est_plot_last_search( self ): import solverLib from solverLib import solve_via_Newtons_method, rand, SearchAnalyticsWrapper maxStep = [0.5, 0.5] xRoots = solve_via_Newtons_method( SearchAnalyticsWrapper(self.f1), rand(2)+3, maxStep, x_tol=0, debugPrintLevel=3, f_tol=10**-12) print( solverLib.analytics['lastSearch'] ) solverLib.analytics['lastSearch'].plot()
def est_plot_last_search( self ): import solverLib from solverLib import solve_via_Newtons_method, rand, SearchAnalyticsWrapper maxStep = [0.5, 0.5] xRoots = solve_via_Newtons_method( SearchAnalyticsWrapper(self.f1), rand(2)+3, maxStep, x_tol=0, debugPrintLevel=3, f_tol=10**-12) print( solverLib.analytics['lastSearch'] ) solverLib.analytics['lastSearch'].plot()
def test_solve_via_Newtons_method(self): from solverLib import solve_via_Newtons_method, rand maxStep = [0.5, 0.5] xMin = solve_via_Newtons_method(self.f1, rand(2) + 3, maxStep, x_tol=0, debugPrintLevel=0) self.assertAllClose(xMin, [2, -1])
def test_gradient_approx_1( self ): 'test on a function which returns a single value' from solverLib import GradientApproximatorRandomPoints, GradientApproximatorForwardDifference, GradientApproximatorCentralDifference, rand grad_f_rp = GradientApproximatorRandomPoints(self.f2) grad_f_fd = GradientApproximatorForwardDifference(self.f2) grad_f_cd = GradientApproximatorCentralDifference(self.f2) for i in range(2): X = rand(2)*10-5 #print(' X %s' % X) #print(' grad_f(X) analytical: %s' % grad_f2(X)) #print(' grad_f(X) randomPoints: %s' % grad_f_rp(X)) #print(' grad_f(X) forwardDiff.: %s' % grad_f_fd(X)) #print(' grad_f(X) centralDiff.: %s' % grad_f_cd(X)) #print(' norm(analytical-randomPoints) %e' % norm(grad_f2(X) - grad_f_rp(X)) ) self.assertAllClose( self.grad_f2(X), grad_f_rp(X) ) self.assertAllClose( self.grad_f2(X), grad_f_fd(X) ) self.assertAllClose( self.grad_f2(X), grad_f_cd(X) )
def test_gradient_approx_1(self): 'test on a function which returns a single value' from solverLib import GradientApproximatorRandomPoints, GradientApproximatorForwardDifference, GradientApproximatorCentralDifference, rand grad_f_rp = GradientApproximatorRandomPoints(self.f2) grad_f_fd = GradientApproximatorForwardDifference(self.f2) grad_f_cd = GradientApproximatorCentralDifference(self.f2) for i in range(2): X = rand(2) * 10 - 5 #print(' X %s' % X) #print(' grad_f(X) analytical: %s' % grad_f2(X)) #print(' grad_f(X) randomPoints: %s' % grad_f_rp(X)) #print(' grad_f(X) forwardDiff.: %s' % grad_f_fd(X)) #print(' grad_f(X) centralDiff.: %s' % grad_f_cd(X)) #print(' norm(analytical-randomPoints) %e' % norm(grad_f2(X) - grad_f_rp(X)) ) self.assertAllClose(self.grad_f2(X), grad_f_rp(X)) self.assertAllClose(self.grad_f2(X), grad_f_fd(X)) self.assertAllClose(self.grad_f2(X), grad_f_cd(X))
def test_gradient_approx_2( self ): 'test on a function which returns multiple values' from solverLib import GradientApproximatorRandomPoints, GradientApproximatorForwardDifference, GradientApproximatorCentralDifference, rand grad_f_rp = GradientApproximatorRandomPoints( self.f1 ) grad_f_fd = GradientApproximatorForwardDifference( self.f1 ) grad_f_cd = GradientApproximatorCentralDifference( self.f1 ) for i in range(2): X = rand(2)*10-5 #print(' X %s' % X) #print(' grad_f(X) analytical:') #prettyPrintArray(grad_f1(X), toStdOut, ' ','%1.6e') #print(' grad_f(X) randomPoints:') #prettyPrintArray(grad_f_rp(X), toStdOut, ' ','%1.6e') #print(' grad_f(X) forwardDiff:') #prettyPrintArray(grad_f_fd(X), toStdOut, ' ','%1.6e') #print(' grad_f(X) centralDiff:') #prettyPrintArray(grad_f_cd(X), toStdOut, ' ','%1.6e') #print(' error rp %e' % norm(grad_f1(X) - grad_f_rp(X))) self.assertAllClose( self.grad_f1(X), grad_f_rp(X) )
def test_gradient_approx_2(self): 'test on a function which returns multiple values' from solverLib import GradientApproximatorRandomPoints, GradientApproximatorForwardDifference, GradientApproximatorCentralDifference, rand grad_f_rp = GradientApproximatorRandomPoints(self.f1) grad_f_fd = GradientApproximatorForwardDifference(self.f1) grad_f_cd = GradientApproximatorCentralDifference(self.f1) for i in range(2): X = rand(2) * 10 - 5 #print(' X %s' % X) #print(' grad_f(X) analytical:') #prettyPrintArray(grad_f1(X), toStdOut, ' ','%1.6e') #print(' grad_f(X) randomPoints:') #prettyPrintArray(grad_f_rp(X), toStdOut, ' ','%1.6e') #print(' grad_f(X) forwardDiff:') #prettyPrintArray(grad_f_fd(X), toStdOut, ' ','%1.6e') #print(' grad_f(X) centralDiff:') #prettyPrintArray(grad_f_cd(X), toStdOut, ' ','%1.6e') #print(' error rp %e' % norm(grad_f1(X) - grad_f_rp(X))) self.assertAllClose(self.grad_f1(X), grad_f_rp(X))
def test( self ): from degreesOfFreedom import PlacementDegreeOfFreedom, LinearMotionDegreeOfFreedom, AxisRotationDegreeOfFreedom, pi, normalize from numpy.random import rand from variableManager import VariableManager #print('creating test FreeCAD document, constraining a single Cube') import FreeCAD, Part FreeCAD.newDocument("testDoc") #FreeCAD.setActiveDocument("box") #FreeCAD.ActiveDocument = FreeCAD.getDocument("box") objName = "box" box = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", objName) box.Shape = Part.makeBox(2,3,2) #FreeCAD.ActiveDocument.recompute() box.Placement.Base.x = rand() box.Placement.Base.y = rand() + 1 box.Placement.Base.z = rand() + 2 #print(box.Placement) class FakeSystem: def __init__(self, variableManager): self.variableManager = variableManager vM = VariableManager(FreeCAD.ActiveDocument) #print(vM.X) constaintSystem = FakeSystem(vM) #print('\nTesting PlacementDegreeOfFreedom') for object_dof in range(6): d = PlacementDegreeOfFreedom( constaintSystem, objName, object_dof ) #print(d) for i in range(6): value = pi*( rand() - 0.5 ) d.setValue(value) assert d.getValue() == value #print('\nTesting LinearMotionDegreeOfFreedom') tol = 10**-14 for i in range(3): d = LinearMotionDegreeOfFreedom( constaintSystem, objName ) d.setDirection( normalize(rand(3) - 0.5) ) #print(d) for i in range(12): value = 12*( rand() - 0.5 ) d.setValue(value) returnedValue = d.getValue() if abs(returnedValue - value) > tol : raise ValueError("d.getValue() - value != %1.0e, [diff %e]" % (tol, returnedValue - value)) #print('\nTesting AxisRotationDegreeOfFreedom') tol = 10**-12 for i in range(3): d = AxisRotationDegreeOfFreedom( constaintSystem, objName ) axis_r = normalize(rand(3) - 0.5) #axis in parts co-ordinate system (i.e. relative to part) axis = normalize(rand(3) - 0.5) # desired axis in global co-ordinate system d.setAxis( axis, axis_r ) d.setValue(0) #update azi,ela,theta to statify aligment of axis vector #print(d) for i in range(6): value = 2*pi*( rand() - 0.5 ) d.setValue(value) returnedValue = d.getValue() #print(' d.getValue() %f value %f, diff %e' % (returnedValue, value, returnedValue - value)) if abs(returnedValue - value) > tol : raise ValueError("d.getValue() - value != %1.0e, [diff %e]" % (tol, returnedValue - value))
def test_solve_via_Newtons_method( self ): from solverLib import solve_via_Newtons_method, rand maxStep = [0.5, 0.5] xMin = solve_via_Newtons_method( self.f1, rand(2)+3, maxStep, x_tol=0, debugPrintLevel=0 ) self.assertAllClose( xMin, [2, -1 ] )
def test(self): from degreesOfFreedom import PlacementDegreeOfFreedom, LinearMotionDegreeOfFreedom, AxisRotationDegreeOfFreedom, pi, normalize from numpy.random import rand from variableManager import VariableManager #print('creating test FreeCAD document, constraining a single Cube') import FreeCAD, Part FreeCAD.newDocument("testDoc") #FreeCAD.setActiveDocument("box") #FreeCAD.ActiveDocument = FreeCAD.getDocument("box") objName = "box" box = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", objName) box.Shape = Part.makeBox(2, 3, 2) #FreeCAD.ActiveDocument.recompute() box.Placement.Base.x = rand() box.Placement.Base.y = rand() + 1 box.Placement.Base.z = rand() + 2 #print(box.Placement) class FakeSystem: def __init__(self, variableManager): self.variableManager = variableManager vM = VariableManager(FreeCAD.ActiveDocument) #print(vM.X) constaintSystem = FakeSystem(vM) #print('\nTesting PlacementDegreeOfFreedom') for object_dof in range(6): d = PlacementDegreeOfFreedom(constaintSystem, objName, object_dof) #print(d) for i in range(6): value = pi * (rand() - 0.5) d.setValue(value) assert d.getValue() == value #print('\nTesting LinearMotionDegreeOfFreedom') tol = 10**-14 for i in range(3): d = LinearMotionDegreeOfFreedom(constaintSystem, objName) d.setDirection(normalize(rand(3) - 0.5)) #print(d) for i in range(12): value = 12 * (rand() - 0.5) d.setValue(value) returnedValue = d.getValue() if abs(returnedValue - value) > tol: raise ValueError( "d.getValue() - value != %1.0e, [diff %e]" % (tol, returnedValue - value)) #print('\nTesting AxisRotationDegreeOfFreedom') tol = 10**-12 for i in range(3): d = AxisRotationDegreeOfFreedom(constaintSystem, objName) axis_r = normalize( rand(3) - 0.5) #axis in parts co-ordinate system (i.e. relative to part) axis = normalize(rand(3) - 0.5) # desired axis in global co-ordinate system d.setAxis(axis, axis_r) d.setValue( 0) #update azi,ela,theta to statify aligment of axis vector #print(d) for i in range(6): value = 2 * pi * (rand() - 0.5) d.setValue(value) returnedValue = d.getValue() #print(' d.getValue() %f value %f, diff %e' % (returnedValue, value, returnedValue - value)) if abs(returnedValue - value) > tol: raise ValueError( "d.getValue() - value != %1.0e, [diff %e]" % (tol, returnedValue - value))