Class GRProblem
The Golomb Ruler problem consists in placing n marks on a ruler such that
all pairwise distances between marks are distinct, and the length of the ruler
(i.e., the position of the last mark) is minimized.
This class defines the problem structure and state transitions to be used in optimization or search algorithms (e.g., A*, DDO, or constraint programming). Each state represents a partially constructed ruler with a set of marks and the distances between them.
Example:
GRProblem problem = new GRProblem(4);
GRState initial = problem.initialState();
Iterator<Integer> domain = problem.domain(initial, 0);
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the possible domain values for the next decision (i.e., possible positions for the next mark).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, containing only the first mark at position 0.doubleReturns the initial cost (always 0).intnbVars()Returns the number of variables in the problem.Returns the known optimal value of the problem, if available.toString()Returns a human-readable representation of the problem instance.transition(GRState state, Decision decision) Computes the next state resulting from applying a decision (adding a new mark).doubletransitionCost(GRState state, Decision decision) Computes the cost associated with a transition between states.
-
Constructor Details
-
GRProblem
public GRProblem(int n) Constructs a Golomb Ruler problem withnmarks and no known optimal value.- Parameters:
n- the number of marks to place on the ruler.
-
GRProblem
public GRProblem(int n, double optimal) Constructs a Golomb Ruler problem withnmarks and a known optimal length.- Parameters:
n- the number of marks to place on the ruler.optimal- the known optimal length of the ruler.
-
-
Method Details
-
toString
Returns a human-readable representation of the problem instance. -
nbVars
public int nbVars()Returns the number of variables in the problem. For a ruler withnmarks, there aren - 1decision variables. -
initialState
Returns the initial state of the problem, containing only the first mark at position 0.- Specified by:
initialStatein interfaceProblem<GRState>- Returns:
- the initial
GRState.
-
initialValue
public double initialValue()Returns the initial cost (always 0).- Specified by:
initialValuein interfaceProblem<GRState>- Returns:
- the initial cost value.
-
domain
Returns the possible domain values for the next decision (i.e., possible positions for the next mark).The method ensures that no pairwise distance between existing marks and the new mark duplicates an already existing distance.
-
transition
Computes the next state resulting from applying a decision (adding a new mark).The new state updates the set of marks and the set of pairwise distances.
- Specified by:
transitionin interfaceProblem<GRState>- Parameters:
state- the current state.decision- the decision representing the position of the new mark.- Returns:
- a new
GRStaterepresenting the updated configuration.
-
transitionCost
Computes the cost associated with a transition between states.The cost corresponds to the distance between the newly placed mark and the previous one.
- Specified by:
transitionCostin interfaceProblem<GRState>- Parameters:
state- the current state.decision- the decision leading to the next mark placement.- Returns:
- the incremental cost, equal to
decision.val() - state.getLastMark().
-
optimalValue
Returns the known optimal value of the problem, if available.- Specified by:
optimalValuein interfaceProblem<GRState>- Returns:
- an
Optionalcontaining the optimal value (negated), or empty if the optimal value is unknown.
-
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<GRState>- 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.
-