master > master: code py - fractional Werte + Sortierung in Greedy-Summen

This commit is contained in:
RD
2022-06-14 20:02:22 +02:00
parent c6149c230a
commit 3791220cee
4 changed files with 61 additions and 65 deletions

View File

@@ -7,6 +7,7 @@
from __future__ import annotations;
from src.thirdparty.maths import *;
from src.thirdparty.types import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -47,8 +48,9 @@ class Mask():
return ''.join([ str(m.value) for m in self.values ]);
@property
def decision(self) -> List[int]:
return [ x.value for x in self.values ];
def choice(self) -> List[Fraction]:
assert all(x != MaskValue.UNSET for x in self.values);
return [ Fraction(x.value) for x in self.values ];
@property
def indexes_set(self) -> List[int]:
@@ -76,17 +78,11 @@ class Mask():
vector2[self.index] = MaskValue.ONE;
return Mask(vector1), Mask(vector2);
def pad_zeros(self) -> Mask:
def pad(self, x: MaskValue) -> Mask:
'''
Completes mask by filling in unset values with zeros
Pads unset values with a give by given value.
'''
return Mask([ MaskValue.ZERO if u == MaskValue.UNSET else u for u in self.values ]);
def pad_ones(self) -> Mask:
'''
Completes mask by filling in unset values with zeros
'''
return Mask([ MaskValue.ONE if u == MaskValue.UNSET else u for u in self.values ]);
return Mask([ x if u == MaskValue.UNSET else u for u in self.values ]);
@property
def support(self) -> List[int]:

View File

@@ -5,9 +5,9 @@
# IMPORTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from __future__ import annotations
from dataclasses import asdict;
from __future__ import annotations;
from src.thirdparty.maths import *;
from src.thirdparty.types import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -24,7 +24,8 @@ __all__ = [
@dataclass
class SolutionRaw():
vector: List[float] = field();
order: List[int] = field();
choice: List[Fraction] = field();
items: List[str] = field();
values: List[float] = field(repr=False);
costs: List[float] = field(repr=False);
@@ -32,16 +33,12 @@ class SolutionRaw():
class Solution(SolutionRaw):
@property
def support(self) -> List[float]:
return [ i for i, v in enumerate(self.vector) if v > 0 ];
@property
def vector_support(self) -> List[float]:
return [ v for v in self.vector if v > 0 ];
return [ i for i, v in enumerate(self.choice) if v > 0 ];
@property
def total_weight(self) -> float:
return sum([ self.vector[i]*x for (i, x) in zip(self.support, self.costs) ]);
return sum([ self.choice[i]*x for (i, x) in zip(self.support, self.costs) ]);
@property
def total_value(self) -> float:
return sum([ self.vector[i]*x for (i, x) in zip(self.support, self.values) ]);
return sum([ self.choice[i]*x for (i, x) in zip(self.support, self.values) ]);