xcsp3_rust/objectives/xobjectives_set.rs
1/*=============================================================================
2* parser for CSP instances represented in XCSP3 Format
3*
4* Copyright (c) 2023 xcsp.org (contact @ xcsp.org)
5*
6* Permission is hereby granted, free of charge, to any person obtaining a copy
7* of this software and associated documentation files (the "Software"), to deal
8* in the Software without restriction, including without limitation the rights
9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10* copies of the Software, and to permit persons to whom the Software is
11* furnished to do so, subject to the following conditions:
12*
13* The above copyright notice and this permission notice shall be included in
14* all copies or substantial portions of the Software.
15*
16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22* THE SOFTWARE.
23*=============================================================================
24*/
25
26/*
27 * <p>@project_name: xcsp3-rust
28 * </p>
29 * <p>@author: luhan zhen
30 * </p>
31 * <p>@date: 2023/7/23 15:11
32 * </p>
33 * <p>@email: zhenlh20@mails.jlu.edu.cn
34 * </p>
35 * <p>@version: 1.0
36 * </p>
37 * <p>@description:
38 * </p>
39 */
40
41pub mod xcsp3_core {
42
43 use crate::objectives::xobjective_element::xcsp3_core::XObjectiveElement;
44 use crate::objectives::xobjective_expression::xcsp3_core::XObjectiveExpression;
45 use crate::objectives::xobjectives_type::xcsp3_core::{XObjective, XObjectivesType};
46 use crate::variables::xvariable_set::xcsp3_core::XVariableSet;
47 use std::slice::{Iter, IterMut};
48
49 pub struct XObjectivesSet<'a> {
50 objectives: Vec<XObjectivesType<'a>>,
51 set: &'a XVariableSet,
52 }
53
54 impl<'a> XObjectivesSet<'a> {
55 pub fn build_maximize(
56 &mut self,
57 list: &str,
58 coeffs: &str,
59 expression: &str,
60 type_str: &str,
61 ) {
62 // println!("list {} coeffs {} expression {} type_str {}", list, coeffs, expression, type_str);
63 if type_str.is_empty() {
64 match XObjectiveExpression::from_expr(expression, self.set) {
65 Ok(xoe) => {
66 self.objectives.push(XObjectivesType::Maximize(
67 XObjective::XObjectiveExpression(xoe),
68 ));
69 }
70 Err(e) => self.objectives.push(XObjectivesType::XObjectiveNone(e)),
71 }
72 } else {
73 match XObjectiveElement::from_str(
74 if !list.is_empty() { list } else { expression },
75 coeffs,
76 type_str,
77 self.set,
78 ) {
79 Ok(ele) => self.objectives.push(XObjectivesType::Maximize(
80 XObjective::XObjectiveElement(ele),
81 )),
82 Err(e) => self.objectives.push(XObjectivesType::XObjectiveNone(e)),
83 }
84 }
85 }
86
87 pub fn build_minimize(
88 &mut self,
89 list: &str,
90 coeffs: &str,
91 expression: &str,
92 type_str: &str,
93 ) {
94 // println!("list {} coeffs {} expression {} type_str {}", list, coeffs, expression, type_str);
95 if type_str.is_empty() {
96 match XObjectiveExpression::from_expr(expression, self.set) {
97 Ok(xoe) => {
98 self.objectives.push(XObjectivesType::Minimize(
99 XObjective::XObjectiveExpression(xoe),
100 ));
101 }
102 Err(e) => self.objectives.push(XObjectivesType::XObjectiveNone(e)),
103 }
104 } else {
105 match XObjectiveElement::from_str(
106 if !list.is_empty() { list } else { expression },
107 coeffs,
108 type_str,
109 self.set,
110 ) {
111 Ok(ele) => self.objectives.push(XObjectivesType::Minimize(
112 XObjective::XObjectiveElement(ele),
113 )),
114 Err(e) => self.objectives.push(XObjectivesType::XObjectiveNone(e)),
115 }
116 }
117 }
118
119 pub fn iter(&self) -> Iter<'_, XObjectivesType> {
120 self.objectives.iter()
121 }
122 pub fn iter_mut(&mut self) -> IterMut<'_, XObjectivesType<'a>> {
123 self.objectives.iter_mut()
124 }
125 pub fn new(set: &'a XVariableSet) -> Self {
126 Self {
127 objectives: vec![],
128 set,
129 }
130 }
131 }
132}