Class MaxCoverProblem
java.lang.Object
org.ddolib.examples.maximumcoverage.MaxCoverProblem
- All Implemented Interfaces:
Problem<MaxCoverState>
Represents an instance of the Maximum Coverage (MaxCover) problem.
This class implements the Problem interface and provides:
- Creation of random or file-based problem instances
- Evaluation of solutions and state transitions for Decision Diagram Optimization (DDO)
- Computation of centralities for each item, used in heuristics
- Support for reading/writing instances in a standard text format
The problem is defined by:
nbItems: the total number of items to covernbSubSets: the total number of available subsetsnbSubSetsToChoose: the number of subsets allowed to selectsubSets: the array of BitSets representing the items covered by each subsetcentralities: the relative frequency of each item appearing in subsetsoptimal: optionally, the optimal solution value (for benchmarking)
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal double[]Relative frequency (centrality) of each item in all subsets.Optional instance name.final intNumber of items in the instance.final intNumber of subsets in the instance.final intNumber of subsets that can be selected.Optional optimal solution value.final BitSet[]Array of subsets represented as BitSets, where each BitSet indicates the items it covers. -
Constructor Summary
ConstructorsConstructorDescriptionMaxCoverProblem(int n, int m, int k, double maxR, int seed) Generates a random MaxCover instance using coordinates and a distance threshold.MaxCoverProblem(int nbItems, int nbSubSets, int nbSubSetsToChoose, BitSet[] subSets) Constructs a MaxCover instance without name or optimal value.MaxCoverProblem(int nbItems, int nbSubSets, int nbSubSetsToChoose, BitSet[] subSets, Optional<Double> optimal) Constructs a MaxCover instance without a name.MaxCoverProblem(String fname) Loads a MaxCover instance from a file.MaxCoverProblem(Optional<String> name, int nbItems, int nbSubSets, int nbSubSetsToChoose, BitSet[] subSets, Optional<Double> optimal) Constructs a MaxCover instance with full specification. -
Method Summary
Modifier and TypeMethodDescriptiondomain(MaxCoverState state, int var) Returns an iterator over the domain of values for a given variable in a state.doubleevaluate(int[] solution) Evaluates a complete solution.Returns the initial state for the DDO search.doubleReturns the initial objective value for the initial state.Returns a formatted string representing the instance for writing to a file.intnbVars()Returns the number of decision variables in the problem.Returns the optimal value of the instance, if known.toString()Returns a string representation of the instance.transition(MaxCoverState state, Decision decision) Applies a decision to a state to produce a new state.doubletransitionCost(MaxCoverState state, Decision decision) Returns the cost of applying a decision to a state.
-
Field Details
-
nbItems
public final int nbItemsNumber of items in the instance. -
nbSubSets
public final int nbSubSetsNumber of subsets in the instance. -
nbSubSetsToChoose
public final int nbSubSetsToChooseNumber of subsets that can be selected. -
subSets
Array of subsets represented as BitSets, where each BitSet indicates the items it covers. -
centralities
public final double[] centralitiesRelative frequency (centrality) of each item in all subsets. -
name
Optional instance name. -
optimal
Optional optimal solution value.
-
-
Constructor Details
-
MaxCoverProblem
public MaxCoverProblem(Optional<String> name, int nbItems, int nbSubSets, int nbSubSetsToChoose, BitSet[] subSets, Optional<Double> optimal) Constructs a MaxCover instance with full specification.- Parameters:
name- optional instance namenbItems- number of itemsnbSubSets- number of subsetsnbSubSetsToChoose- number of subsets allowed to selectsubSets- array of subsets represented as BitSetsoptimal- optional optimal solution value
-
MaxCoverProblem
public MaxCoverProblem(int nbItems, int nbSubSets, int nbSubSetsToChoose, BitSet[] subSets, Optional<Double> optimal) Constructs a MaxCover instance without a name.- Parameters:
nbItems- number of itemsnbSubSets- number of subsetsnbSubSetsToChoose- number of subsets allowed to selectsubSets- array of subsets represented as BitSetsoptimal- optional optimal solution value
-
MaxCoverProblem
Constructs a MaxCover instance without name or optimal value.- Parameters:
nbItems- number of itemsnbSubSets- number of subsetsnbSubSetsToChoose- number of subsets allowed to selectsubSets- array of subsets represented as BitSets
-
MaxCoverProblem
public MaxCoverProblem(int n, int m, int k, double maxR, int seed) Generates a random MaxCover instance using coordinates and a distance threshold.- Parameters:
n- number of itemsm- number of subsetsk- number of subsets to selectmaxR- maximum coverage radiusseed- random seed
-
MaxCoverProblem
Loads a MaxCover instance from a file.The file should contain the number of items, number of subsets, budget, optional optimal value, and the item indices for each subset.
- Parameters:
fname- path to the instance file- Throws:
IOException- if the file cannot be read
-
-
Method Details
-
instanceFormat
Returns a formatted string representing the instance for writing to a file.- Returns:
- the formatted instance as a string
-
nbVars
public int nbVars()Returns the number of decision variables in the problem.- Specified by:
nbVarsin interfaceProblem<MaxCoverState>- Returns:
- number of variables (subsets to select)
-
initialState
Returns the initial state for the DDO search.- Specified by:
initialStatein interfaceProblem<MaxCoverState>- Returns:
- a state with no items covered
-
initialValue
public double initialValue()Returns the initial objective value for the initial state.- Specified by:
initialValuein interfaceProblem<MaxCoverState>- Returns:
- 0 (no items covered yet)
-
domain
Returns an iterator over the domain of values for a given variable in a state.- Specified by:
domainin interfaceProblem<MaxCoverState>- Parameters:
state- the current statevar- the variable index- Returns:
- an iterator over feasible subset indices (or -1 if no options)
-
transition
Applies a decision to a state to produce a new state.- Specified by:
transitionin interfaceProblem<MaxCoverState>- Parameters:
state- the current statedecision- the decision to apply- Returns:
- a new state reflecting the added subset
-
transitionCost
Returns the cost of applying a decision to a state.Cost is defined as the negative number of newly covered items.
- Specified by:
transitionCostin interfaceProblem<MaxCoverState>- Parameters:
state- the current statedecision- the decision to apply- Returns:
- the transition cost
-
optimalValue
Returns the optimal value of the instance, if known.- Specified by:
optimalValuein interfaceProblem<MaxCoverState>- Returns:
- the negative of the optimal value (for minimization DDO)
-
evaluate
Evaluates a complete solution.- Specified by:
evaluatein interfaceProblem<MaxCoverState>- Parameters:
solution- array of selected subset indices- Returns:
- negative number of items covered
- Throws:
InvalidSolutionException- if the solution length is incorrect
-
toString
Returns a string representation of the instance.
-