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;

View File

@ -10,10 +10,6 @@ from src.local.misc import *;
from src.local.system import *;
from src.local.typing import *;
from datetime import timedelta;
from src.core.metrics import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GLOBAL VARIABLES
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -21,7 +17,6 @@ from src.core.metrics import *;
_logging_prefix: str = '';
_quietmode: bool = False;
_debugmode: bool = False;
_ctr: Counter = Counter();
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHOD get/set quiet mode, logging depth, timer
@ -43,24 +38,6 @@ def SetDebugMode(mode: bool):
_debugmode = mode;
return;
def RestartCounter():
global _ctr;
_ctr.reset();
return;
def AddToCounter(n: int = 1):
global _ctr;
_ctr.add(n);
return;
def NumberOfSteps() -> int:
return _ctr.numberOfStep;
def TimeElapsed() -> timedelta:
global _ctr;
_ctr.stop();
return _ctr.elapsedTime;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Logging
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -5,17 +5,24 @@
# IMPORTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from __future__ import annotations;
from datetime import datetime;
from datetime import timedelta;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GLOBAL VARIABLES
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_ctr_time = None;
_ctr_space = None;
_tmr = None;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# CLASS counter
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Counter(object):
_nr_steps: int;
_timeelapsed: timedelta;
_timecurrent: datetime;
def __init__(self):
self.reset();
@ -27,27 +34,87 @@ class Counter(object):
def numberOfStep(self) -> int:
return self._nr_steps;
@property
def elapsedTime(self) -> timedelta:
return self._timeelapsed;
def start(self):
self._timecurrent = datetime.now();
return self;
def stop(self):
t0 = self._timecurrent;
t1 = datetime.now();
self._timecurrent = t1;
self._timeelapsed += (t1 - t0);
return self;
def add(self, n: int = 1):
self._nr_steps += n;
return self;
def reset(self):
t = datetime.now();
self._timeelapsed = t - t;
self._nr_steps = 0;
self._timecurrent = t;
return self;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# CLASS timer
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Timer(object):
_time_elapsed: timedelta;
_time_current: datetime;
def __init__(self):
self.reset();
def __str__(self) -> str:
return str(self._time_elapsed);
@property
def elapsedTime(self) -> timedelta:
return self._time_elapsed;
def start(self):
self._time_current = datetime.now();
return self;
def stop(self):
t0 = self._time_current;
t1 = datetime.now();
delta = t1 - t0;
self._time_current = t1;
self._time_elapsed += delta;
return self;
def reset(self):
t = datetime.now();
delta = t - t;
self._time_current = t;
self._time_elapsed = delta;
return self;
## Initialisierung:
_ctr_time = Counter();
_ctr_space = Counter();
_tmr = Timer();
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHODS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def RestartMetrics():
global _ctr_time;
global _ctr_space;
global _tmr;
_ctr_time.reset();
_ctr_space.reset();
_tmr.reset();
return;
def AddTimeCost(n: int = 1):
global _ctr_time;
_ctr_time.add(n);
return;
def AddSpaceCost(n: int = 1):
global _ctr_space;
_ctr_space.add(n);
return;
def GetTimeCost() -> int:
return _ctr_time.numberOfStep;
def GetSpaceCost() -> int:
return _ctr_space.numberOfStep;
def GetTimeElapsed() -> timedelta:
global _tmr;
_tmr.stop();
return _tmr.elapsedTime;

View File

@ -8,6 +8,7 @@
from src.local.typing import *;
from src.core.log import *;
from src.core.metrics import *;
from src.setup.cli import GetArgumentParser;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -67,8 +68,9 @@ def DisplayEndOfAlgorithm(*_: Any, **outputs: Any):
return;
def DisplayMetrics():
logPlain('Dauer der Ausführung: t = \033[1m{}\033[0m'.format(TimeElapsed()));
logPlain('Anzahl der Schritte: T(n) = \033[1m{}\033[0m'.format(NumberOfSteps()));
logPlain('Dauer der Ausführung: t = \033[1m{}\033[0m'.format(GetTimeElapsed()));
logPlain('Kosten (Zeit): T(n) = \033[1m{}\033[0m'.format(GetTimeCost()));
logPlain('Kosten (Platz): S(n) = \033[1m{}\033[0m'.format(GetSpaceCost()));
return;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~