master > master: code py - algorithmen für rucksackproblem
This commit is contained in:
@@ -42,4 +42,6 @@ def interpret_command(command: Command) -> Command:
|
||||
return CommandTsp(**command.dict());
|
||||
case EnumAlgorithmNames.hirschberg:
|
||||
return CommandHirschberg(**command.dict());
|
||||
case EnumAlgorithmNames.rucksack:
|
||||
return CommandRucksack(**command.dict());
|
||||
raise command;
|
||||
|
||||
20
code/python/src/models/rucksack/__init__.py
Normal file
20
code/python/src/models/rucksack/__init__.py
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from src.models.rucksack.mask import *;
|
||||
from src.models.rucksack.solution import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'empty_mask',
|
||||
'MaskValue',
|
||||
'Mask',
|
||||
'Solution',
|
||||
];
|
||||
80
code/python/src/models/rucksack/mask.py
Normal file
80
code/python/src/models/rucksack/mask.py
Normal file
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from __future__ import annotations;
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'empty_mask',
|
||||
'MaskValue',
|
||||
'Mask',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# ENUMS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class MaskValue(Enum):
|
||||
ZERO = 0;
|
||||
ONE = 1;
|
||||
UNSET = '*';
|
||||
|
||||
class Mask():
|
||||
index: int;
|
||||
values: List[MaskValue];
|
||||
|
||||
def __init__(self, values: List[MaskValue]):
|
||||
self.values = values;
|
||||
if MaskValue.UNSET in values:
|
||||
self.index = values.index(MaskValue.UNSET);
|
||||
else:
|
||||
self.index = -1;
|
||||
return;
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.values);
|
||||
|
||||
def __str__(self) -> str:
|
||||
return ''.join([ str(m.value) for m in self.values ]);
|
||||
|
||||
@property
|
||||
def decision(self) -> List[int]:
|
||||
return [ x.value for x in self.values ];
|
||||
|
||||
@property
|
||||
def indexes_set(self) -> List[int]:
|
||||
return [i for i, value in enumerate(self.values) if value != MaskValue.UNSET];
|
||||
|
||||
@property
|
||||
def indexes_one(self) -> List[int]:
|
||||
return [i for i, value in enumerate(self.values) if value == MaskValue.ONE];
|
||||
|
||||
@property
|
||||
def indexes_zero(self) -> List[int]:
|
||||
return [i for i, value in enumerate(self.values) if value == MaskValue.ZERO];
|
||||
|
||||
@property
|
||||
def indexes_unset(self) -> List[int]:
|
||||
return [i for i, value in enumerate(self.values) if value == MaskValue.UNSET];
|
||||
|
||||
def splittable(self) -> bool:
|
||||
return self.index >= 0;
|
||||
|
||||
def split(self) -> Tuple[Mask, Mask]:
|
||||
vector1 = self.values[:];
|
||||
vector1[self.index] = MaskValue.ZERO;
|
||||
vector2 = self.values[:];
|
||||
vector2[self.index] = MaskValue.ONE;
|
||||
return Mask(vector1), Mask(vector2);
|
||||
|
||||
def empty_mask(n: int):
|
||||
return Mask([MaskValue.UNSET for _ in range(n)]);
|
||||
43
code/python/src/models/rucksack/solution.py
Normal file
43
code/python/src/models/rucksack/solution.py
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from __future__ import annotations
|
||||
from dataclasses import asdict;
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'Solution',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# ENUMS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@dataclass
|
||||
class SolutionRaw():
|
||||
vector: Union[List[int], List[float]] = field();
|
||||
items: List[str] = field();
|
||||
values: List[float] = field(repr=False);
|
||||
weights: List[float] = field(repr=False);
|
||||
|
||||
class Solution(SolutionRaw):
|
||||
@property
|
||||
def support(self) -> List[float]:
|
||||
return [ i for i, v in enumerate(self.vector) if v > 0 ];
|
||||
|
||||
@property
|
||||
def total_weight(self) -> float:
|
||||
return sum([ self.vector[i]*x for (i, x) in zip(self.support, self.weights) ]);
|
||||
|
||||
@property
|
||||
def total_value(self) -> float:
|
||||
return sum([ self.vector[i]*x for (i, x) in zip(self.support, self.values) ]);
|
||||
@@ -39,6 +39,13 @@ class Stack:
|
||||
def __contains__(self, value: Any) -> bool:
|
||||
return value in self.elements;
|
||||
|
||||
def __iter__(self) -> Generator[Any, None, None]:
|
||||
for value in self.elements:
|
||||
yield value;
|
||||
|
||||
def __str__(self) -> str:
|
||||
return ', '.join([str(value) for value in self.elements[::-1]]);
|
||||
|
||||
def push(self, value: Any):
|
||||
'''
|
||||
add element to stack
|
||||
@@ -68,3 +75,6 @@ class Stack:
|
||||
checks if element in stack:
|
||||
'''
|
||||
return element in self.elements;
|
||||
|
||||
def empty(self) -> bool:
|
||||
return len(self) == 0;
|
||||
|
||||
Reference in New Issue
Block a user