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))
def getSumOuterNormal(polygon): coneVol = cV.getConeVol(polygon) normSum = 0 for v in coneVol: n = [v[0], v[1]] normSum = normSum + M.norm(n) return normSum
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 quadraticMinSearcherTestWithoutGamma( repeats , f ): eps = 0.01 notUnique = 0 singDiffMatrix = 0 fails = 0 while repeats > 0: repeats -= 1 P = rP.getRandomPolygon( 5 ) cD = cV.getConeVol( P ) result = quadraticMinSearcher( cD , [ f , eps ] )[0] if M.dist( result , P[0]) > 0.1: value = M.dist(cV.getConeVolIterator( cD , result ) , result) if value < eps: print( 'not unique solution' ) print( P ) print(result) notUnique += 1 break else: if f( result , cD ) < 0.01: print(" singular diff matrix ") print(P) print(result) print( f( result , cD ) ) singDiffMatrix += 1 else: print('quadratic searcher failed by') print(P) print(result) fails += 1 return [notUnique , singDiffMatrix , fails]
def coneVolTest(polygon, coneVolume, eps): diff = 0 coneData = cV.getConeVol(polygon) for i in range(len(polygon)): diff += M.dist(coneData[i - 1], coneVolume[i - 1])**2 if diff >= eps: print('coneVolTest failed with polygon') print(polygon)
def makeUnitVolume(orderedPolytop): coneVolume = cV.getConeVol(orderedPolytop) volume = 0 for data in coneVolume: volume = volume + data[2] for i in range(len(orderedPolytop)): orderedPolytop[i - 1] = M.scaleVek(1.0 / math.sqrt(volume), orderedPolytop[i - 1])
def normalsTest(repeats, verticesNumber, test_eps): for i in range(repeats): P = rP.getRandomPolygon(verticesNumber) coneVol = cV.getConeVol(P) diff = math.fabs(getSumOuterNormal(P) - verticesNumber) if diff > test_eps: #print( P ) #print( diff ) normalNormsPrint(coneVol) print(' normalsTest failed at') print(len(coneVol)) print(len(P)) pT.plotPoly(P, 'r') break
def logMinTest(orderedCartPolygon_1, orderedCartPolygon_2, bringInPosition, eps_prod): K = orderedCartPolygon_1 L = orderedCartPolygon_2 bringInPosition(K) bringInPosition(L) rP.makeUnitVolume(K) rP.makeUnitVolume(L) # not sure if makeUnitVolume destroys the centered property... it is clear that it needs it for the normalization of volume bringInPosition(K) bringInPosition(L) coneVol = cV.getConeVol(K) #print( coneVol ) prod = 1 for i in range(len(K)): # Reihenfolge der Normalenvektoren sollte jetzt egal sein u = [coneVol[i - 1][0], coneVol[i - 1][1]] h_1 = rP.supportFunctionCartesianCentered(L, u) h_2 = rP.supportFunctionCartesianCentered(K, u) quotient = h_1 / h_2 if quotient == 0: print('quotient von logMin war gleich 0...') print(u) print(L[i - 1]) print(K[i - 1]) #print( quotient) #print( K[ i - 1] ) #print( u ) if quotient >= 0: prod = prod * math.pow(quotient, coneVol[i - 1][2]) else: print(' quotient von logMin war negativ') print(u) print(coneVol[i - 1][2]) pT.plotPoly(K, 'r') pT.plotPoly(L, 'g') if prod + eps_prod >= 1: return True else: print(1 - prod) return False
def gradPolyFunctionalTest_array(polygon_list, point_list, eps, h): index = 0 for P in polygon_list: cD_test = cV.getConeVol(P) point_test = point_list[index] # could be improved by making delta smaller if check is false ... orby setting point_test in the near of an vertex... check = hMethodPolyFunctionalCheck(cD_test, point_test, eps, h) if not check: print( ' gradPolyFunctionalTest failed with polytop , point_test , value : ' ) print(P) print(point_test) print(cV.polyFunctional(cD_test, point_test)) index += 1
def polyFunctionalTest(repeats, edgeNumber, eps): turns = 0 while turns < repeats: P = rP.getRandomPolygon(edgeNumber) cD = cV.getConeVol(P) vertex = P[0] value_vertex = cV.polyFunctional(cD, vertex) grad_vertex = cV.gradPolyFunctional(cD, vertex) # getConeVol, polyFunctional oder gradPolyFunctional könnte falsch sein... if (value_vertex >= eps): print(' Fehler bei polyFunctionalTest , value zu hoch') print(P) print(value_vertex) if (M.norm(grad_vertex) >= eps): print(' Fehler bei polyFunctionalTest , grad zu hoch') print(P) print(grad_vertex) turns += 1
def testerDetOfDiff(repeats, range_test, eps): complete_fail = 0 approx_fail = 0 analytic_fail = 0 for i in range(repeats): P = rP.getRandomPolygon(5) #random.random() * 5 + 3 ) cD = cV.getConeVol(P) point = [ random.random() * 2 * range_test - range_test, random.random() * 2 * range_test - range_test ] if not checkDetOfDiff(point, cD, eps): if not checkDetOfDiffApprox(point, cD, eps): complete_fail += 1 else: analytic_fail += 1 else: if not checkDetOfDiffApprox(point, cD, eps): approx_fail += 1 print([repeats, complete_fail, analytic_fail, approx_fail])
import randomPolygon3Centered as rP import coneVol2 as cV import math import random import matrix as M repeats = 0 noFail = 0 fail_soft = 0 fail_medium = 0 fail_hard = 0 while repeats < 50: eps_test = 0.00001 P_test = rP.getRandomPolygon(math.floor(10 * random.random() + 3)) cD_test = cV.getConeVol(P_test) result = pR.polygonReconstructCentered(cD_test, 0.0001, 0.5, 0.9, 0.5, 0.5, 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:
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]
mp.show() # besser kontrollierbaren Grid-Maker schreiben #polygon_ohShit = [[0.9440772944478957, -0.5997771631098889], [-16.804067260622325, -40.38884325863358], [-17.999708289662934, -10.829240656802343], [-32.34730498625317, 88.7483757416121]] #pT.plotPoly( polygon_ohShit , 'r') vertexNumber = 5 polygon_test = [[2.5867398322535426, 0.35370196134820064], [2.816765578870172, 0.6295822977163863], [2.876297688138016, 0.7085658788323118], [3.0156770817979806, 0.920133822103927], [3.2394931255109474, 1.5199632701112769], [3.264789304173254, 1.595191542976061], [3.724878042101699, 3.0170215939392877], [3.814877212031399, 3.562393608394033], [3.805267457792396, 3.625719686521581], [3.7323733429202934, 3.7628157979801506], [3.4945376684513922, 4.032118077767379], [3.4021986658415564, 3.962113846872688], [2.4505649979872577, 3.2406425887674666], [2.385287887899589, 3.1911533801671967], [1.9963758828748048, 2.896301904722165], [1.7526950672949497, 2.7115560857408734], [1.4785918040258232, 2.5037455673666718], [1.1702316222363573, 2.269963267234045], [0.5302116894870117, 1.7847341606554012], [0.44507910318623856, 1.720191157098469], [0.43071118097515304, 1.709298161893054], [0.42830602333192025, 1.7074746991618641], [0.4036475938508899, 1.6887799881124186], [0.3229526946722635, 1.6276014031503094], [0.24413354710762858, 1.5678449133941952], [0.04640950546993311, 1.4179410507701142], [-0.17957128234478759, 1.2466144245712263], [-0.32300368032403437, 1.1378716014708878], [-0.5880100510817052, 0.9369578516354184], [-0.6355138006568612, 0.9009430326182588], [-0.6665890121590617, 0.8773834563844612], [-0.7208415097330012, 0.8362520894116748], [-0.7300663954692099, 0.8292582699055915], [-0.7824545990073234, 0.7895403090423702], [-0.7900257292263145, 0.7838002780334713], [-0.8279311661190941, 0.7550623780209548], [-0.8431717474537053, 0.7435077738836858], [-0.8774692505965414, 0.7175052174523378], [-0.8946005706825306, 0.7045171547510899], [-0.9027183852317148, 0.6983626561604043], [-0.980975673677574, 0.6390321055035905], [-1.1437591255835025, 0.5156182712988058], [-1.1571655604007427, 0.5054542080680189], [-1.2121263435464593, 0.4637855506049331], [-1.2391048516264667, 0.44333170809264905], [-1.2947322885531918, 0.40115750588408106], [-1.4065446449744032, 0.31638605977647105], [-1.8161441053769063, 0.0058434199872439], [-1.823014096743991, 0.0006348551607779501], [-1.8288016930344846, -0.0037530791232921606], [-1.9145610626624578, -0.06877255557914909], [-1.9936022040773425, -0.12869933204554046], [-2.2038634659290217, -0.28811493361000384], [-2.814872197671972, -0.7514658864113118], [-3.5227364675839805, -1.2884736665985472], [-4.6384411646339885, -2.134891875319733], [-5.179392634218635, -2.5453042166976307], [-4.575135177671799, -2.5491832973702873], [-4.406993908926179, -2.5484823712208424], [-3.89424447437459, -2.542094311062124], [-3.6642999583764757, -2.534673161773632], [-3.0202764273616363, -2.5086776327746], [-2.0470511252220422, -2.4543626947998436], [-1.7612691106880378, -2.429745430121149], [-1.5404746093384005, -2.4103618645634293], [-1.5287149810424265, -2.4091034912847946], [-1.3894247703113551, -2.393782991805184], [-1.2994005389089274, -2.38363505034361], [-1.206691313666303, -2.3721780133196804], [-0.8711180767800243, -2.3042102161462186], [-0.7550131627905892, -2.267194799492451], [-0.6843971511040434, -2.221638072237676], [-0.5845851531938262, -2.1569017289347756], [-0.2994920181681502, -1.9672474240024926], [-0.2192620692964681, -1.9120937245860663], [-0.16420466742060374, -1.8742446690311845], [-0.08707743585254532, -1.821223712225415], [-0.08086383376133327, -1.8169519967919534], [-0.027151202685557208, -1.7797807755444641], [-0.007573798549373401, -1.766205938865122], [0.1395859706232025, -1.6640090617139216], [0.2652031839025275, -1.576470287551836], [0.3462086463897328, -1.5193205447610185], [0.3754295420740775, -1.4985757427793513], [0.4865241418674051, -1.4128001450344456], [0.8805607448622572, -1.0820321270396074], [1.1339464919009248, -0.8690378214383169], [1.2525537079152953, -0.7692638914303036], [1.3318867589631302, -0.7025274548171956], [1.3753089931526346, -0.6659995318706317], [1.4370282196278574, -0.6140795033168567], [1.549044208701108, -0.5198480589690921], [1.7401003076406498, -0.3591120554768841], [1.8107863165430262, -0.2996412267221694], [1.8593681193512923, -0.25875992631402345], [1.9162321726508709, -0.21090661037899877], [1.9204859833256185, -0.20732680009425675], [2.1090823744184988, -0.048606704424120004], [2.1211746417837225, -0.03842949148610582], [2.1369155998954, -0.02517399121459922]] polygon_test = [ [ 1 , 1 ] , [ -1 , 1 ] , [-1 , -1 ] , [ 1 , -1 ] ]#rP.getRandomPolygon(vertexNumber) #pT.plotPoly( polygon_test , 'r') #print( 'here comes the polygon') #print( polygon_test ) cD_test = cV.getConeVol( polygon_test )#[ [ 1 , 0 , 1 ] , [ 0 , 1 , 1 ] , [ -1 , 0 , 1 ] , [ 0 , -1 , 1 ] ] #vizLowValue( cD_test , 10 , 1 , 10 , 100) #vizLowGrad( cD_test , 10 , 2 , 1 , 1) a_plot = 1 * 5.0 b_plot = 10 * 5.0 c_plot = 100 * 5.0 size_plot = 100 midPoint_plot= [ size_plot / 2.0 , size_plot / 2.0 ] stepSize_plot = size_plot / 10.0 #vizConeDataParamFunction( size_plot , stepSize_plot , cV.phi , cD_test , midPoint_plot , a_plot , b_plot , c_plot ) #vizConeDataParamFunction( size_plot , stepSize_plot , cV.sigma , cD_test , midPoint_plot , a_plot , b_plot , c_plot ) #vizDiffDet( size_plot , stepSize_plot , cD_test , midPoint_plot, 0 , 0.0001 )
def getVolume(orderedPolygon): coneVolume = cV.getConeVol(orderedPolygon) volume = 0 for data in coneVolume: volume = volume + data[2] return volume