Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/solverforge-config/WIREFRAME.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ Derives: `Debug, Clone, Default, Deserialize, Serialize`.
| Field | Type | Default |
|-------|------|---------|
| `construction_heuristic_type` | `ConstructionHeuristicType` | `FirstFit` |
| `construction_obligation` | `ConstructionObligation` | `PreserveUnassigned` |
| `construction_obligation` | `ConstructionObligation` | `PreserveUnassigned`; scalar heuristics only, list heuristics reject any other value |
| `target` | `VariableTargetConfig` | empty target |
| `k` | `usize` | `2`; valid: `2..=5` for `ListKOpt`, which polishes each route with k-opt; other heuristics do not use it and reject any value but `2` |
| `value_candidate_limit` | `Option<usize>` | `None`; valid: `>= 1` when set |
| `value_candidate_limit` | `Option<usize>` | `None`; valid: `>= 1` when set; scalar heuristics only, list heuristics reject it |
| `group_name` | `Option<String>` | `None` |
| `group_candidate_limit` | `Option<usize>` | `None`; valid: `>= 1` when set |
| `group_candidate_limit` | `Option<usize>` | `None`; valid: `>= 1` when set; requires `group_name`; list heuristics reject it |
| `termination` | `Option<TerminationConfig>` | `None` |

`target` is flattened in serde, so configuration files still use top-level
Expand Down
7 changes: 5 additions & 2 deletions crates/solverforge-config/src/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub struct ConstructionHeuristicConfig {
pub construction_heuristic_type: ConstructionHeuristicType,

/// Whether nullable scalar variables may keep their current unassigned value during
/// construction, or must receive a candidate when one exists.
/// construction, or must receive a candidate when one exists. Scalar
/// heuristics only; a list heuristic rejects any value but the default.
#[serde(default)]
pub construction_obligation: ConstructionObligation,

Expand All @@ -49,13 +50,15 @@ pub struct ConstructionHeuristicConfig {
#[serde(default = "default_k")]
pub k: usize,

/// Optional cap for scalar value candidates generated per entity.
/// Optional cap for scalar value candidates generated per entity. Scalar
/// heuristics only; a list heuristic rejects it.
pub value_candidate_limit: Option<usize>,

/// Optional named scalar group for atomic grouped scalar construction.
pub group_name: Option<String>,

/// Optional cap for grouped scalar candidates generated per provider call.
/// Requires `group_name`; a list heuristic rejects it.
pub group_candidate_limit: Option<usize>,

/// Phase termination configuration.
Expand Down
40 changes: 38 additions & 2 deletions crates/solverforge-solver/src/runtime/compiler/selector_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
//! rejected with its key path before any phase runs.

use solverforge_config::{
ConstructionHeuristicConfig, ConstructionHeuristicType, KOptMoveSelectorConfig,
ListPermuteMoveConfig, MoveSelectorConfig, PhaseConfig, SolverConfig,
ConstructionHeuristicConfig, ConstructionHeuristicType, ConstructionObligation,
KOptMoveSelectorConfig, ListPermuteMoveConfig, MoveSelectorConfig, PhaseConfig, SolverConfig,
};

use crate::heuristic::r#move::MAX_LIST_PERMUTE_WINDOW_SIZE;

use super::construction::is_list_construction;
use super::types::{RuntimeCompileError, RuntimeCompileErrorKind};

/// Largest k the k-opt reconnection kernel enumerates.
Expand Down Expand Up @@ -74,10 +75,45 @@ fn validate_construction(
),
));
}
if is_list_construction(config.construction_heuristic_type) {
reject_scalar_only_construction_keys(config, path)?;
}
if config.group_candidate_limit.is_some() && config.group_name.is_none() {
return Err(invalid(
path,
"group_candidate_limit",
"caps grouped scalar candidates and needs `group_name`".to_string(),
));
}
positive_limit(path, "value_candidate_limit", config.value_candidate_limit)?;
positive_limit(path, "group_candidate_limit", config.group_candidate_limit)
}

/// List heuristics place list elements; the keys that shape scalar candidate
/// generation have nothing to apply to there.
fn reject_scalar_only_construction_keys(
config: &ConstructionHeuristicConfig,
path: &str,
) -> Result<(), RuntimeCompileError> {
let scalar_only = |field: &str| {
invalid(
path,
field,
"applies only to scalar construction heuristics, not to list construction".to_string(),
)
};
if config.value_candidate_limit.is_some() {
return Err(scalar_only("value_candidate_limit"));
}
if config.group_candidate_limit.is_some() {
return Err(scalar_only("group_candidate_limit"));
}
if config.construction_obligation != ConstructionObligation::default() {
return Err(scalar_only("construction_obligation"));
}
Ok(())
}

fn validate_selector(config: &MoveSelectorConfig, path: &str) -> Result<(), RuntimeCompileError> {
match config {
MoveSelectorConfig::ChangeMoveSelector(config) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ fn construction_values_reject_their_invalid_value_with_the_key_path() {
"value_candidate_limit",
),
(
"group_candidate_limit = 0",
"group_candidate_limit = 1",
"group_name = \"g\"\ngroup_candidate_limit = 0",
"group_name = \"g\"\ngroup_candidate_limit = 1",
"group_candidate_limit",
),
("k = 3", "k = 2", "k"),
Expand Down Expand Up @@ -272,3 +272,52 @@ fn runtime_graph_compilation_rejects_selector_values_before_any_phase() {
"{error}"
);
}

const LIST_HEURISTICS: [&str; 5] = [
"list_round_robin",
"list_cheapest_insertion",
"list_regret_insertion",
"list_clarke_wright",
"list_k_opt",
];

#[test]
fn list_construction_rejects_scalar_only_keys() {
for heuristic in LIST_HEURISTICS {
let list = |field: &str| {
construction(&format!(
"construction_heuristic_type = \"{heuristic}\"\n{field}"
))
};
assert_valid(&list(""), heuristic);
for (field, value) in [
("value_candidate_limit", "3"),
("group_candidate_limit", "3"),
(
"construction_obligation",
"\"assign_when_candidate_exists\"",
),
] {
assert_invalid(
&list(&format!("{field} = {value}")),
&format!("phases[0].construction.{field}"),
);
}
assert_valid(
&list("construction_obligation = \"preserve_unassigned\""),
"the default obligation",
);
}
}

#[test]
fn group_candidate_limit_needs_a_group_name() {
assert_invalid(
&construction("group_candidate_limit = 3"),
"phases[0].construction.group_candidate_limit",
);
assert_valid(
&construction("group_name = \"g\"\ngroup_candidate_limit = 3"),
"with group_name",
);
}
Loading