powerio_prob/instance/
objective.rs1use serde::{Deserialize, Serialize};
11
12#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
14#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
15#[serde(rename_all = "snake_case", tag = "term")]
16#[non_exhaustive]
17pub enum ObjectiveTerm {
18 NetworkGeneratorCost,
21 ActivePowerDispatchCost,
24}
25
26#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
28#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
29pub struct Objective {
30 terms: Vec<ObjectiveTerm>,
31}
32
33impl Objective {
34 #[must_use]
36 pub const fn none() -> Self {
37 Self { terms: Vec::new() }
38 }
39
40 #[must_use]
42 pub fn network_generator_cost() -> Self {
43 Self {
44 terms: vec![ObjectiveTerm::NetworkGeneratorCost],
45 }
46 }
47
48 #[must_use]
50 pub fn active_power_dispatch_cost() -> Self {
51 Self {
52 terms: vec![ObjectiveTerm::ActivePowerDispatchCost],
53 }
54 }
55
56 #[must_use]
58 pub fn with_term(mut self, term: ObjectiveTerm) -> Self {
59 self.terms.push(term);
60 self
61 }
62
63 #[must_use]
65 pub fn terms(&self) -> &[ObjectiveTerm] {
66 &self.terms
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn an_objective_states_its_terms_in_order() {
76 let objective =
77 Objective::network_generator_cost().with_term(ObjectiveTerm::ActivePowerDispatchCost);
78 assert_eq!(objective.terms().len(), 2);
79 assert_eq!(objective.terms()[0], ObjectiveTerm::NetworkGeneratorCost);
80 let serialized = serde_json::to_value(&objective).unwrap();
81 assert_eq!(serialized["terms"][1]["term"], "active_power_dispatch_cost");
82 }
83}