Doc-Strings und Type-Hinting

This commit is contained in:
2026-01-13 11:09:12 +01:00
parent 8507ca1afa
commit efd1b8c5fb
9 changed files with 235 additions and 135 deletions

View File

@@ -14,7 +14,7 @@ P_next: NDArray = None
P_end: NDArray = None
stepLen: float = None
def Bogenlaenge(P1, P2):
def Bogenlaenge(P1: NDArray, P2: NDArray) -> float:
"""
Berechnung der mittleren Bogenlänge zwischen zwei kartesischen Punkten
:param P1: kartesische Koordinate Punkt 1
@@ -23,9 +23,9 @@ def Bogenlaenge(P1, P2):
"""
R1 = np.linalg.norm(P1)
R2 = np.linalg.norm(P2)
R = 0.5*(R1 + R2)
R = 0.5 * (R1 + R2)
theta = arccos(P1 @ P2 / (R1 * R2))
s = R * theta
s = float(R * theta)
return s
@@ -49,7 +49,7 @@ def gha2_ES(ell: EllipsoidTriaxial, P0: NDArray, Pk: NDArray, stepLenTarget: flo
ell_ES = ell
P_start = P0
P_end = Pk
if stepLenTarget == None:
if stepLenTarget is None:
R0 = (ell.ax + ell.ay + ell.b) / 3
stepLenTarget = R0 * 1 / 600
stepLen = stepLenTarget
@@ -99,7 +99,7 @@ def gha2_ES(ell: EllipsoidTriaxial, P0: NDArray, Pk: NDArray, stepLenTarget: flo
# % v0 = atan2(q(2), q(1));
# % xmean_init = [u0;
# v0];
xmean_init = ell.cartonell(P_prev + stepLen * (P_end - P_prev) / np.linalg.norm(P_end - P_prev))
xmean_init = ell.point_onto_ellipsoid(P_prev + stepLen * (P_end - P_prev) / np.linalg.norm(P_end - P_prev))
# [~, ~, aux] = geoLength(xmean_init);
# print('Startguess: d_step=%.3f (soll %.3f), d_to_target=%.3f\n', aux(1), stepLen, aux(2));
@@ -107,7 +107,7 @@ def gha2_ES(ell: EllipsoidTriaxial, P0: NDArray, Pk: NDArray, stepLenTarget: flo
print(f'[Punkt {i}] Optimiere nächsten Punkt: Restdistanz = {round(d_remain, 3)} m')
xmean_init = np.array(ell_ES.cart2para(xmean_init))
u, v = escma(geoLength,2, xmean_init, sigmaStep, -np.inf, stopeval)
u, v = escma(geoLength, N=2, xmean=xmean_init, sigma=sigmaStep, stopfitness=-np.inf, stopeval=stopeval)
P_next = ell.para2cart(u, v)
@@ -128,26 +128,30 @@ def gha2_ES(ell: EllipsoidTriaxial, P0: NDArray, Pk: NDArray, stepLenTarget: flo
totalLen += d_step
P_prev = P_next
print('Maximale Schrittanzahl erreicht.')
P_all.append(P_end)
totalLen += Bogenlaenge(P_prev, P_end)
p0i = ell.cartonell(P0 + 10 * (P_all[1] - P0) / np.linalg.norm(P_all[1] - P0))
p0i = ell.point_onto_ellipsoid(P0 + 10 * (P_all[1] - P0) / np.linalg.norm(P_all[1] - P0))
sigma0 = (p0i - P0) / np.linalg.norm(p0i - P0)
alpha0 = sigma2alpha(ell_ES, sigma0, P0)
p1i = ell.cartonell(Pk - 10 * (Pk - P_all[-2]) / np.linalg.norm(Pk - P_all[-2]))
p1i = ell.point_onto_ellipsoid(Pk - 10 * (Pk - P_all[-2]) / np.linalg.norm(Pk - P_all[-2]))
sigma1 = (Pk - p1i) / np.linalg.norm(Pk - p1i)
alpha1 = sigma2alpha(ell_ES, sigma1, Pk)
if all_points:
return alpha0, alpha1, totalLen, np.array(P_all)
else:
return alpha0, alpha1, totalLen
return alpha0, alpha1, totalLen
def geoLength(P_candidate):
def geoLength(P_candidate: Tuple) -> float:
"""
Berechung der Fitness eines Kandidaten anhand der Strecken
:param P_candidate: Kandidat in parametrischen Koordinaten
:return: Fitness-Wert
"""
# P_candidate = [u;v] des naechsten Punktes.
# Ziel: Distanz zum Ziel minimieren, aber Schrittlaenge ~ stepLenTarget erzwingen.
u, v = P_candidate
@@ -165,7 +169,7 @@ def geoLength(P_candidate):
pen_step = ((d_step - stepLen) / stepLen)**2
# falls Punkt "weg" vom Ziel geht, extra bestrafen
pen_away = max(0, (d_to_target - d_prev_to_target) / stepLen)**2
pen_away = max(0.0, (d_to_target - d_prev_to_target) / stepLen)**2
# Gewichtungen
alpha = 1e2
@@ -175,13 +179,20 @@ def geoLength(P_candidate):
f = d_to_target * (1 + alpha * pen_step + gamma * pen_away)
# Für Debug / Extraktion
aux = [d_step, d_to_target]
# aux = [d_step, d_to_target]
return f # , P_candidate, aux
def show_points(points: NDArray, pointsNum:NDArray, p0: NDArray, p1: NDArray):
def show_points(points: NDArray, pointsES: NDArray, p0: NDArray, p1: NDArray):
"""
Anzeigen der Punkte
:param points: wahre Punkte der Linie
:param pointsES: Punkte der Linie aus ES
:param p0: wahrer Startpunkt
:param p1: wahrer Endpunkt
"""
fig = go.Figure()
fig.add_scatter3d(x=pointsNum[:, 0], y=pointsNum[:, 1], z=pointsNum[:, 2],
fig.add_scatter3d(x=pointsES[:, 0], y=pointsES[:, 1], z=pointsES[:, 2],
mode='lines', line=dict(color="green", width=3), name="Numerisch")
fig.add_scatter3d(x=points[:, 0], y=points[:, 1], z=points[:, 2],
mode='lines', line=dict(color="red", width=3), name="ES")