1
//! Normalisation of optimisation objectives before rewriting.
2

            
3
use crate::ast::{Atom, Expression, Metadata, Model, Moo, Reference};
4
use crate::bug;
5

            
6
/// Introduces an auxiliary find variable for a non-atomic optimisation objective and links it
7
/// with an aux declaration constraint.
8
///
9
/// Objectives that are already atoms (for example `minimising z`) are left unchanged.
10
1447046
pub fn introduce_objective_auxiliary(mut model: Model) -> Model {
11
1447046
    let Some(objective) = model.objective.as_ref() else {
12
1424251
        return model;
13
    };
14

            
15
22795
    if matches!(&objective.expression, Expression::Atomic(_, _)) {
16
2707
        return model;
17
20088
    }
18

            
19
20088
    let expr = objective.expression.clone();
20

            
21
20088
    let Some(domain) = expr.domain_of() else {
22
        bug!(
23
            "objective expression has no domain and could not be introduced as an auxiliary variable: {expr}"
24
        );
25
    };
26

            
27
20088
    let mut symbols = model.symbols().clone();
28
20088
    let decl = symbols.gen_find_auxiliary(&domain);
29
20088
    let aux_reference = Expression::Atomic(Metadata::new(), Atom::new_ref(decl.clone()));
30
    let aux_constraint =
31
20088
        Expression::AuxDeclaration(Metadata::new(), Reference::new(decl), Moo::new(expr));
32

            
33
20088
    model.symbols_mut().extend(symbols);
34
20088
    model.add_constraint(aux_constraint);
35
20088
    model
36
20088
        .objective
37
20088
        .as_mut()
38
20088
        .expect("objective should still be present")
39
20088
        .expression = aux_reference;
40

            
41
20088
    model
42
1447046
}