Skip to content

Commit c5c05ee

Browse files
committed
Integrate tested interval QUBO allocation guard
2 parents 4810b63 + 2226ebe commit c5c05ee

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎src/rules/sequencingwithinintervals_qubo.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ impl ReduceTo<Decision<QUBO<i64>>> for SequencingWithinIntervals {
3838
type Result = ReductionSequencingWithinIntervalsToQUBO;
3939
fn reduce_to(&self) -> Result<Self::Result, ReductionError> {
4040
type Builder = QuboBuilder<SequencingWithinIntervals, Decision<QUBO<i64>>>;
41+
let num_slots = self.num_start_slots();
42+
std::alloc::Layout::array::<(usize, i64, i64)>(num_slots)
43+
.map_err(|_| Builder::overflow())?;
44+
// Each row's usize index vector is no larger than the checked slot array.
45+
// The source constructor also checks the total, so offset + length fits.
4146
let lengths: Vec<_> = self.start_slot_counts().collect();
42-
let mut q = Builder::new(self.num_start_slots());
43-
let mut slots = Vec::with_capacity(self.num_start_slots());
47+
let mut q = Builder::new(num_slots);
48+
let mut slots = Vec::with_capacity(num_slots);
4449
for (i, &length) in lengths.iter().enumerate() {
4550
let offset = slots.len();
4651
q.square(&(offset..offset + length).collect::<Vec<_>>(), 1, 1)?;

‎src/unit_tests/rules/direct_qubo.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,58 @@ fn test_interval_sequencing_to_qubo_closed_loop() {
248248
);
249249
}
250250

251+
#[test]
252+
fn test_interval_sequencing_rejects_unrepresentable_allocations() {
253+
use crate::rules::{ReduceTo, ReductionError};
254+
255+
let max_slots = usize::try_from(isize::MAX).unwrap() / std::mem::size_of::<(usize, i64, i64)>();
256+
let mut counts = vec![i64::try_from(max_slots + 1).unwrap()];
257+
if usize::try_from(i64::MAX).is_ok() {
258+
counts.push(i64::MAX);
259+
}
260+
for count in counts {
261+
let source = SequencingWithinIntervals::new(vec![0], vec![count - 1], vec![0]).unwrap();
262+
assert_eq!(source.evaluate(&vec![0]).unwrap(), Or(true));
263+
assert!(matches!(
264+
<_ as ReduceTo<Decision<QUBO<i64>>>>::reduce_to(&source),
265+
Err(ReductionError::IntegerOverflow { .. })
266+
));
267+
}
268+
269+
// Each task's slots fit individually, but their combined tuple array does not.
270+
let deadline = i64::try_from(max_slots / 2).unwrap();
271+
let source = SequencingWithinIntervals::new(vec![0; 2], vec![deadline; 2], vec![0; 2]).unwrap();
272+
assert_eq!(source.evaluate(&vec![0; 2]).unwrap(), Or(true));
273+
assert!(matches!(
274+
<_ as ReduceTo<Decision<QUBO<i64>>>>::reduce_to(&source),
275+
Err(ReductionError::IntegerOverflow { .. })
276+
));
277+
}
278+
279+
#[test]
280+
fn test_interval_sequencing_allows_representable_time_boundaries() {
281+
feasible(
282+
SequencingWithinIntervals::new(vec![], vec![], vec![]).unwrap(),
283+
true,
284+
0,
285+
);
286+
feasible(
287+
SequencingWithinIntervals::new(vec![i64::MAX - 2], vec![i64::MAX], vec![0]).unwrap(),
288+
true,
289+
3,
290+
);
291+
feasible(
292+
SequencingWithinIntervals::new(
293+
vec![i64::MAX - 2, i64::MAX - 1, i64::MAX],
294+
vec![i64::MAX - 1, i64::MAX, i64::MAX],
295+
vec![1, 1, 0],
296+
)
297+
.unwrap(),
298+
true,
299+
3,
300+
);
301+
}
302+
251303
#[test]
252304
fn test_timetable_to_qubo_closed_loop() {
253305
feasible(

0 commit comments

Comments
 (0)