Class MKSProblem
Problem for decision diagram optimization.
Each instance defines a set of items, their profits, and weights along multiple dimensions, along with the capacities of the knapsacks. This class supports evaluation of solutions, state transitions, and generation of variable domains.
States in this problem are represented by MKSState, which tracks the remaining capacities
in each dimension.
The problem can be constructed either programmatically or loaded from a file. The class also provides an optional known optimal solution for testing purposes.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMKSProblem(double[] capa, int[] profit, int[][] weight, double optimal) Constructs an MKSProblem with the given capacities, profits, and weights.MKSProblem(String fname) Loads an MKSProblem from a text file. -
Method Summary
Modifier and TypeMethodDescriptionReturns an iterator over the domain of a variable (item) in a given state.doubleevaluate(int[] solution) Evaluates the cost of a given solution.Returns the initial state for this problem, representing full capacities.doubleReturns the initial value associated with the initial state.intnbVars()Returns the number of decision variables (items).Returns the optional optimal value (negated to follow minimization conventions in DDO).toString()Returns a human-readable string representation of the problem, including capacities, items' profits, weights, and known optimal value.transition(MKSState state, Decision decision) Computes the state resulting from taking a decision in the current state.doubletransitionCost(MKSState state, Decision decision) Computes the cost of taking a decision in a given state.
-
Field Details
-
optimal
Optional known optimal solution value.
-
-
Constructor Details
-
MKSProblem
public MKSProblem(double[] capa, int[] profit, int[][] weight, double optimal) Constructs an MKSProblem with the given capacities, profits, and weights.- Parameters:
capa- the capacities of each knapsack dimensionprofit- the profit of each itemweight- the weight of each item along each dimensionoptimal- the known optimal solution value
-
MKSProblem
Loads an MKSProblem from a text file.The file format is expected to contain:
- First line: number of items, number of dimensions, optional optimal value
- Second line: capacities of each dimension
- Next lines: profit and weights of each item (profit first, then weights)
- Parameters:
fname- the path to the file- Throws:
IOException- if the file cannot be read
-
-
Method Details
-
nbVars
public int nbVars()Returns the number of decision variables (items). -
initialState
Returns the initial state for this problem, representing full capacities.- Specified by:
initialStatein interfaceProblem<MKSState>- Returns:
- initial
MKSState
-
initialValue
public double initialValue()Returns the initial value associated with the initial state.- Specified by:
initialValuein interfaceProblem<MKSState>- Returns:
- 0
-
domain
Returns an iterator over the domain of a variable (item) in a given state.An item can be either taken (1) or not taken (0), depending on remaining capacities.
-
transition
Computes the state resulting from taking a decision in the current state.- Specified by:
transitionin interfaceProblem<MKSState>- Parameters:
state- the current MKS statedecision- the decision to apply- Returns:
- the resulting
MKSStateafter the decision
-
transitionCost
Computes the cost of taking a decision in a given state.The cost is equal to the negative profit if the item is taken, or 0 otherwise.
- Specified by:
transitionCostin interfaceProblem<MKSState>- Parameters:
state- the current MKS statedecision- the decision applied- Returns:
- the transition cost
-
optimalValue
Returns the optional optimal value (negated to follow minimization conventions in DDO).- Specified by:
optimalValuein interfaceProblem<MKSState>- Returns:
- optional negated optimal value
-
evaluate
Evaluates the cost of a given solution.Checks that the solution respects knapsack capacities; if violated, an
InvalidSolutionExceptionis thrown.- Specified by:
evaluatein interfaceProblem<MKSState>- Parameters:
solution- the selection vector of items (1 = taken, 0 = not taken)- Returns:
- the negated total profit of the solution
- Throws:
InvalidSolutionException- if the solution is invalid
-
toString
Returns a human-readable string representation of the problem, including capacities, items' profits, weights, and known optimal value.
-