selen 0.15.5

Constraint Satisfaction Problem (CSP) solver
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Test suite for Step 6.3: Subproblem Solving Strategies
//!
//! This module tests the specialized solving strategies for partitioned subproblems.
//! Tests cover float subproblem solving, integer subproblem solving, and coordination.

#[cfg(test)]
mod tests {
    use super::super::subproblem_solving::*;
    use super::super::variable_partitioning::*;
    use crate::model::Model;
    use crate::variables::VarId;
    use std::time::Duration;
    
    fn create_float_model() -> (Model, Vec<VarId>) {
        let mut model = Model::with_float_precision(3);
        
        // Add some float variables
        let var0 = m.float(-10.0, 10.0);
        let var1 = m.float(0.0, 100.0);
        let var2 = m.float(-5.0, 5.0);
        
        (model, vec![var0, var1, var2])
    }
    
    fn create_integer_model() -> (Model, Vec<VarId>) {
        let mut model = Model::with_float_precision(3);
        
        // Add some integer variables
        let var0 = m.int(1, 10);
        let var1 = m.int(-5, 5);
        let var2 = m.int(0, 100);
        
        (model, vec![var0, var1, var2])
    }
    
    fn create_mixed_model() -> (Model, Vec<VarId>) {
        let mut model = Model::with_float_precision(3);
        
        // Add mixed variables
        let var0 = m.float(0.0, 10.0);  // float
        let var1 = m.int(1, 5);         // integer
        let var2 = m.float(-1.0, 1.0);  // float
        let var3 = m.int(10, 20);       // integer
        
        (model, vec![var0, var1, var2, var3])
    }
    
    #[test]
    fn test_float_solver_creation() {
        let _solver = FloatSubproblemSolver::new(3);
        // Test that creation succeeds - precision_digits field is private
        
        let _solver_with_timeout = FloatSubproblemSolver::new(2)
            .with_timeout(Duration::from_millis(500));
        // Test that timeout configuration doesn't crash
    }
    
    #[test]
    fn test_integer_solver_creation() {
        let _solver = IntegerSubproblemSolver::new();
        // Test that creation succeeds - max_depth field is private
        
        let _solver_with_config = IntegerSubproblemSolver::new()
            .with_max_depth(500)
            .with_timeout(Duration::from_millis(2000));
        // Test that configuration doesn't crash
    }
    
    #[test]
    fn test_float_subproblem_solving() {
        let (model, var_ids) = create_float_model();
        let solver = FloatSubproblemSolver::new(3);
        
        // Create a float partition
        let float_partition = VariablePartition {
            float_variables: var_ids.clone(),
            integer_variables: vec![],
            constraint_count: 0,
        };
        
        let result = solver.solve_float_subproblem(&model, &float_partition);
        assert!(result.is_ok());
        
        let solution = result.unwrap();
        assert!(solution.is_solved);
        assert_eq!(solution.variable_count, 3);
        assert_eq!(solution.variable_assignments.len(), 3);
        
        // Check that solutions are within bounds
        for (var_id, value) in &solution.variable_assignments {
            if let SubproblemValue::Float(val) = value {
                // Find which variable this is based on order
                let index = var_ids.iter().position(|v| v == var_id).unwrap();
                match index {
                    0 => assert!(*val >= -10.0 && *val <= 10.0),
                    1 => assert!(*val >= 0.0 && *val <= 100.0),
                    2 => assert!(*val >= -5.0 && *val <= 5.0),
                    _ => panic!("Unexpected variable ID"),
                }
            } else {
                panic!("Expected float value");
            }
        }
    }
    
    #[test]
    fn test_integer_subproblem_solving() {
        let (model, var_ids) = create_integer_model();
        let solver = IntegerSubproblemSolver::new();
        
        // Create an integer partition
        let integer_partition = VariablePartition {
            float_variables: vec![],
            integer_variables: var_ids.clone(),
            constraint_count: 0,
        };
        
        let result = solver.solve_integer_subproblem(&model, &integer_partition);
        assert!(result.is_ok());
        
        let solution = result.unwrap();
        assert!(solution.is_solved);
        assert_eq!(solution.variable_count, 3);
        assert_eq!(solution.variable_assignments.len(), 3);
        
        // Check that solutions are within bounds
        for (var_id, value) in &solution.variable_assignments {
            if let SubproblemValue::Integer(val) = value {
                // Find which variable this is based on order
                let index = var_ids.iter().position(|v| v == var_id).unwrap();
                match index {
                    0 => assert!(*val >= 1 && *val <= 10),
                    1 => assert!(*val >= -5 && *val <= 5),
                    2 => assert!(*val >= 0 && *val <= 100),
                    _ => panic!("Unexpected variable ID"),
                }
            } else {
                panic!("Expected integer value");
            }
        }
    }
    
    #[test]
    fn test_float_solver_empty_partition() {
        let (model, _) = create_float_model();
        let solver = FloatSubproblemSolver::new(3);
        
        // Create an empty float partition
        let empty_partition = VariablePartition {
            float_variables: vec![],
            integer_variables: vec![],
            constraint_count: 0,
        };
        
        let result = solver.solve_float_subproblem(&model, &empty_partition);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SubproblemSolvingError::FloatSolvingFailed(FloatSolvingError::EmptyPartition)));
    }
    
    #[test]
    fn test_integer_solver_empty_partition() {
        let (model, _) = create_integer_model();
        let solver = IntegerSubproblemSolver::new();
        
        // Create an empty integer partition
        let empty_partition = VariablePartition {
            float_variables: vec![],
            integer_variables: vec![],
            constraint_count: 0,
        };
        
        let result = solver.solve_integer_subproblem(&model, &empty_partition);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SubproblemSolvingError::IntegerSolvingFailed(IntegerSolvingError::EmptyPartition)));
    }
    
    #[test]
    fn test_coordinator_creation() {
        let _coordinator = SubproblemCoordinator::new(3);
        // We can't access private fields directly, so just test creation succeeds
        
        let _coordinator_with_timeout = SubproblemCoordinator::new(2)
            .with_global_timeout(Duration::from_millis(5000));
        // Test that timeout configuration doesn't crash
    }
    
    #[test]
    fn test_coordinator_solve_float_only_problem() {
        let (model, var_ids) = create_float_model();
        let coordinator = SubproblemCoordinator::new(3);
        
        // Create a partition result with only float variables
        let float_partition = VariablePartition {
            float_variables: var_ids,
            integer_variables: vec![],
            constraint_count: 0,
        };
        
        let partition_result = PartitionResult {
            float_partition: Some(float_partition),
            integer_partition: None,
            is_separable: true,
            total_variables: 3,
            total_constraints: 0,
        };
        
        let result = coordinator.solve_partitioned_problem(&model, &partition_result);
        assert!(result.is_ok());
        
        let combined_solution = result.unwrap();
        assert!(combined_solution.is_complete);
        assert_eq!(combined_solution.all_assignments.len(), 3);
        assert_eq!(combined_solution.subproblem_results.len(), 1);
        assert!(combined_solution.speedup_factor > 1.0);
    }
    
    #[test]
    fn test_coordinator_solve_integer_only_problem() {
        let (model, var_ids) = create_integer_model();
        let coordinator = SubproblemCoordinator::new(3);
        
        // Create a partition result with only integer variables
        let integer_partition = VariablePartition {
            float_variables: vec![],
            integer_variables: var_ids,
            constraint_count: 0,
        };
        
        let partition_result = PartitionResult {
            float_partition: None,
            integer_partition: Some(integer_partition),
            is_separable: true,
            total_variables: 3,
            total_constraints: 0,
        };
        
        let result = coordinator.solve_partitioned_problem(&model, &partition_result);
        assert!(result.is_ok());
        
        let combined_solution = result.unwrap();
        assert!(combined_solution.is_complete);
        assert_eq!(combined_solution.all_assignments.len(), 3);
        assert_eq!(combined_solution.subproblem_results.len(), 1);
        assert!(combined_solution.speedup_factor > 1.0);
    }
    
    #[test]
    fn test_coordinator_solve_mixed_separable_problem() {
        let (model, var_ids) = create_mixed_model();
        let coordinator = SubproblemCoordinator::new(3);
        
        // Create a partition result with both float and integer subproblems
        let float_partition = VariablePartition {
            float_variables: vec![var_ids[0], var_ids[2]], // vars 0 and 2 are float
            integer_variables: vec![],
            constraint_count: 0,
        };
        
        let integer_partition = VariablePartition {
            float_variables: vec![],
            integer_variables: vec![var_ids[1], var_ids[3]], // vars 1 and 3 are integer
            constraint_count: 0,
        };
        
        let partition_result = PartitionResult {
            float_partition: Some(float_partition),
            integer_partition: Some(integer_partition),
            is_separable: true,
            total_variables: 4,
            total_constraints: 0,
        };
        
        let result = coordinator.solve_partitioned_problem(&model, &partition_result);
        assert!(result.is_ok());
        
        let combined_solution = result.unwrap();
        assert!(combined_solution.is_complete);
        assert_eq!(combined_solution.all_assignments.len(), 4);
        assert_eq!(combined_solution.subproblem_results.len(), 2);
        
        // Check that we have both float and integer assignments
        let mut float_count = 0;
        let mut integer_count = 0;
        
        for value in combined_solution.all_assignments.values() {
            match value {
                SubproblemValue::Float(_) => float_count += 1,
                SubproblemValue::Integer(_) => integer_count += 1,
            }
        }
        
        assert_eq!(float_count, 2);
        assert_eq!(integer_count, 2);
        assert!(combined_solution.speedup_factor > 1.0);
    }
    
    #[test]
    fn test_coordinator_no_subproblems() {
        let (model, _) = create_mixed_model();
        let coordinator = SubproblemCoordinator::new(3);
        
        // Create a partition result with no subproblems
        let partition_result = PartitionResult {
            float_partition: None,
            integer_partition: None,
            is_separable: false,
            total_variables: 4,
            total_constraints: 0,
        };
        
        let result = coordinator.solve_partitioned_problem(&model, &partition_result);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), SubproblemSolvingError::NoSubproblems));
    }
    
    #[test]
    fn test_solve_with_partitioning_convenience_function() {
        let (model, var_ids) = create_float_model();
        
        // Create a simple partition result
        let float_partition = VariablePartition {
            float_variables: vec![var_ids[0], var_ids[1]],
            integer_variables: vec![],
            constraint_count: 0,
        };
        
        let partition_result = PartitionResult {
            float_partition: Some(float_partition),
            integer_partition: None,
            is_separable: true,
            total_variables: 2,
            total_constraints: 0,
        };
        
        let result = solve_with_partitioning(&model, &partition_result);
        assert!(result.is_ok());
        
        let combined_solution = result.unwrap();
        assert!(combined_solution.is_complete);
        assert_eq!(combined_solution.all_assignments.len(), 2);
    }
    
    #[test]
    fn test_subproblem_value_equality() {
        let float_val1 = SubproblemValue::Float(3.14);
        let float_val2 = SubproblemValue::Float(3.14);
        let float_val3 = SubproblemValue::Float(2.71);
        
        let int_val1 = SubproblemValue::Integer(42);
        let int_val2 = SubproblemValue::Integer(42);
        let int_val3 = SubproblemValue::Integer(7);
        
        assert_eq!(float_val1, float_val2);
        assert_ne!(float_val1, float_val3);
        assert_eq!(int_val1, int_val2);
        assert_ne!(int_val1, int_val3);
        assert_ne!(float_val1, int_val1);
    }
    
    #[test]
    fn test_error_display() {
        let errors = vec![
            SubproblemSolvingError::FloatSolvingFailed(FloatSolvingError::EmptyPartition),
            SubproblemSolvingError::IntegerSolvingFailed(IntegerSolvingError::EmptyPartition),
            SubproblemSolvingError::TimeoutExceeded,
            SubproblemSolvingError::CombinationFailed(CombinationError::InvalidStructure),
            SubproblemSolvingError::NoSubproblems,
        ];
        
        for error in errors {
            let error_string = format!("{}", error);
            assert!(!error_string.is_empty());
        }
    }
    
    #[test]
    fn test_performance_characteristics() {
        let (model, var_ids) = create_mixed_model();
        let coordinator = SubproblemCoordinator::new(3);
        
        // Create a larger partition to test performance
        let float_partition = VariablePartition {
            float_variables: vec![var_ids[0], var_ids[2]],
            integer_variables: vec![],
            constraint_count: 0,
        };
        
        let integer_partition = VariablePartition {
            float_variables: vec![],
            integer_variables: vec![var_ids[1], var_ids[3]],
            constraint_count: 0,
        };
        
        let partition_result = PartitionResult {
            float_partition: Some(float_partition),
            integer_partition: Some(integer_partition),
            is_separable: true,
            total_variables: 4,
            total_constraints: 0,
        };
        
        let start_time = std::time::Instant::now();
        let result = coordinator.solve_partitioned_problem(&model, &partition_result);
        let elapsed = start_time.elapsed();
        
        assert!(result.is_ok());
        let combined_solution = result.unwrap();
        
        // Performance assertions
        assert!(elapsed < Duration::from_millis(100)); // Should be very fast for small problems
        assert!(combined_solution.total_time < Duration::from_millis(50));
        assert!(combined_solution.speedup_factor >= 1.0);
        
        println!("Step 6.3 Performance:");
        println!("  Total solving time: {:?}", combined_solution.total_time);
        println!("  Estimated speedup: {:.1}x", combined_solution.speedup_factor);
        println!("  Variables solved: {}", combined_solution.all_assignments.len());
        println!("  Subproblems: {}", combined_solution.subproblem_results.len());
    }
}