def coneVolumeDescendArmijo(cD, start, crease, efficiency, beta_1, beta_2): result = start n = 0 previous = [-1, -1] print('gehe in while schleife ') while (cV.phi(result, cD) > eps_descend): if M.dist(previous, result) > minimalStep_descendArmijo: previous[0] = result[0] previous[1] = result[1] d = M.scaleVek(-1, cV.gradPhi(result, cD)) #d = M.scaleVek( 1 / M.norm(d) , d ) alpha = sS.stepSizeArmijo(cD, result, d, crease, efficiency, beta_1, beta_2) result = M.addVek(result, M.scaleVek(alpha, d)) else: print('versuche es mit BFGS und BFGSApprox') result_1 = coneVolumeDescendBFGSApprox(cD, result, previous, crease, efficiency, beta_1, beta_2) result_2 = coneVolumeDescendBFGS(cD, result, previous, crease, efficiency, beta_1, beta_2) if cV.phi(result_1, cD) < cV.phi(result_2, cD): return result_1 else: return result_2 n = n + 1 return result
def powellFunction_1(cD, params, d, step): dividend = cV.phi(M.addScaleVek(params, step, d), cD) - cV.phi(params, cD) divisor = step * M.scal(cV.gradPhi(params, cD), d) if (step <= eps): return 1 return dividend / divisor
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 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 vizNiveauGradOnGrid( size , stepSize , cD , param , grad , eps): grid = [] value_param = cV.phi( param , cD ) n = math.floor(size * 1.0/stepSize) for i in range(n): for j in range(n): grid.append( [ i * stepSize - size / 2.0 + param[0], j *stepSize - size / 2.0 + param[1]]) print('grid ist fertig') x_1 = [] y_1 = [] for point in grid: while(True): try: value= cV.phi(point , cD ) if math.fabs(value - value_param) < eps: x_1.append(point[0]) y_1.append([point[1]]) break except ZeroDivisionError: break d= M.scaleVek( 1.0 / M.norm(grad) , grad ) x_d = [] y_d = [] stepBound = 0.71 * size step = - stepBound while( step <= stepBound ): point = M.addVek( param , M.scaleVek( step , d ) ) x_d.append( point[0] ) y_d.append( point[1] ) step = step + stepSize a_x = param[0] - size / 2.0 b_x = param[0] + size / 2.0 + 5 a_y = param[1] - size / 2.0 b_y = param[1] + size / 2.0 + 5 mp.plot( x_1 , y_1 , 'go') mp.plot( x_d , y_d , 'yo') mp.plot([param[0]], [param[1]], 'wo') mp.axes( [a_x , b_x , a_y , b_y]) #print('here comes minimal point, gamma , value, gradient:') #print( point_min ) #print( cV.gamma( cD , point_min )) #print( cV.phi( point_min , cD )) #print( cV.gradPhi( point_min , cD)) mp.show()
def stepSize_posGrad(cD, point, d, n): alpha = 0.5 nextPoint = M.addVek(point, M.scaleVek(alpha, d)) while (cV.phi(point, cD) + eps < cV.phi( nextPoint, cD)): #or pS.scalGrad( cD , nextPoint ) < 0 ): alpha = alpha * 0.5 nextPoint = M.addVek(point, M.scaleVek(alpha, d)) if n % 300 == 0: print(alpha) if alpha < eps == 1: return 0 return alpha
def coneVolumeDescendBFGSApprox(cD, params_new, params_prev, crease, efficiency, beta_1, beta_2): A_k = M.idMatrix(2) n = 0 while (cV.phi(params_new, cD) > eps_descend): while (True): try: A_k = BFGS.getBFGSApprox(cD, params_new, params_prev, A_k) break except ZeroDivisionError: print('es geht nicht besser mit BFGSApprox') return params_new break antiGrad = M.scaleVek(-1, cV.gradPhiApprox(params_new, cD, 0.0001)) d = A_k.lgsSolve(antiGrad) d = M.scaleVek(1.0 / M.norm(d), d) alpha = sS.stepSizeArmijo(cD, params_new, d, crease, efficiency, beta_1, beta_2) d = M.scaleVek(alpha, d) params_prev = [params_new[0], params_new[1]] params_new = M.addVek(params_new, d) if (M.dist(params_new, params_prev) < minimalStep_BFGSApprox): print(' distance is lower than minimalStep of BFGSApprox ') break n = n + 1 return params_new
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 getLowPointOnGrid( grid , cD , upperBound ): result = [] value_result = upperBound for point in grid: while (True): try: value = cV.phi( point , cD ) if value_result >= value: result = point value_result = value break except ZeroDivisionError: # print('zero division at',pair) break return result
def coneVolumeDescendBFGS(cD, params_new, params_prev, crease, efficiency, beta_1, beta_2): A_k = M.idMatrix(2) n = 0 while (cV.phi(params_new, cD) > eps_descend): # ICH VERÄNDERE HIER MEIN CREASE... crease = 0.000001 #cV.phi( params_new , cD) * 0.00000001 while (True): try: A_k = BFGS.getBFGS(cD, params_new, params_prev, A_k) break except ZeroDivisionError: print('es geht nicht besser') return params_new break antiGrad = M.scaleVek(-1, cV.gradPhi(params_new, cD)) d = A_k.lgsSolve(antiGrad) d = M.scaleVek(1.0 / M.norm(d), d) alpha = sS.stepSize_posGrad( cD, params_new, d, n) # crease , efficiency , beta_1 , beta_2 ) d = M.scaleVek(alpha, d) params_prev = [params_new[0], params_new[1]] params_new = M.addVek(params_new, d) if (M.dist(params_new, params_prev) < minimalStep_BFGS): print(' distance is lower than minimalStep of BFGS ') break n = n + 1 return params_new
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) print(result) print(cV.gamma(cD_test, result)) print(cV.phi(result, cD_test)) print(cV.sigma(result, cD_test)) repeats += 1 print(repeats) print([noFail, fail_soft, fail_medium, fail_hard])
def notEfficient(cD, point, d, stepSize, crease): v = M.addVek(point, M.scaleVek(stepSize, d)) upper_bound = cV.phi(point, cD) + crease * stepSize * M.scal( cV.gradPhi(point, cD), d) / M.norm(d) return cV.phi(v, cD) > upper_bound
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]
def visiualizeLowValueOnGrid( size , stepSize , cD , param , value_1 , value_2 , value_3 ): grid = [] n = math.floor(size * 1.0/stepSize) for i in range(n): for j in range(n): grid.append( [ i * stepSize - size / 2.0 + param[0], j *stepSize - size / 2.0 + param[1]]) print('grid ist fertig') min = math.inf point_min = param x_1 = [] y_1 = [] x_2 = [] y_2 = [] x_3 = [] y_3 = [] x_4 = [] y_4 = [] for point in grid: while(True): try: value= cV.phi(point , cD ) if value < min: point_min = point min = value if value < value_1: x_1.append(point[0]) y_1.append([point[1]]) else: if value < value_2: x_2.append(point[0]) y_2.append([point[1]]) else: if value < value_3: x_3.append(point[0]) y_3.append([point[1]]) else: x_4.append(point[0]) y_4.append(point[1]) break except ZeroDivisionError: break a_x = param[0] - size / 2.0 b_x = param[0] + size / 2.0 + 5 a_y = param[1] - size / 2.0 b_y = param[1] + size / 2.0 + 5 mp.plot( x_1 , y_1 , 'go') mp.plot( x_2 , y_2 , 'yo') mp.plot( x_3 , y_3 , 'ro') mp.plot( x_4 , y_4 , 'bo') mp.plot([param[0]], [param[1]], 'wo') mp.axes( [a_x , b_x , a_y , b_y]) #print('here comes minimal point, gamma , value, gradient:') #print( point_min ) #print( cV.gamma( cD , point_min )) #print( cV.phi( point_min , cD )) #print( cV.gradPhi( point_min , cD)) mp.show()
def phiNormScaled( params , cD): return cV.phi( params , cD ) * ( ( M.norm( params ) ) ** n + 1 )