master > master: src py - minor änderungen

This commit is contained in:
RD
2021-11-01 19:58:03 +01:00
parent 15c2c37213
commit 2c23f4c728
11 changed files with 132 additions and 84 deletions

View File

@@ -8,6 +8,7 @@
import functools;
from src.core.log import *;
from src.core.metrics import *;
from src.setup.display import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -39,12 +40,12 @@ def algorithmInfos(
name: str,
checks: bool = True,
metrics: bool = None,
outputnames: Union[str, Tuple[str]] = 'result',
outputnames: List[str] = ['result'],
preChecks: Any = None,
postChecks: Any = None
):
'''
Decorator für Algorthmen, der Folgendes macht:
Decorator für Algorithmen, der Folgendes macht:
- Zeigt vorm Start Console-Messages mit Namen des Algorithmus + Inputs.
- Zeit am Ende optional Metriken (bspw. Zeitkosten).
@@ -65,7 +66,7 @@ def algorithmInfos(
if state:
# Initialisierung
DisplayStartOfAlgorithm(name, **inputs);
RestartCounter();
RestartMetrics();
# Prechecks
if checks and callable(preChecks):
preChecks(**inputs);
@@ -78,8 +79,7 @@ def algorithmInfos(
if state:
# benenne Outputs:
outputs_ = outputs if isinstance(outputs, tuple) else tuple([outputs]);
outputnames_ = outputnames if isinstance(outputnames, tuple) else tuple([outputnames]);
outputsNamed = { outputnames_[k]: value for k, value in enumerate(outputs_) };
outputsNamed = { outputnames[k]: value for k, value in enumerate(outputs_) };
# Letzte Messages
if metrics:
DisplayMetrics();

View File

@@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
# ALGORITHM binary search
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Binärsuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='Binärsuchalgorithmus', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def BinarySearch(L: List[int], x: int) -> int:
'''
Inputs: L = Liste von Zahlen, x = Zahl.
@@ -49,7 +49,7 @@ def BinarySearch(L: List[int], x: int) -> int:
if len(L) == 0:
logDebug('x nicht in L');
return -1;
AddToCounter();
AddTimeCost();
m = math.floor(len(L)/2);
if L[m] == x:
logDebug('x in Position m gefunden');

View File

@@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
# ALGORITHM interpolation
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Interpolationssuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='Interpolationssuchalgorithmus', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def InterpolationSearch(L: List[int], x: int, u: int, v: int) -> int:
'''
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
@@ -66,7 +66,7 @@ def getSuchposition(L: List[int], x: int, u: int, v: int) -> int:
Inputs: L = Liste von Zahlen, x = Zahl, [u, v] = Suchinterval.
Outputs: Interpolierte Position, um Suchinterval ausgeglichen aufzuteilen.
'''
AddToCounter();
AddTimeCost();
r = (x - L[u])/(L[v]-L[u]);
p = math.floor(u + r*(v-u))
return p;

View File

@@ -33,10 +33,10 @@ def postChecks(L: List[int], i: int, value: int, **_):
return;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ALGORITHM jump search
# ALGORITHM find ith smallest element
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element)', outputnames='value', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element)', outputnames=['value'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def FindIthSmallest(L: List[int], i: int) -> int:
'''
Inputs: L = Liste von Zahlen, i = Ordinalzahl
@@ -51,7 +51,7 @@ def FindIthSmallest(L: List[int], i: int) -> int:
'''
index = 0;
minValue = L[0];
AddToCounter(len(L));
AddTimeCost(len(L));
for i_ in range(1, len(L)):
if L[i_] < minValue:
index = i_;
@@ -65,10 +65,10 @@ def FindIthSmallest(L: List[int], i: int) -> int:
return FindIthSmallest(L=L[:index] + L[(index+1):], i=i);
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ALGORITHM jump search (D & C)
# ALGORITHM find ith smallest element (D & C)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element, D & C)', outputnames='value', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='Auswahlproblem (i. kleinstes Element, D & C)', outputnames=['value'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def FindIthSmallestDC(L: List[int], i: int) -> int:
'''
Inputs: L = Liste von Zahlen, i = Ordinalzahl
@@ -81,7 +81,7 @@ def FindIthSmallestDC(L: List[int], i: int) -> int:
Outputs: Wert des i. kleinste Element in L.
Beachte 1.kleinstes <==> Minimum.
'''
AddToCounter();
AddTimeCost();
p = L[len(L)-1]; # NOTE: Pivotelement kann beliebig gewählt werden
Ll = [ x for x in L if x < p ];
Lr = [ x for x in L if x > p ];

View File

@@ -39,7 +39,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
# ALGORITHM jump search
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Sprungsuche', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='Sprungsuche', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
'''
Inputs: L = Liste von Zahlen, x = Zahl, m = lineare Sprunggröße.
@@ -52,7 +52,7 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
'''
i = 0;
while i*m < len(L):
AddToCounter();
AddTimeCost();
offset = i*m;
block = L[offset:][:m];
elementAfterBlock = block[-1] + 1;
@@ -70,7 +70,7 @@ def JumpSearchLinear(L: List[int], x: int, m: int) -> int:
# ALGORITHM jump search - exponentiell
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Sprungsuche (exponentiell)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='Sprungsuche (exponentiell)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def JumpSearchExponentiell(L: List[int], x: int) -> int:
'''
Inputs: L = Liste von Zahlen, x = Zahl.
@@ -84,7 +84,7 @@ def JumpSearchExponentiell(L: List[int], x: int) -> int:
i0 = 0;
i1 = 1;
while i0 < len(L):
AddToCounter();
AddTimeCost();
block = L[i0:i1];
elementAfterBlock = block[-1] + 1;
if x < elementAfterBlock:

View File

@@ -21,23 +21,25 @@ from src.algorithms.methods import *;
# CHECKS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def preChecks(L: List[bool], **_):
assert sum(L) > 0, 'Mindestens ein Getränk muss vergiftet sein!';
assert sum(L) == 1, 'Höchstens ein Getränk darf vergiftet sein!';
def preChecks(L: List[int], **_):
s = sum(L);
assert s > 0, 'Mindestens ein Getränk muss vergiftet sein!';
assert s <= 1, 'Höchstens ein Getränk darf vergiftet sein!';
return;
def postChecks(L: List[bool], index: int, **_):
assert L[index] == True, 'Der Algorithmus hat das vergiftete Getränk nicht erfolgreich bestimmt.';
def postChecks(L: List[int], index: int, **_):
assert index >= 0, 'Der Algorithmus hat kein vergiftetes Getränk gefunden, obwohl per Annahme eines existiert.';
assert L[index] > 0, 'Der Algorithmus hat das vergiftete Getränk nicht erfolgreich bestimmt.';
return;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ALGORITHM find poison
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Giftsuche (O(n) Vorkoster)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def FindPoison(L: List[bool]) -> int:
@algorithmInfos(name='Giftsuche (O(n) Vorkoster)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def FindPoison(L: List[int]) -> int:
'''
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
Inputs: L = Liste von Getränken: durch 0-1 Werte wird dargestellt, ob ein Getränk vergiftet ist.
Annahme: Genau ein Getränk sei vergiftet.
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
@@ -47,7 +49,7 @@ def FindPoison(L: List[bool]) -> int:
n = len(L);
testers = [];
for i in range(n):
AddToCounter();
AddSpaceCost();
logDebug('Füge Vorkoster hinzu, der nur Getränk {i} testet.'.format(i=i))
testers.append([i]);
logDebug('Warte auf Effekte');
@@ -62,10 +64,10 @@ def FindPoison(L: List[bool]) -> int:
# ALGORITHM find poison fast
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Giftsuche (O(log(n)) Vorkoster)', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def FindPoisonFast(L: List[bool]) -> List[int]:
@algorithmInfos(name='Giftsuche (O(log(n)) Vorkoster)', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def FindPoisonFast(L: List[int]) -> int:
'''
Inputs: L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
Inputs: L = Liste von Getränken: durch 0-1 Werte wird dargestellt, ob ein Getränk vergiftet ist.
Annahme: Genau ein Getränk sei vergiftet.
Outputs: Der Index des vergifteten Getränks, falls es eines gibt, ansonsten -1.
@@ -81,7 +83,7 @@ def FindPoisonFast(L: List[bool]) -> List[int]:
tester1 = [ k for k in range(n) if not (k in tester0) ];
# NOTE: tester1 ist virtuell: aus den Effekten auf tester0 und den Annahmen lassen sich die Effekte auf tester0 erschließen.
# Darum zählen wir nicht 2 sondern 1 Vorkoster.
AddToCounter(1);
AddSpaceCost(1);
logDebug('Füge Vorkoster hinzu, der alle Getränke k testet mit {i}. Bit = 0.'.format(i=i))
testers.append(tester0);
testers.append(tester1);
@@ -97,10 +99,10 @@ def FindPoisonFast(L: List[bool]) -> List[int]:
# AUXILIARY METHOD wait for effects, evaluate effects
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def waitForEffects(L: List[bool], testers: List[List[int]]) -> List[int]:
def waitForEffects(L: List[int], testers: List[List[int]]) -> List[int]:
'''
Inputs:
- L = Liste von Getränken: durch boolesche Werte wird dargestellt, ob ein Getränk vergiftet ist.
- L = Liste von Getränken: durch 0-1 Werte wird dargestellt, ob ein Getränk vergiftet ist.
- testers = Liste von Vorkostern. Jeder Vorkoster kostet eine 'Teilliste' der Getränke.
Outputs: effects = eine Liste, die jedem Vorkoster zuordnet, wie viele vergiftete Getränke er konsumiert hat.

View File

@@ -37,7 +37,7 @@ def postChecks(L: List[int], x: int, index: int, **_):
# ALGORITHM sequential search
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='Sequenziellsuchalgorithmus', outputnames='index', checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='Sequenziellsuchalgorithmus', outputnames=['index'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def SequentialSearch(L: List[int], x: int) -> int:
'''
Inputs: L = Liste von Zahlen, x = Zahl.
@@ -45,7 +45,7 @@ def SequentialSearch(L: List[int], x: int) -> int:
'''
n = len(L);
for i in range(n):
AddToCounter();
AddTimeCost();
if L[i] == x:
logDebug('Element in Position {} gefunden.'.format(i));
return i;

View File

@@ -33,7 +33,7 @@ def postChecks(L: List[int], **_):
# ALGORITHM max sub sum
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme)', outputnames=('maxSum', 'index_from', 'index_to'), checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme)', outputnames=['maxSum', 'index_from', 'index_to'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def MaxSubSum(L: List[float]) -> Tuple[float, int, int]:
'''
Inputs: L = Liste von Zahlen
@@ -58,7 +58,7 @@ def MaxSubSum(L: List[float]) -> Tuple[float, int, int]:
# ALGORITHM max sub sum (D & C)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme mit D & C)', outputnames=('maxSum', 'index_from', 'index_to'), checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
@algorithmInfos(name='MaxSubSum (Maximale Teilsumme mit D & C)', outputnames=['maxSum', 'index_from', 'index_to'], checks=True, metrics=True, preChecks=preChecks, postChecks=postChecks)
def MaxSubSumDC(L: List[float]) -> Tuple[float, int, int]:
'''
Inputs: L = Liste von Zahlen
@@ -127,7 +127,7 @@ def lRandSum(L: List[float]) -> Tuple[float, int, int]:
'''
n = len(L);
## berechne kumulative Summen (vorwärts)
AddToCounter(n);
AddTimeCost(n);
total = L[0];
maxSum = total;
u = 0;
@@ -148,7 +148,7 @@ def rRandSum(L: List[float]) -> Tuple[float, int, int]:
'''
n = len(L);
## berechne kumulative Summen (rückwärts)
AddToCounter(n);
AddTimeCost(n);
total = L[n-1];
maxSum = total;
u = n-1;