forked from TimelyDataflow/differential-dataflow
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprojekt.rs
177 lines (148 loc) · 5.79 KB
/
projekt.rs
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
extern crate rand;
extern crate timely;
extern crate differential_dataflow;
use timely::dataflow::operators::probe::Handle;
use differential_dataflow::input::InputSession;
use differential_dataflow::operators::*;
fn main() {
// define a new computational scope, in which to run BFS
timely::execute_from_args(std::env::args(), move |worker| {
// An input for (x,y,z) placements.
let mut xyzs = InputSession::<_,_,isize>::new();
// Inputs for (x,y) and (x,z) goals.
let mut xy_goal = InputSession::new();
let mut xz_goal = InputSession::new();
let mut probe = Handle::new();
// Dataflow to validate input against goals.
worker.dataflow(|scope| {
// Introduce inputs to the scope.
let xyzs = xyzs.to_collection(scope);
let xy_goal = xy_goal.to_collection(scope);
let xz_goal = xz_goal.to_collection(scope);
// Report unmet XY goals, and met XY non-goals.
let xy_errors =
xyzs.map(|(x,y,_)| (x,y))
.distinct()
.negate()
.concat(&xy_goal)
.consolidate();
// Report unmet XZ goals, and met XZ non-goals.
let xz_errors =
xyzs.map(|(x,_,z)| (x,z))
.distinct()
.negate()
.concat(&xz_goal)
.consolidate();
let xy_total = xy_errors.distinct().map(|_| ());
let xz_total = xz_errors.distinct().map(|_| ());
xy_total
.concat(&xz_total)
.distinct()
.inspect(|x| println!("Not done: {:?}", x))
.probe_with(&mut probe);
});
// Dataflow to produce maximum inputs.
worker.dataflow(|scope| {
// Introduce goals to the scope.
let xy_goal = xy_goal.to_collection(scope);
let xz_goal = xz_goal.to_collection(scope);
let xy_xs = xy_goal.map(|(x,_)| (x,()));
let xz_xs = xz_goal.map(|(x,_)| (x,()));
xy_xs.join(&xz_xs)
.map(|_| ())
.consolidate()
.inspect(|x| println!("Maximum solution size: {}", x.2))
.probe_with(&mut probe);
// // For each x, produce valid pairs of y and z.
// xy_goal
// .join(&xz_goal)
// .map(|(x,(y,z))| (x,y,z))
// .inspect(|x| println!("Maximum solution: {:?}", x))
// .probe_with(&mut probe);
});
// Dataflow to produce minimum inputs.
worker.dataflow(|scope| {
// Introduce goals to the scope.
let xy_goal = xy_goal.to_collection(scope);
let xz_goal = xz_goal.to_collection(scope);
let xy_xs = xy_goal.map(|(x,_)| x).count();
let xz_xs = xz_goal.map(|(x,_)| x).count();
xy_xs.join(&xz_xs)
.explode(|(_,(ys,zs))| Some(((), ::std::cmp::max(ys,zs))))
.consolidate()
.inspect(|x| println!("Minimum solution size: {}", x.2))
.probe_with(&mut probe);
// // Produce pairs (x, ys) and (x, zs).
// let xy_xs = xy_goal.group(|_x,ys,out|
// out.push((ys.iter().map(|(&y,_)| y).collect::<Vec<_>>(), 1))
// );
// let xz_xs = xz_goal.group(|_x,zs,out|
// out.push((zs.iter().map(|(&z,_)| z).collect::<Vec<_>>(), 1))
// );
// xy_xs.join(&xz_xs)
// .flat_map(|(x,(ys, zs))| {
// let max = ::std::cmp::max(ys.len(), zs.len());
// let ys = ys.into_iter().cycle();
// let zs = zs.into_iter().cycle();
// ys.zip(zs).take(max).map(move |(y,z)| (x,y,z))
// })
// .inspect(|x| println!("Minimum solution: {:?}", x))
// .probe_with(&mut probe);
});
// Introduce XY projektion.
xy_goal.insert((0, 0));
xy_goal.insert((0, 1));
xy_goal.insert((0, 3));
xy_goal.insert((0, 4));
xy_goal.insert((1, 1));
xy_goal.insert((1, 3));
xy_goal.insert((2, 1));
xy_goal.insert((2, 2));
xy_goal.insert((3, 2));
xy_goal.insert((3, 3));
xy_goal.insert((3, 4));
xy_goal.insert((4, 0));
xy_goal.insert((4, 1));
xy_goal.insert((4, 2));
// Introduce XZ projektion.
xz_goal.insert((0, 2));
xz_goal.insert((0, 3));
xz_goal.insert((0, 4));
xz_goal.insert((1, 2));
xz_goal.insert((1, 4));
xz_goal.insert((2, 1));
xz_goal.insert((2, 2));
xz_goal.insert((2, 3));
xz_goal.insert((3, 0));
xz_goal.insert((3, 1));
xz_goal.insert((3, 3));
xz_goal.insert((3, 4));
xz_goal.insert((4, 1));
xz_goal.insert((4, 4));
// Advance one round.
xyzs.advance_to(1); xyzs.flush();
xy_goal.advance_to(1); xy_goal.flush();
xz_goal.advance_to(1); xz_goal.flush();
// Introduce candidate solution.
xyzs.insert((0, 0, 2));
xyzs.insert((0, 1, 3));
xyzs.insert((0, 3, 4));
xyzs.insert((0, 4, 4));
xyzs.insert((1, 1, 2));
xyzs.insert((1, 3, 4));
xyzs.insert((2, 1, 1));
xyzs.insert((2, 2, 2));
xyzs.insert((2, 2, 3));
xyzs.insert((3, 2, 0));
xyzs.insert((3, 3, 1));
xyzs.insert((3, 4, 3));
xyzs.insert((3, 4, 4));
xyzs.insert((4, 0, 1));
xyzs.insert((4, 1, 4));
xyzs.insert((4, 2, 4));
// Advance another round.
xyzs.advance_to(2); xyzs.flush();
xy_goal.advance_to(2); xy_goal.flush();
xz_goal.advance_to(2); xz_goal.flush();
}).unwrap();
}