Class BKSProblem
java.lang.Object
org.ddolib.examples.boundedknapsack.BKSProblem
Represents an instance of the Bounded Knapsack Problem (BKP).
This class implements the Problem interface and can be used by generic
optimization solvers such as Dynamic Decision Diagrams (DD), A*, or Anytime Column Search (ACS).
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration defining possible correlation types between item weights and profits when generating random instances. -
Field Summary
FieldsModifier and TypeFieldDescriptionfinal intfinal int[]final int[]final int[] -
Constructor Summary
ConstructorsConstructorDescriptionBKSProblem(int capacity, int[] values, int[] weights, int[] quantities) Constructs a bounded knapsack problem from explicitly given parameters.BKSProblem(int n, int range, BKSProblem.InstanceType type, long seed) Randomly generates an instance of the bounded knapsack problem. -
Method Summary
Modifier and TypeMethodDescriptionReturns the domain (set of possible values) for a given variable (item), given the current remaining capacity.doubleevaluate(int[] solution) Given a solution such thatsolution[i]is the value of the variablex_i, returns the value of this solution and checks if the solution respects the problem's constraints.Returns the initial state of the problem, which corresponds to the remaining capacity of the knapsack before adding any items.doubleReturns the initial objective value of the problem.intnbVars()Returns the number of variables (items) in the knapsack.toString()transition(Integer state, Decision decision) Computes the next state after making a decision on an item.doubletransitionCost(Integer state, Decision decision) Computes the transition cost associated with a decision.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.ddolib.modeling.Problem
optimalValue
-
Field Details
-
capacity
public final int capacity -
values
public final int[] values -
weights
public final int[] weights -
quantities
public final int[] quantities
-
-
Constructor Details
-
BKSProblem
public BKSProblem(int capacity, int[] values, int[] weights, int[] quantities) Constructs a bounded knapsack problem from explicitly given parameters.- Parameters:
capacity- the total capacity of the knapsackvalues- an array containing the profit (value) of each itemweights- an array containing the weight of each itemquantities- an array containing the maximum available quantity of each item
-
BKSProblem
Randomly generates an instance of the bounded knapsack problem.The generated instance is controlled by a type of correlation between item weights and values, as described in
BKSProblem.InstanceType.- Parameters:
n- the number of itemsrange- the upper bound for weights and profitstype- the correlation type between weight and profitseed- the random seed used for reproducibility
-
-
Method Details
-
nbVars
public int nbVars()Returns the number of variables (items) in the knapsack. -
initialState
Returns the initial state of the problem, which corresponds to the remaining capacity of the knapsack before adding any items.- Specified by:
initialStatein interfaceProblem<Integer>- Returns:
- the initial remaining capacity
-
initialValue
public double initialValue()Returns the initial objective value of the problem.- Specified by:
initialValuein interfaceProblem<Integer>- Returns:
- the initial value (0.0)
-
domain
Returns the domain (set of possible values) for a given variable (item), given the current remaining capacity.For each item, the decision variable represents the number of copies to include in the knapsack, constrained by both available quantity and remaining capacity.
-
transition
Computes the next state after making a decision on an item.- Specified by:
transitionin interfaceProblem<Integer>- Parameters:
state- the current remaining capacitydecision- the decision specifying which item and how many copies to include- Returns:
- the updated remaining capacity after including the chosen number of items
-
transitionCost
Computes the transition cost associated with a decision.Since this problem is typically formulated as a maximization, this method returns the negative profit to fit a minimization framework.
- Specified by:
transitionCostin interfaceProblem<Integer>- Parameters:
state- the current remaining capacitydecision- the decision specifying which item and how many copies to include- Returns:
- the negative profit of the chosen items
-
evaluate
Description copied from interface:ProblemGiven a solution such thatsolution[i]is the value of the variablex_i, returns the value of this solution and checks if the solution respects the problem's constraints.Note: For maximization problems, the returned value is minus the computed value.
- Specified by:
evaluatein interfaceProblem<Integer>- Parameters:
solution- A solution of the problem.- Returns:
- The value of the input solution
- Throws:
InvalidSolutionException- If the solution does not respect problem's constraints.
-
toString
-