def quadraticMinSearcherTest( repeats , f ): eps = 0.00001 fails = 0 while repeats > 0: print( repeats ) repeats -= 1 P = rP.getRandomPolygon( 5 ) cD = cV.getConeVol( P ) result = quadraticMinSearcher( cD , [ f , eps ] )[0] if M.dist( cV.gamma( cD , result) , P[0]) > 0.1: if cV.sigma( result , cD ) < eps: print( 'not unique solution' ) print( P ) print(result) print( cV.gamma( cD , result ) ) fails += 1 break else: print('quadratic searcher failed by by') print( cV.sigma( result , cD ) ) print(P) print(result) print(cV.gamma(cD, result)) fails += 1 return fails
def vizLowValue( cD , n , first_bound, second_bound , third_bound): grid = pS.getGrid(n) x_1 = [] y_1 = [] x_2 = [] y_2 = [] x_3 = [] y_3 = [] b = 0 d = 0 for point in grid: while (True): try: value = cV.phi(point, cD) if value <= first_bound: x_1.append(point[0]) y_1.append(point[1]) else: if value <= second_bound: x_2.append(point[0]) y_2.append(point[1]) else: if value <= third_bound: x_3.append(point[0]) y_3.append(point[1]) if point[0] > b: b = point[0] if point[1] > d: d = point[1] break except ZeroDivisionError: # print('zero division at',pair) break mp.plot(x_1 , y_1 , 'go') mp.plot(x_2 , y_2 , 'yo') mp.plot(x_3, y_3, 'ro') mp.axis( [ 0, b, 0, d ] ) # print(vertices) mp.show() if len(x_1) > 0 : params = [ x_1[0] , y_1[0] ] vertex = cV.gamma(cD, params) print( ' Point near to result , gamma params , phi-value , gradient ') print(vertex) print( params) print( cV.phi( params , cD ) ) print( cV.gradPhi( params , cD) ) if len(x_1) > 1 : lastIndex = len( x_1 ) - 1 params = [x_1[lastIndex],y_1[lastIndex]] print(' ANOTHER one is ') vertex = cV.gamma(cD, params) print(vertex) print(params) print(cV.phi(params, cD)) print(cV.gradPhi(params, cD))
def preSolverPlot( cD , params ): x = cV.domain( 0.22 , 0.223 , 0.0001 ) v = cV.gamma( cD , params ) for x_value in x: stretchedParams = M.scaleVek( x_value , v ) y_value = preSolverPhi( stretchedParams , cD ) mp.plot( x_value , y_value , 'ro' ) mp.show()
def preSolverPhi( params , cD ): v = cV.gamma( cD , params ) t = M.norm(v) n = len( cD ) if t >= 1 : return ( t ** ( 2 * n ) * cV.phi( params , cD ) ) else: return cV.phi( params , cD )
def coneVolumeDescend(cD, start): result = start previous = [-1, -1] n = 0 print('gehe in while schleife ') while (cV.phi(result, cD) > eps_descend): if M.dist(previous, result) > 0.1: previous[0] = result[0] previous[1] = result[1] d = M.scaleVek(-1, cV.gradPhiApprox(result, cD, 0.0001)) # d = M.scaleVek( 1 / M.norm(d) , d ) alpha = sS.stepSize_posGrad(cD, result, d, n) result = M.addVek(result, M.scaleVek(alpha, d)) n = n + 1 else: print('versuche es mit BFGS und BFGSApprox, Start bei:') print(cV.gamma(cD, result)) result_2 = coneVolumeDescendBFGS(cD, result, previous, crease_test, efficiency_test, beta_1test, beta_2test) print('BFGS ist fertig mit:') print(cV.gamma(cD, result_2)) result_1 = coneVolumeDescendBFGSApprox(cD, result, previous, crease_test, efficiency_test, beta_1test, beta_2test) print('BFGS Approx ist fertig!') if cV.phi(result_1, cD) < cV.phi(result_2, cD): return result_1 else: return result_2 return result
def compareResults(P, alternativeParams): cD = cV.getConeVol(P) alternatePoint = cV.gamma(cD, alternativeParams) P_alternative = polygonReconstruct(cD, alternatePoint) cD_alternative = cV.getConeVol(P_alternative) pT.plotPoly(P, 'b') pT.plotPoly(P_alternative, 'r') print(len(P_alternative)) print('difference in coneVolume matrices') print(M.distMatrix(M.Matrix(cD), M.Matrix(cD_alternative))) print(' sigma value of alternative params') print(cV.sigma(alternativeParams, cD))
eps_test) sigma_result = cV.sigma(result, cD_test) if (sigma_result < eps_test): noFail += 1 else: if (sigma_result < eps_test * 100): fail_soft += 1 else: if (sigma_result < eps_test * 100 * 100): fail_medium += 1 else: fail_hard += 1 if M.dist(cV.gamma(cD_test, result), P_test[0]) > 0.01: if sigma_result < eps_test: print( ' not unique!!! polygon, result, gamma result , phi value, sigma value:' ) print(P_test) print(result) print(cV.gamma(cD_test, result)) print(cV.phi(result, cD_test)) print(cV.sigma(result, cD_test)) else: print( ' FAIL!! polygon, result, gamma result , phi value, sigma value:' ) print(P_test)
def lineSolution( cD , params ): v = cV.gamma( cD , params ) stepsize = 0.5
import coneVol2 as cV import matrix as M import machEps as mE import vizualization as v polygon_test2 = [[2.673368179682499, 3.09152986544487], [1.2086453601351808, 4.28111986768648], [-1.1761317014903958, -0.022433820601322707], [-3.4952312190856785, -4.881491593765966], [0.789349380758395, -2.4687243187640626]] cD = cV.getConeVol(polygon_test2) params_now = [4.7, 1.6152821997297826] print(cV.gamma(cD, params_now)) print(cV.phi(params_now, cD)) d = [ -0.2083940408151545, -0.9780449497608644 ] # should come from a BFGS algorithm to test the efficiency of BFGS directions grad = M.scaleVek(1.0 / M.norm(cV.gradPhi(params_now, cD)), cV.gradPhi(params_now, cD)) grad_approx = cV.gradPhiApprox(params_now, cD, 0.00001) stepSize = 1 params_next = M.addVek(params_now, M.scaleVek(stepSize, d)) v.visiualizeLowValueOnGrid(0.001, 0.00001, cD, params_now, 0.02405, 0.024059, 0.02406) v.vizNiveauGradOnGrid(0.001, 0.00001, cD, params_now, d, 0.000001) v.vizNiveauGradOnGrid(0.001, 0.00001, cD, params_now, grad, 0.000001) v.vizNiveauGradOnGrid(0.001, 0.00001, cD, params_now, grad_approx, 0.000001) n = 0 diff = (1 * cV.phi(params_now, cD)) - (1 * cV.phi(params_next, cD)) d = [-1, 2]