Package org.ddolib.examples.knapsack
Class KSDistance
java.lang.Object
org.ddolib.examples.knapsack.KSDistance
- All Implemented Interfaces:
StateDistance<Integer>
Distance measure for states in a Knapsack (KS) problem.
This class implements StateDistance for states represented as integers
(typically the current total weight of the knapsack). It provides methods to
compute distances between states and between a state and the root.
Distances are normalized by the knapsack capacity to produce values in [0,1].
-
Constructor Summary
ConstructorsConstructorDescriptionKSDistance(KSProblem problem) Constructs a distance measure for a given Knapsack problem instance. -
Method Summary
Modifier and TypeMethodDescriptiondoubleComputes the normalized distance between two states.doubledistanceWithRoot(Integer state) Computes the normalized distance between a state and the root (empty knapsack).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.ddolib.ddo.core.heuristics.cluster.StateDistance
distance
-
Constructor Details
-
KSDistance
Constructs a distance measure for a given Knapsack problem instance.- Parameters:
problem- the Knapsack problem
-
-
Method Details
-
distance
Computes the normalized distance between two states.The distance is the absolute difference of their integer values divided by the knapsack capacity.
- Specified by:
distancein interfaceStateDistance<Integer>- Parameters:
a- the first stateb- the second state- Returns:
- the normalized distance between
aandb
-
distanceWithRoot
Computes the normalized distance between a state and the root (empty knapsack).- Specified by:
distanceWithRootin interfaceStateDistance<Integer>- Parameters:
state- the state to measure- Returns:
- the normalized distance from the root
-