Browse Source

master > master: code py - fügte option hinzu, um auch 0-costs anzuzeigen

- Abdunkeln in Summen jetzt nicht bei Werten
RD 2 months ago
parent
commit
d07a76ce5d
  1. 2
      code/python/assets/config.yaml
  2. 21
      code/python/models/config-schema.yaml
  3. 4
      code/python/src/algorithms/rucksack/algorithms.py
  4. 25
      code/python/src/algorithms/rucksack/display.py

2
code/python/assets/config.yaml

@ -30,3 +30,5 @@ options:
- TREE
rucksack:
verbose: true
show:
- ALL-WEIGHTS

21
code/python/models/config-schema.yaml

@ -53,7 +53,7 @@ components:
- tsp
- tarjan
- hirschberg
- rucksack-branch-and-bound
- rucksack
properties:
log-level:
$ref: '#/components/schemas/EnumLogLevel'
@ -117,12 +117,16 @@ components:
default: []
rucksack:
type: object
required:
- verbose
required: []
properties:
verbose:
type: boolean
default: false
show:
type: array
items:
$ref: '#/components/schemas/EnumRucksackShow'
default: []
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Enum LogLevel
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -148,8 +152,17 @@ components:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EnumHirschbergShow:
description: |-
Enumeration of verbosity options for Hirschberg
Enumeration of display options for Hirschberg
type: string
enum:
- TREE
- ATOMS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Enum Rucksack - display options
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EnumRucksackShow:
description: |-
Enumeration of display options for the Rucksack problem
type: string
enum:
- ALL-WEIGHTS

4
code/python/src/algorithms/rucksack/algorithms.py

@ -83,7 +83,7 @@ def rucksack_greedy_algorithm(
# verbose output hier behandeln (irrelevant für Algorithmus):
if verbose:
repr_rucksack = display_rucksack(items=items[rucksack], costs=costs[rucksack], values=values[rucksack], choice=np.asarray(choice)[rucksack]);
repr_rucksack = display_rucksack(items=items, costs=costs, values=values, choice=choice);
print('\x1b[1mEingeschätzte Lösung\x1b[0m');
print('');
print(f'Mask: [{", ".join(map(str, soln.choice))}]');
@ -173,7 +173,7 @@ def rucksack_branch_and_bound_algorithm(
# verbose output hier behandeln (irrelevant für Algorithmus):
if verbose:
repr = display_branch_and_bound(values=values, steps=logged_steps);
repr_rucksack = display_rucksack(items=items[rucksack], costs=costs[rucksack], values=values[rucksack], choice=np.asarray(mask.choice)[rucksack]);
repr_rucksack = display_rucksack(items=items, costs=costs, values=values, choice=mask.choice);
print(repr);
print('');
print('\x1b[1mLösung\x1b[0m');

25
code/python/src/algorithms/rucksack/display.py

@ -9,6 +9,8 @@ from src.thirdparty.code import *;
from src.thirdparty.maths import *;
from src.thirdparty.types import *;
from src.setup import config;
from models.generated.config import *;
from src.models.stacks import *;
from src.models.rucksack import *;
@ -62,9 +64,17 @@ def display_rucksack(
items: np.ndarray,
costs: np.ndarray,
values: np.ndarray,
choice: np.ndarray,
choice: List[Fraction],
) -> str:
show_options = config.OPTIONS.rucksack.show;
render = lambda r: f'{r:g}';
choice = np.asarray(choice);
rucksack = np.where(choice > 0);
if not(EnumRucksackShow.all_weights in show_options):
items = items[rucksack];
costs = costs[rucksack];
values = values[rucksack];
choice = choice[rucksack];
table = pd.DataFrame({
'items': items.tolist() + ['----', ''],
'nr': list(map(str, choice))
@ -132,15 +142,22 @@ def display_sum(
indexes: List[int] = [],
as_maximum: bool = True,
) -> str:
show_options = config.OPTIONS.rucksack.show;
show_all_weights = (EnumRucksackShow.all_weights in show_options);
def render(x: Tuple[bool, Fraction, float]):
b, u, value = x;
expr = f'\x1b[91m{value:g}\x1b[0m' if b else f'\x1b[2m{value:g}\x1b[0m';
return expr if u == 1 else f'\x1b[4;2m{u}\x1b[0m\x1b[2m·\x1b[0m{expr}';
expr = f'\x1b[91m{value:g}\x1b[0m' if b else f'\x1b[0m{value:g}\x1b[0m';
if not show_all_weights and u == 1:
return expr;
return f'\x1b[2m{u}\x1b[0m\x1b[2m·\x1b[0m{expr}';
parts = [ (i in indexes, u, x) for i, (u, x) in enumerate(zip(choice, values)) ];
if not (order is None):
parts = [ parts[j] for j in order ];
parts = list(filter(lambda x: x[1] > 0, parts));
if not show_all_weights:
parts = list(filter(lambda x: x[1] > 0, parts));
value = sum([ u*x for _, u, x in parts ]);
expr = '\x1b[2m+\x1b[0m'.join(map(render, parts));

Loading…
Cancel
Save