-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
310 lines (247 loc) · 12.1 KB
/
Program.cs
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
309
310
namespace Z3.LinqDemo;
using System;
using System.Diagnostics;
using System.Globalization;
using Z3.Linq;
using Z3.Linq.Examples;
using Z3.Linq.Examples.RiverCrossing;
using Z3.Linq.Examples.Sudoku;
public static class Program
{
private static void Main(string[] args)
{
Console.WriteLine("==== Missionaries & Cannibals using Solve() ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in MissionariesAndCannibals.Create(ctx, 50)
where t.MissionaryAndCannibalCount == 3
where t.SizeBoat == 2
select t;
var sw = Stopwatch.StartNew();
MissionariesAndCannibals? result = theorem.Solve();
sw.Stop();
Console.WriteLine(result);
Console.WriteLine($"Time to solution: {sw.Elapsed.TotalMilliseconds} ms");
Console.WriteLine();
Console.WriteLine("==== Missionaries & Cannibals using Optimize() ====");
Console.WriteLine();
sw = Stopwatch.StartNew();
var minimal = theorem.Optimize(Optimization.Minimize, t => t.Length);
sw.Stop();
Console.WriteLine(minimal);
Console.WriteLine($"Time to optimized solution: {sw.Elapsed.TotalMilliseconds} ms");
Console.WriteLine();
Console.WriteLine("==== Missionaries & Cannibals using orderby clause ====");
Console.WriteLine();
sw = Stopwatch.StartNew();
minimal = (from t in theorem
orderby t.Length
select t).Solve();
sw.Stop();
Console.WriteLine(minimal);
Console.WriteLine($"Time to optimized solution: {sw.Elapsed.TotalMilliseconds} ms");
}
Console.WriteLine();
Console.WriteLine("==== t.x ^ t.y using Anonymous Types ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in ctx.NewTheorem(new { x = default(bool), y = default(bool) })
where t.x ^ t.y
select t;
var result = theorem.Solve();
Console.WriteLine(result);
}
Console.WriteLine();
Console.WriteLine("==== t.x ^ t.y using ValueTuples ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in ctx.NewTheorem<(bool x, bool y)>()
where t.x ^ t.y
select t;
var result = theorem.Solve();
Console.WriteLine(result);
ctx.Dispose();
}
Console.WriteLine();
Console.WriteLine("==== t.x ^ t.y using Custom Theorem using Record type ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in ctx.NewTheorem(new RecordTheorem<bool, bool>())
where t.X ^ t.Y
select t;
var result = theorem.Solve();
Console.WriteLine(result);
}
Console.WriteLine();
Console.WriteLine("==== Bart's example from TechEd Europe 2012 ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in ctx.NewTheorem<Symbols<int, int, int>>()
where t.X1 - t.X2 >= 1
where t.X1 - t.X2 <= 3
where t.X1 == (2 * t.X3) + t.X2
select t;
var result = theorem.Solve();
Console.WriteLine(result);
}
Console.WriteLine();
Console.WriteLine("==== Bart's example from TechEd Europe 2012 using ValueTuples ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in ctx.NewTheorem<(int x, int y, int z)>()
where t.x - t.y >= 1
where t.x - t.y <= 3
where t.x == (2 * t.z) + t.y
select t;
var result = theorem.Solve();
Console.WriteLine(result);
}
Console.WriteLine();
Console.WriteLine("==== Example using Symbols<T1, T2> ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in ctx.NewTheorem<Symbols<int, int>>()
where t.X1 < t.X2 + 1
where t.X1 > 2
where t.X1 != t.X2
select t;
var result = theorem.Solve();
Console.WriteLine(result);
}
Console.WriteLine();
Console.WriteLine("==== SudokuTheorem Example ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in SudokuTheorem.Create(ctx)
where t.Cell13 == 2 && t.Cell16 == 1 && t.Cell18 == 6
where t.Cell23 == 7 && t.Cell26 == 4
where t.Cell31 == 5 && t.Cell37 == 9
where t.Cell42 == 1 && t.Cell44 == 3
where t.Cell51 == 8 && t.Cell55 == 5 && t.Cell59 == 4
where t.Cell66 == 6 && t.Cell68 == 2
where t.Cell73 == 6 && t.Cell79 == 7
where t.Cell84 == 8 && t.Cell87 == 3
where t.Cell92 == 4 && t.Cell94 == 9 && t.Cell97 == 2
select t;
var result = theorem.Solve();
Console.WriteLine(result);
}
Console.WriteLine("==== SudokuTheorem Example from https://sandiway.arizona.edu/sudoku/examples.html ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem = from t in SudokuTheorem.Create(ctx)
where t.Cell14 == 2 && t.Cell15 == 6 && t.Cell17 == 7 && t.Cell19 == 1
where t.Cell21 == 6 && t.Cell22 == 8 && t.Cell25 == 7 && t.Cell28 == 9
where t.Cell31 == 1 && t.Cell32 == 9 && t.Cell36 == 4 && t.Cell37 == 5
where t.Cell41 == 8 && t.Cell42 == 2 && t.Cell44 == 1 && t.Cell48 == 4
where t.Cell53 == 4 && t.Cell54 == 6 && t.Cell56 == 2 && t.Cell57 == 9
where t.Cell62 == 5 && t.Cell66 == 3 && t.Cell68 == 2 && t.Cell69 == 8
where t.Cell73 == 9 && t.Cell74 == 3 && t.Cell78 == 7 && t.Cell79 == 4
where t.Cell82 == 4 && t.Cell85 == 5 && t.Cell88 == 3 && t.Cell89 == 6
where t.Cell91 == 7 && t.Cell93 == 3 && t.Cell95 == 1 && t.Cell96 == 8
select t;
var result = theorem.Solve();
Console.WriteLine(result);
}
Console.WriteLine("==== Oil Purchase Problem. ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var solveable = from t in ctx.NewTheorem<(double vz, double sa)>()
where 0.3 * t.sa + 0.4 * t.vz >= 1900
where 0.4 * t.sa + 0.2 * t.vz >= 1500
where 0.2 * t.sa + 0.3 * t.vz >= 500
where 0 <= t.sa && t.sa <= 9000
where 0 <= t.vz && t.vz <= 6000
orderby 20.0 * t.sa + 15.0 * t.vz
select t;
var result = solveable.Solve();
Console.WriteLine(string.Create(CultureInfo.CreateSpecificCulture("en-US"), $"Saudi Arabia: {result.sa} barrels ({(result.sa * 20):C}), Venezuela: {result.vz} barrels ({(result.vz * 15):C})"));
}
Console.WriteLine();
Console.WriteLine("==== Warehouse Logistics Problem ====");
Console.WriteLine();
using (var ctx = new Z3Context())
{
var theorem =
from t in ctx.NewTheorem<(double w1c1, double w1c2, double w1c3, double w1c4, double w2c1, double w2c2, double w2c3, double w2c4)>()
where t.w1c1 + t.w1c2 + t.w1c3 + t.w1c4 <= 60_000 // Warehouse 1 Product Availability
where t.w2c1 + t.w2c2 + t.w2c3 + t.w2c4 <= 80_000 // Warehouse 2 Product Availability
where t.w1c1 + t.w2c1 == 35_000 && (t.w1c1 >= 0 && t.w2c1 >= 0) // Customer 1 Orders
where t.w1c2 + t.w2c2 == 22_000 && (t.w1c2 >= 0 && t.w2c2 >= 0) // Customer 2 Orders
where t.w1c3 + t.w2c3 == 18_000 && (t.w1c3 >= 0 && t.w2c3 >= 0) // Customer 3 Orders
where t.w1c4 + t.w2c4 == 30_000 && (t.w1c4 >= 0 && t.w2c4 >= 0) // Customer 4 Orders
orderby (1.00 * t.w1c1) + (3.00 * t.w1c2) + (0.50 * t.w1c3) + (4.00 * t.w1c4) +
(2.50 * t.w2c1) + (5.00 * t.w2c2) + (1.50 * t.w2c3) + (2.50 * t.w2c4) // Optimize for Total Shipping Cost
select t;
var result = theorem.Solve();
Console.WriteLine($"| | Customer 1 | Customer 2 | Customer 3 | Customer 4 |");
Console.WriteLine($"|---------------------|------------|-------------|------------|------------|");
Console.WriteLine($"| Warehouse 1 | {result.w1c1} | {result.w1c2} | {result.w1c3} | {result.w1c4} |");
Console.WriteLine($"| Warehouse 2 | {result.w2c1} | {result.w2c2} | {result.w2c3} | {result.w2c4} |");
Console.WriteLine();
Console.WriteLine(string.Create(CultureInfo.CreateSpecificCulture("en-US"), $"Total Cost: {1.00 * result.w1c1 + 3.00 * result.w1c2 + 0.50 * result.w1c3 + 4.00 * result.w1c4 + 2.50 * result.w2c1 + 5.00 * result.w2c2 + 1.50 * result.w2c3 + 2.50 * result.w2c4:C}"));
}
// AllSamplesWithLogging();
Console.ReadKey();
}
private static void AllSamplesWithLogging()
{
using (var ctx = new Z3Context())
{
ctx.Log = Console.Out; // see internal logging
Solve(from t in ctx.NewTheorem(new { x = default(bool) })
where t.x && !t.x
select t);
Solve(from t in ctx.NewTheorem(new { x = default(bool), y = default(bool) })
where t.x ^ t.y
select t);
Solve(from t in ctx.NewTheorem(new { x = default(int), y = default(int) })
where t.x < t.y + 1
where t.x > 2
select t);
Solve(from t in ctx.NewTheorem<Symbols<int, int>>()
where t.X1 < t.X2 + 1
where t.X1 > 2
where t.X1 != t.X2
select t);
Solve(from t in ctx.NewTheorem<Symbols<int, int, int, int, int>>()
where t.X1 - t.X2 >= 1
where t.X1 - t.X2 <= 3
where t.X1 == (2 * t.X3) + t.X5
where t.X3 == t.X5
where t.X2 == 6 * t.X4
select t);
Solve(from t in ctx.NewTheorem<Symbols<int, int>>()
where Z3Methods.Distinct(t.X1, t.X2)
select t);
Solve(from t in SudokuTheorem.Create(ctx)
where t.Cell13 == 2 && t.Cell16 == 1 && t.Cell18 == 6
where t.Cell23 == 7 && t.Cell26 == 4
where t.Cell31 == 5 && t.Cell37 == 9
where t.Cell42 == 1 && t.Cell44 == 3
where t.Cell51 == 8 && t.Cell55 == 5 && t.Cell59 == 4
where t.Cell66 == 6 && t.Cell68 == 2
where t.Cell73 == 6 && t.Cell79 == 7
where t.Cell84 == 8 && t.Cell87 == 3
where t.Cell92 == 4 && t.Cell94 == 9 && t.Cell97 == 2
select t);
}
}
private static void Solve<T>(Theorem<T> t) where T : class
{
Console.WriteLine(t);
var res = t.Solve();
Console.WriteLine(res == null ? "none" : res.ToString());
Console.WriteLine();
}
}