2022-06-14 01:35:10 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# IMPORTS
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
from src.thirdparty.types import *;
|
|
|
|
from src.thirdparty.maths import *;
|
|
|
|
|
|
|
|
from models.generated.config import *;
|
2022-06-14 10:09:21 +02:00
|
|
|
from src.core.utils import *;
|
2022-06-14 01:35:10 +02:00
|
|
|
from src.models.rucksack import *;
|
|
|
|
from src.models.stacks import *;
|
|
|
|
from src.algorithms.rucksack.display import *;
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# EXPORTS
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'rucksack_greedy_algorithm',
|
|
|
|
'rucksack_branch_and_bound_algorithm',
|
|
|
|
];
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# METHOD greedy algorithm
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
def rucksack_greedy_algorithm(
|
|
|
|
capacity: float,
|
|
|
|
weights: np.ndarray,
|
|
|
|
values: np.ndarray,
|
|
|
|
items: np.ndarray,
|
|
|
|
fractional: bool,
|
|
|
|
verbose: bool,
|
|
|
|
) -> Solution:
|
|
|
|
'''
|
|
|
|
Durch den Greedy-Algorithm wird der optimale Wert eines Rucksacks
|
|
|
|
unter Rücksicht der Kapizitätsschranke eingeschätzt.
|
|
|
|
|
|
|
|
NOTE: Wenn man `fractional = True` verwendet, liefert der Algorithmus
|
|
|
|
eine obere Schranke des maximalen Wertes beim Originalproblem.
|
|
|
|
'''
|
|
|
|
# sortiere daten:
|
2022-06-14 09:03:29 +02:00
|
|
|
order = resort_by_value_per_weight(weights=weights, values=values, items=items);
|
2022-06-14 10:09:21 +02:00
|
|
|
uorder = iperm(order);
|
2022-06-14 09:03:29 +02:00
|
|
|
|
|
|
|
# verbose output hier behandeln (irrelevant für Algorithmus):
|
|
|
|
if verbose:
|
2022-06-14 10:09:21 +02:00
|
|
|
repr = display_order(order=order, weights=weights[uorder], values=values[uorder], items=items[uorder], one_based=True);
|
2022-06-14 09:03:29 +02:00
|
|
|
print('');
|
|
|
|
print('\x1b[1mRucksack Problem - Greedy\x1b[0m');
|
|
|
|
print('');
|
|
|
|
print(repr);
|
|
|
|
print('');
|
2022-06-14 01:35:10 +02:00
|
|
|
|
|
|
|
# führe greedy aus:
|
|
|
|
n = len(weights);
|
|
|
|
weight_total = 0;
|
|
|
|
vector = [ 0 for _ in range(n) ];
|
|
|
|
for i in range(n):
|
|
|
|
# füge Item i hinzu, solange das Gesamtgewicht noch <= Schranke
|
|
|
|
if weight_total + weights[i] <= capacity:
|
|
|
|
weight_total += weights[i];
|
|
|
|
vector[i] = 1;
|
|
|
|
# sonst abbrechen. Falls Bruchteile erlaubt, füge einen Bruchteil des i. Items hinzu
|
|
|
|
else:
|
|
|
|
if fractional:
|
|
|
|
vector[i] = (capacity - weight_total)/weights[i];
|
|
|
|
break;
|
|
|
|
|
2022-06-14 01:53:48 +02:00
|
|
|
# Aspekte der Lösung speichern:
|
2022-06-14 10:09:21 +02:00
|
|
|
rucksack = [i for i, v in enumerate(vector) if v > 0]; # Indexes von Items im Rucksack
|
2022-06-14 01:53:48 +02:00
|
|
|
soln = Solution(
|
|
|
|
vector = vector,
|
|
|
|
items = items[rucksack].tolist(),
|
|
|
|
weights = weights[rucksack].tolist(),
|
|
|
|
values = values[rucksack].tolist(),
|
|
|
|
);
|
|
|
|
|
2022-06-14 01:35:10 +02:00
|
|
|
# verbose output hier behandeln (irrelevant für Algorithmus):
|
|
|
|
if verbose:
|
2022-06-14 10:09:21 +02:00
|
|
|
# Umsortierung rückgängig machen:
|
|
|
|
vector = [ soln.vector[i] for i in order ];
|
2022-06-14 11:24:32 +02:00
|
|
|
rucksack = sorted([ uorder[r] for r in rucksack ]);
|
2022-06-14 10:09:21 +02:00
|
|
|
permute_data(weights=weights, values=values, items=items, perm=uorder);
|
|
|
|
# Ausdrücke bestimmen:
|
|
|
|
expr_value = display_sum(vector=vector, values=values);
|
|
|
|
expr_weight = display_sum(vector=vector, values=weights);
|
|
|
|
print('\x1b[1mEingeschätzte Lösung\x1b[0m');
|
2022-06-14 01:35:10 +02:00
|
|
|
print('');
|
2022-06-14 01:53:48 +02:00
|
|
|
if fractional:
|
2022-06-14 10:09:21 +02:00
|
|
|
print(f'Mask: {soln.vector}');
|
|
|
|
print(f' ---> {vector} (unter urspr. Sortierung)');
|
|
|
|
print(f'Rucksack: {", ".join(items[rucksack])}.');
|
2022-06-14 01:53:48 +02:00
|
|
|
print(f'max. Value ≈ {expr_value}');
|
|
|
|
print(f'∑ Weights = {expr_weight}');
|
2022-06-14 01:35:10 +02:00
|
|
|
print('');
|
|
|
|
|
|
|
|
# Lösung ausgeben
|
2022-06-14 01:53:48 +02:00
|
|
|
return soln;
|
2022-06-14 01:35:10 +02:00
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# METHOD branch and bound algorithm
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
def rucksack_branch_and_bound_algorithm(
|
|
|
|
capacity: float,
|
|
|
|
weights: np.ndarray,
|
|
|
|
values: np.ndarray,
|
|
|
|
items: np.ndarray,
|
|
|
|
verbose: bool,
|
|
|
|
) -> Solution:
|
|
|
|
'''
|
|
|
|
Durch Branch & Bound wird der optimale Wert eines Rucksacks
|
|
|
|
unter Rücksicht der Kapizitätsschranke exakt und effizienter bestimmt.
|
|
|
|
'''
|
|
|
|
|
2022-06-14 09:03:29 +02:00
|
|
|
order = resort_by_value_per_weight(weights=weights, values=values, items=items);
|
2022-06-14 10:09:21 +02:00
|
|
|
uorder = iperm(order);
|
2022-06-14 01:35:10 +02:00
|
|
|
|
2022-06-14 09:03:29 +02:00
|
|
|
# verbose output hier behandeln (irrelevant für Algorithmus):
|
|
|
|
if verbose:
|
2022-06-14 10:09:21 +02:00
|
|
|
repr = display_order(order=order, weights=weights[uorder], values=values[uorder], items=items[uorder], one_based=True);
|
2022-06-14 09:03:29 +02:00
|
|
|
print('');
|
|
|
|
print('\x1b[1mRucksack Problem - Branch & Bound\x1b[0m');
|
|
|
|
print('');
|
|
|
|
print(repr);
|
|
|
|
print('');
|
|
|
|
|
|
|
|
logged_steps = [];
|
2022-06-14 01:35:10 +02:00
|
|
|
vector = empty_mask(n=len(weights));
|
|
|
|
lb_estimate = np.inf;
|
|
|
|
S = Stack();
|
|
|
|
S.push(vector);
|
|
|
|
while not S.empty():
|
2022-06-14 11:24:32 +02:00
|
|
|
lb, u, can_add_all, can_add_none = estimate_lower_bound(mask=S.top(), capacity=capacity, weights=weights, values=values, items=items);
|
2022-06-14 01:35:10 +02:00
|
|
|
if verbose:
|
2022-06-14 12:19:35 +02:00
|
|
|
logged_steps.append((lb_estimate, lb, str(S), u, can_add_all, can_add_none));
|
2022-06-14 01:35:10 +02:00
|
|
|
# Update nur nötig, wenn die (eingeschätzte) untere Schranke von A das bisherige Minimum verbessert:
|
|
|
|
A: Mask = S.pop();
|
|
|
|
if lb < lb_estimate:
|
2022-06-14 11:24:32 +02:00
|
|
|
# Bound, wenn sich A nicht weiter aufteilen lässt od. man A wie eine einelementige Option behandeln kann:
|
|
|
|
if not A.splittable() or can_add_all or can_add_none:
|
|
|
|
lb_estimate = lb;
|
|
|
|
if can_add_all:
|
|
|
|
vector = A.pad_ones();
|
|
|
|
elif can_add_none:
|
|
|
|
vector = A.pad_zeros();
|
|
|
|
else:
|
|
|
|
vector = A;
|
2022-06-14 12:19:35 +02:00
|
|
|
# Branch sonst
|
2022-06-14 11:24:32 +02:00
|
|
|
else:
|
2022-06-14 01:35:10 +02:00
|
|
|
B, C = A.split();
|
|
|
|
S.push(B);
|
|
|
|
# Nur dann C auf Stack legen, wenn mind. eine Möglichkeit in C die Kapazitätsschranke erfüllt:
|
|
|
|
if sum(weights[C.indexes_one]) <= capacity:
|
|
|
|
S.push(C);
|
|
|
|
|
2022-06-14 01:53:48 +02:00
|
|
|
# Aspekte der Lösung speichern
|
|
|
|
rucksack = vector.indexes_one; # Indexes von Items im Rucksack
|
|
|
|
soln = Solution(
|
|
|
|
vector = vector.decision,
|
|
|
|
items = items[rucksack].tolist(),
|
|
|
|
values = values[rucksack].tolist(),
|
|
|
|
weights = weights[rucksack].tolist(),
|
|
|
|
);
|
|
|
|
|
2022-06-14 01:35:10 +02:00
|
|
|
# verbose output hier behandeln (irrelevant für Algorithmus):
|
|
|
|
if verbose:
|
2022-06-14 10:09:21 +02:00
|
|
|
# NOTE: Information in Tabelle gemäß permutierten Daten:
|
2022-06-14 01:35:10 +02:00
|
|
|
repr = display_branch_and_bound(values=values, steps=logged_steps);
|
2022-06-14 10:09:21 +02:00
|
|
|
# Umsortierung rückgängig machen:
|
|
|
|
vector = [ soln.vector[i] for i in order ];
|
2022-06-14 11:24:32 +02:00
|
|
|
rucksack = sorted([ uorder[r] for r in rucksack ]);
|
2022-06-14 10:09:21 +02:00
|
|
|
permute_data(weights=weights, values=values, items=items, perm=uorder);
|
|
|
|
# Ausdrücke bestimmen:
|
|
|
|
expr_value = display_sum(vector=vector, values=values);
|
|
|
|
expr_weight = display_sum(vector=vector, values=weights);
|
|
|
|
print('\x1b[1mLösung\x1b[0m');
|
2022-06-14 01:35:10 +02:00
|
|
|
print('');
|
|
|
|
print(repr);
|
|
|
|
print('');
|
2022-06-14 10:09:21 +02:00
|
|
|
print(f'Mask: {soln.vector}');
|
|
|
|
print(f' ---> {vector} (unter urspr. Sortierung)');
|
|
|
|
print(f'Rucksack: {", ".join(items[rucksack])}.');
|
2022-06-14 01:53:48 +02:00
|
|
|
print(f'max. Value ≈ {expr_value}');
|
|
|
|
print(f'∑ Weights = {expr_weight}');
|
|
|
|
print('');
|
|
|
|
|
2022-06-14 01:35:10 +02:00
|
|
|
# Lösung ausgeben
|
2022-06-14 01:53:48 +02:00
|
|
|
return soln;
|
2022-06-14 01:35:10 +02:00
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# AUXILIARY METHOD resort
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
def resort_by_value_per_weight(
|
|
|
|
weights: np.ndarray,
|
|
|
|
values: np.ndarray,
|
|
|
|
items: np.ndarray,
|
|
|
|
) -> List[int]:
|
|
|
|
'''
|
|
|
|
Sortiert Daten absteigend nach values/weights.
|
|
|
|
'''
|
|
|
|
n = len(weights);
|
|
|
|
indexes = list(range(n));
|
|
|
|
order = sorted(indexes, key=lambda i: -values[i]/weights[i]);
|
2022-06-14 10:09:21 +02:00
|
|
|
permute_data(weights=weights, values=values, items=items, perm=order);
|
2022-06-14 01:35:10 +02:00
|
|
|
return order;
|
|
|
|
|
2022-06-14 10:09:21 +02:00
|
|
|
def permute_data(
|
|
|
|
weights: np.ndarray,
|
|
|
|
values: np.ndarray,
|
|
|
|
items: np.ndarray,
|
|
|
|
perm: List[int],
|
|
|
|
):
|
|
|
|
weights[:] = weights[perm];
|
|
|
|
values[:] = values[perm];
|
|
|
|
items[:] = items[perm];
|
|
|
|
return;
|
|
|
|
|
2022-06-14 01:35:10 +02:00
|
|
|
def estimate_lower_bound(
|
|
|
|
mask: Mask,
|
|
|
|
capacity: float,
|
|
|
|
weights: np.ndarray,
|
|
|
|
values: np.ndarray,
|
|
|
|
items: np.ndarray,
|
2022-06-14 11:24:32 +02:00
|
|
|
) -> Tuple[float, List[float], bool]:
|
2022-06-14 01:35:10 +02:00
|
|
|
'''
|
|
|
|
Wenn partielle Information über den Rucksack festgelegt ist,
|
|
|
|
kann man bei dem unbekannten Teil das Rucksack-Problem
|
|
|
|
mit Greedy-Algorithmus »lösen«,
|
|
|
|
um schnell eine gute Einschätzung zu bestimmen.
|
|
|
|
|
|
|
|
NOTE: Diese Funktion wird `g(vector)` im Skript bezeichnet.
|
|
|
|
'''
|
|
|
|
indexes_one = mask.indexes_one;
|
|
|
|
indexes_unset = mask.indexes_unset;
|
|
|
|
n = len(mask);
|
|
|
|
vector = np.zeros(shape=(n,), dtype=float);
|
2022-06-14 12:19:35 +02:00
|
|
|
|
|
|
|
# Berechnungen bei Items mit bekanntem Status in Rucksack:
|
|
|
|
value_rucksack = sum(values[indexes_one]);
|
|
|
|
weight_rucksack = sum(weights[indexes_one]);
|
2022-06-14 01:35:10 +02:00
|
|
|
vector[indexes_one] = 1;
|
|
|
|
|
2022-06-14 12:19:35 +02:00
|
|
|
# Für Rest des Rucksacks (Items mit unbekanntem Status):
|
|
|
|
weight_rest = capacity - weight_rucksack;
|
|
|
|
can_add_all = False;
|
|
|
|
can_add_none = False;
|
|
|
|
# Prüfe, ob man als Lösung alles/nichts hinzufügen kann:
|
|
|
|
if len(indexes_unset) > 0 and sum(weights[indexes_unset]) <= weight_rest:
|
|
|
|
can_add_all = True;
|
|
|
|
vector[indexes_unset] = 1;
|
|
|
|
value_rest = sum(values[indexes_unset]);
|
|
|
|
elif len(indexes_unset) > 0 and min(weights[indexes_unset]) > weight_rest:
|
|
|
|
can_add_none = True;
|
|
|
|
vector[indexes_unset] = 0;
|
|
|
|
value_rest = 0;
|
|
|
|
# Sonst mit Greedy-Algorithmus lösen:
|
|
|
|
# NOTE: Lösung ist eine Überschätzung des max-Wertes.
|
|
|
|
else:
|
|
|
|
soln_rest = rucksack_greedy_algorithm(
|
|
|
|
capacity = weight_rest, # <- Kapazität = Restgewicht
|
|
|
|
weights = weights[indexes_unset],
|
|
|
|
values = values[indexes_unset],
|
|
|
|
items = items[indexes_unset],
|
|
|
|
fractional = True,
|
|
|
|
verbose = False,
|
|
|
|
);
|
|
|
|
value_rest = soln_rest.total_value;
|
|
|
|
vector[indexes_unset] = soln_rest.vector;
|
2022-06-14 11:24:32 +02:00
|
|
|
|
2022-06-14 12:19:35 +02:00
|
|
|
# Einschätzung des max-Wertes:
|
2022-06-14 01:35:10 +02:00
|
|
|
value_max_est = value_rucksack + value_rest;
|
2022-06-14 12:19:35 +02:00
|
|
|
|
|
|
|
# Ausgabe mit -1 multiplizieren (weil maximiert wird):
|
2022-06-14 11:24:32 +02:00
|
|
|
return -value_max_est, vector.tolist(), can_add_all, can_add_none;
|