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
//! Functions for creating tree-like constraint systems.

use std::{fmt::Debug, sync::Arc};

use super::factory::make_cs;
use crate::{
    macros::{RawConstraint, RawMethod},
    model::ConstraintSystem,
};

/// Root has two constraints, one to each of its children.
pub fn singleoutput_singleway<T>(n_components: usize, n_variables: usize) -> ConstraintSystem<T>
where
    T: Debug + Clone + Default + Send + 'static,
{
    let make_constraints: fn(&[String], &[String]) -> Vec<RawConstraint<T>> =
        |constraint_names, variable_names| {
            let n_variables = variable_names.len();
            let apply = Arc::new(Ok);
            let mut constraints = Vec::new();
            for current in 0..n_variables {
                let current_name = &variable_names[current];
                let left = 2 * current + 1;
                let right = 2 * current + 2;
                if left >= n_variables || right >= n_variables {
                    break;
                }
                let left_name = &variable_names[left];
                let right_name = &variable_names[right];
                constraints.push(RawConstraint::new(
                    &constraint_names[left],
                    vec![RawMethod::new(
                        "left",
                        vec![current_name.as_str()],
                        vec![left_name.as_str()],
                        apply.clone(),
                    )],
                ));
                constraints.push(RawConstraint::new(
                    &constraint_names[right],
                    vec![RawMethod::new(
                        "right",
                        vec![current_name.as_str()],
                        vec![right_name.as_str()],
                        apply.clone(),
                    )],
                ));
            }
            constraints
        };
    make_cs(n_components, n_variables, make_constraints)
}

/// Root has two constraints, one to each of its children.
/// Each child has a method back to the root.
pub fn singleoutput_multiway<T>(n_components: usize, n_variables: usize) -> ConstraintSystem<T>
where
    T: Debug + Clone + Default + Send + 'static,
{
    let make_constraints: fn(&[String], &[String]) -> Vec<RawConstraint<T>> =
        |constraint_names, variable_names| {
            let n_variables = variable_names.len();
            let apply = Arc::new(Ok);
            let mut constraints = Vec::new();
            for current in 0..n_variables {
                let current_name = &variable_names[current];
                let left = 2 * current + 1;
                let right = 2 * current + 2;
                if left >= n_variables || right >= n_variables {
                    break;
                }
                let left_name = &variable_names[left];
                let right_name = &variable_names[right];
                constraints.push(RawConstraint::new(
                    &constraint_names[left],
                    vec![
                        RawMethod::new(
                            "left1",
                            vec![current_name.as_str()],
                            vec![left_name.as_str()],
                            apply.clone(),
                        ),
                        RawMethod::new(
                            "left2",
                            vec![left_name.as_str()],
                            vec![current_name.as_str()],
                            apply.clone(),
                        ),
                    ],
                ));
                constraints.push(RawConstraint::new(
                    &constraint_names[right],
                    vec![
                        RawMethod::new(
                            "right1",
                            vec![current_name.as_str()],
                            vec![right_name.as_str()],
                            apply.clone(),
                        ),
                        RawMethod::new(
                            "right2",
                            vec![right_name.as_str()],
                            vec![current_name.as_str()],
                            apply.clone(),
                        ),
                    ],
                ));
            }
            constraints
        };
    make_cs(n_components, n_variables, make_constraints)
}

/// The root has one constraint with its children, and one method that outputs to both.
pub fn multioutput_singleway<T>(n_components: usize, n_variables: usize) -> ConstraintSystem<T>
where
    T: Debug + Clone + Default + Send + 'static,
{
    let make_constraints: fn(&[String], &[String]) -> Vec<RawConstraint<T>> =
        |constraint_names, variable_names| {
            let n_variables = variable_names.len();
            let apply = Arc::new(|v: Vec<Arc<T>>| {
                let value = (*v[0]).clone();
                Ok(vec![Arc::new(value.clone()), Arc::new(value)])
            });
            let mut constraints = Vec::new();
            for current in 0..n_variables {
                let current_name = &variable_names[current];
                let left = 2 * current + 1;
                let right = 2 * current + 2;
                if left >= n_variables || right >= n_variables {
                    break;
                }
                let left_name = &variable_names[left];
                let right_name = &variable_names[right];
                constraints.push(RawConstraint::new(
                    &constraint_names[current],
                    vec![RawMethod::new(
                        "m",
                        vec![current_name.as_str()],
                        vec![left_name.as_str(), right_name.as_str()],
                        apply.clone(),
                    )],
                ));
            }
            constraints
        };
    make_cs(n_components, n_variables, make_constraints)
}

/// The root has one constraint with its children, one method that outputs to both,
/// and the left child has one that writes to the root and right child.
pub fn multioutput_twoway<T>(n_components: usize, n_variables: usize) -> ConstraintSystem<T>
where
    T: Debug + Clone + Default + Send + 'static,
{
    let make_constraints: fn(&[String], &[String]) -> Vec<RawConstraint<T>> =
        |constraint_names, variable_names| {
            let n_variables = variable_names.len();
            let apply = Arc::new(|v: Vec<Arc<T>>| {
                let value = (*v[0]).clone();
                Ok(vec![Arc::new(value.clone()), Arc::new(value)])
            });
            let mut constraints = Vec::new();
            for current in 0..n_variables {
                let current_name = &variable_names[current];
                let left = 2 * current + 1;
                let right = 2 * current + 2;
                if left >= n_variables || right >= n_variables {
                    break;
                }
                let left_name = &variable_names[left];
                let right_name = &variable_names[right];
                constraints.push(RawConstraint::new(
                    &constraint_names[current],
                    vec![
                        RawMethod::new(
                            "top_to_left_right",
                            vec![current_name.as_str()],
                            vec![left_name.as_str(), right_name.as_str()],
                            apply.clone(),
                        ),
                        RawMethod::new(
                            "left_to_top_right",
                            vec![left_name.as_str()],
                            vec![current_name.as_str(), right_name.as_str()],
                            apply.clone(),
                        ),
                    ],
                ));
            }
            constraints
        };
    make_cs(n_components, n_variables, make_constraints)
}

/// The root has one constraint with its children, there are three methods,
/// each one reads from one of them and writes to the two others.
pub fn multioutput_threeway<T>(n_components: usize, n_variables: usize) -> ConstraintSystem<T>
where
    T: Debug + Clone + Default + Send + 'static,
{
    let make_constraints: fn(&[String], &[String]) -> Vec<RawConstraint<T>> =
        |constraint_names, variable_names| {
            let n_variables = variable_names.len();
            let apply = Arc::new(|v: Vec<Arc<T>>| {
                let value = (*v[0]).clone();
                Ok(vec![Arc::new(value.clone()), Arc::new(value)])
            });
            let mut constraints = Vec::new();
            for current in 0..n_variables {
                let current_name = &variable_names[current];
                let left = 2 * current + 1;
                let right = 2 * current + 2;
                if left >= n_variables || right >= n_variables {
                    break;
                }
                let left_name = &variable_names[left];
                let right_name = &variable_names[right];
                constraints.push(RawConstraint::new(
                    &constraint_names[current],
                    vec![
                        RawMethod::new(
                            "top_to_left_right",
                            vec![current_name.as_str()],
                            vec![left_name.as_str(), right_name.as_str()],
                            apply.clone(),
                        ),
                        RawMethod::new(
                            "left_to_top_right",
                            vec![left_name.as_str()],
                            vec![current_name.as_str(), right_name.as_str()],
                            apply.clone(),
                        ),
                        RawMethod::new(
                            "right_to_top_left",
                            vec![right_name.as_str()],
                            vec![current_name.as_str(), left_name.as_str()],
                            apply.clone(),
                        ),
                    ],
                ));
            }
            constraints
        };
    make_cs(n_components, n_variables, make_constraints)
}

/// The root has one constraint with its children, there are three methods,
/// each one reads from one of them and writes to the two others.
pub fn unprunable<T>(n_components: usize, n_variables: usize) -> ConstraintSystem<T>
where
    T: Debug + Clone + Default + Send + 'static,
{
    let make_constraints: fn(&[String], &[String]) -> Vec<RawConstraint<T>> =
        |constraint_names, variable_names| {
            let n_variables = variable_names.len();
            let apply = Arc::new(|v: Vec<Arc<T>>| {
                let value = (*v[0]).clone();
                Ok(vec![Arc::new(value.clone()), Arc::new(value)])
            });
            let mut constraints = Vec::new();
            for current in 0..n_variables {
                let current_name = &variable_names[current];
                let left = 2 * current + 1;
                let right = 2 * current + 2;
                if left >= n_variables || right >= n_variables {
                    break;
                }
                let left_name = &variable_names[left];
                let right_name = &variable_names[right];
                constraints.push(RawConstraint::new(
                    &constraint_names[current],
                    vec![
                        RawMethod::new(
                            "down_left",
                            vec![current_name.as_str(), right_name.as_str()],
                            vec![left_name.as_str()],
                            apply.clone(),
                        ),
                        RawMethod::new(
                            "down_right",
                            vec![current_name.as_str(), left_name.as_str()],
                            vec![right_name.as_str()],
                            apply.clone(),
                        ),
                    ],
                ));
            }
            constraints
        };
    make_cs(n_components, n_variables, make_constraints)
}

#[cfg(test)]
mod tests {
    extern crate test;
    use super::unprunable;
    use crate::planner::hierarchical_planner;
    use test::Bencher;

    #[bench]
    fn bench_unprunable(b: &mut Bencher) {
        let cs = unprunable::<()>(1, 400);
        let comp = cs.component("0").unwrap();
        b.iter(|| hierarchical_planner(comp));
    }
}