/examples/tests/testcp.cs

http://or-tools.googlecode.com/ · C# · 460 lines · 402 code · 15 blank · 43 comment · 106 complexity · cff2b096466ad4f5ede5973b8e86284e MD5 · raw file

  1. // Copyright 2010-2012 Google
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. using System;
  14. using Google.OrTools.ConstraintSolver;
  15. public class CsTestCpOperator
  16. {
  17. static void Check(bool test, String message)
  18. {
  19. if (!test)
  20. {
  21. Console.WriteLine("Error: " + message);
  22. }
  23. }
  24. static void CheckEquality(double v1, double v2, String message)
  25. {
  26. if (v1 != v2)
  27. {
  28. Console.WriteLine("Error: " + v1 + " != " + v2 + " " + message);
  29. }
  30. }
  31. static void TestConstructors()
  32. {
  33. Console.WriteLine("TestConstructors");
  34. Solver solver = new Solver("test");
  35. IntVar x = solver.MakeIntVar(0, 10, "x");
  36. Constraint c1 = x == 2;
  37. Console.WriteLine(c1.ToString());
  38. Constraint c2 = x >= 2;
  39. Console.WriteLine(c2.ToString());
  40. Constraint c3 = x > 2;
  41. Console.WriteLine(c3.ToString());
  42. Constraint c4 = x <= 2;
  43. Console.WriteLine(c4.ToString());
  44. Constraint c5 = x < 2;
  45. Console.WriteLine(c5.ToString());
  46. Constraint c6 = x != 2;
  47. Console.WriteLine(c6.ToString());
  48. }
  49. static void TestConstraintWithExpr()
  50. {
  51. Console.WriteLine("TestConstraintWithExpr");
  52. Solver solver = new Solver("test");
  53. IntVar x = solver.MakeIntVar(0, 10, "x");
  54. IntVar y = solver.MakeIntVar(0, 10, "y");
  55. Constraint c1 = x == 2;
  56. Constraint c2 = y == 3;
  57. IntExpr e2a = c1 + 1;
  58. Console.WriteLine(e2a.ToString());
  59. IntExpr e2b = 1 + c1;
  60. Console.WriteLine(e2b.ToString());
  61. IntExpr e2c = c1 - 1;
  62. Console.WriteLine(e2c.ToString());
  63. IntExpr e2d = 1 - c1;
  64. Console.WriteLine(e2d.ToString());
  65. IntExpr e2e = c1 * 2;
  66. Console.WriteLine(e2e.ToString());
  67. IntExpr e2f = 2 * c1;
  68. Console.WriteLine(e2f.ToString());
  69. IntExpr e3a = c1 + y;
  70. Console.WriteLine(e3a.ToString());
  71. IntExpr e3b = y + c1;
  72. Console.WriteLine(e3b.ToString());
  73. IntExpr e3c = c1 - y;
  74. Console.WriteLine(e3c.ToString());
  75. IntExpr e3d = y - c1;
  76. Console.WriteLine(e3d.ToString());
  77. IntExpr e3e = c1 * y;
  78. Console.WriteLine(e3e.ToString());
  79. IntExpr e3f = y * c1;
  80. Console.WriteLine(e3f.ToString());
  81. IntExpr e4 = -c1;
  82. Console.WriteLine(e4.ToString());
  83. IntExpr e5 = c1 / 4;
  84. Console.WriteLine(e5.ToString());
  85. IntExpr e6 = c1.Abs();
  86. Console.WriteLine(e6.ToString());
  87. IntExpr e7 = c1.Square();
  88. Console.WriteLine(e7.ToString());
  89. Constraint c8a = c1 == 1;
  90. Console.WriteLine(c8a.ToString());
  91. Constraint c8b = 1 == c1;
  92. Console.WriteLine(c8b.ToString());
  93. Constraint c8c = c1 != 1;
  94. Console.WriteLine(c8c.ToString());
  95. Constraint c8d = 1 != c1;
  96. Console.WriteLine(c8d.ToString());
  97. Constraint c8e = c1 >= 1;
  98. Console.WriteLine(c8e.ToString());
  99. Constraint c8f = 1 >= c1;
  100. Console.WriteLine(c8f.ToString());
  101. Constraint c8g = c1 > 1;
  102. Console.WriteLine(c8g.ToString());
  103. Constraint c8h = 1 > c1;
  104. Console.WriteLine(c8h.ToString());
  105. Constraint c8i = c1 <= 1;
  106. Console.WriteLine(c8i.ToString());
  107. Constraint c8j = 1 <= c1;
  108. Console.WriteLine(c8j.ToString());
  109. Constraint c8k = c1 < 1;
  110. Console.WriteLine(c8k.ToString());
  111. Constraint c8l = 1 < c1;
  112. Console.WriteLine(c8l.ToString());
  113. Constraint c9a = c1 == y;
  114. Console.WriteLine(c9a.ToString());
  115. Constraint c9b = y == c1;
  116. Console.WriteLine(c9b.ToString());
  117. Constraint c9c = c1 != y;
  118. Console.WriteLine(c9c.ToString());
  119. Constraint c9d = y != c1;
  120. Console.WriteLine(c9d.ToString());
  121. Constraint c9e = c1 >= y;
  122. Console.WriteLine(c9e.ToString());
  123. Constraint c9f = y >= c1;
  124. Console.WriteLine(c9f.ToString());
  125. Constraint c9g = c1 > y;
  126. Console.WriteLine(c9g.ToString());
  127. Constraint c9h = y > c1;
  128. Console.WriteLine(c9h.ToString());
  129. Constraint c9i = c1 <= y;
  130. Console.WriteLine(c9i.ToString());
  131. Constraint c9j = y <= c1;
  132. Console.WriteLine(c9j.ToString());
  133. Constraint c9k = c1 < y;
  134. Console.WriteLine(c9k.ToString());
  135. Constraint c9l = y < c1;
  136. Console.WriteLine(c9l.ToString());
  137. Constraint c10a = c1 == c2;
  138. Console.WriteLine(c10a.ToString());
  139. Constraint c10c = c1 != c2;
  140. Console.WriteLine(c10c.ToString());
  141. Constraint c10e = c1 >= c2;
  142. Console.WriteLine(c10e.ToString());
  143. Constraint c10g = c1 > c2;
  144. Console.WriteLine(c10g.ToString());
  145. Constraint c10i = c1 <= c2;
  146. Console.WriteLine(c10i.ToString());
  147. Constraint c10k = c1 < c2;
  148. Console.WriteLine(c10k.ToString());
  149. IntExpr e11a = c1 + (y == 2);
  150. Console.WriteLine(e11a.ToString());
  151. IntExpr e11b = (y == 2) + c1;
  152. Console.WriteLine(e11b.ToString());
  153. IntExpr e11c = c1 - (y == 2);
  154. Console.WriteLine(e11c.ToString());
  155. IntExpr e11d = (y == 2) - c1;
  156. Console.WriteLine(e11d.ToString());
  157. IntExpr e11e = c1 * (y == 2);
  158. Console.WriteLine(e11e.ToString());
  159. IntExpr e11f = (y == 2) * c1;
  160. Console.WriteLine(e11f.ToString());
  161. Constraint c12a = c1 == (y == 2);
  162. Console.WriteLine(c12a.ToString());
  163. Constraint c12b = (y == 2) == c1;
  164. Console.WriteLine(c12b.ToString());
  165. Constraint c12c = c1 != (y == 2);
  166. Console.WriteLine(c12c.ToString());
  167. Constraint c12d = (y == 2) != c1;
  168. Console.WriteLine(c12d.ToString());
  169. Constraint c12e = c1 >= (y == 2);
  170. Console.WriteLine(c12e.ToString());
  171. Constraint c12f = (y == 2) >= c1;
  172. Console.WriteLine(c12f.ToString());
  173. Constraint c12g = c1 > (y == 2);
  174. Console.WriteLine(c12g.ToString());
  175. Constraint c12h = (y == 2) > c1;
  176. Console.WriteLine(c12h.ToString());
  177. Constraint c12i = c1 <= (y == 2);
  178. Console.WriteLine(c12i.ToString());
  179. Constraint c12j = (y == 2) <= c1;
  180. Console.WriteLine(c12j.ToString());
  181. Constraint c12k = c1 < (y == 2);
  182. Console.WriteLine(c12k.ToString());
  183. Constraint c12l = (y == 2) < c1;
  184. Console.WriteLine(c12l.ToString());
  185. }
  186. static void TestWrappedConstraintWithExpr()
  187. {
  188. Console.WriteLine("TestWrappedConstraintWithExpr");
  189. Solver solver = new Solver("test");
  190. IntVar x = solver.MakeIntVar(0, 10, "x");
  191. IntVar y = solver.MakeIntVar(0, 10, "y");
  192. IntExpr e2a = (x == 1) + 1;
  193. Console.WriteLine(e2a.ToString());
  194. IntExpr e2b = 1 + (x == 1);
  195. Console.WriteLine(e2b.ToString());
  196. IntExpr e2c = (x == 1) - 1;
  197. Console.WriteLine(e2c.ToString());
  198. IntExpr e2d = 1 - (x == 1);
  199. Console.WriteLine(e2d.ToString());
  200. IntExpr e2e = (x == 1) * 2;
  201. Console.WriteLine(e2e.ToString());
  202. IntExpr e2f = 2 * (x == 1);
  203. Console.WriteLine(e2f.ToString());
  204. IntExpr e3a = (x == 1) + y;
  205. Console.WriteLine(e3a.ToString());
  206. IntExpr e3b = y + (x == 1);
  207. Console.WriteLine(e3b.ToString());
  208. IntExpr e3c = (x == 1) - y;
  209. Console.WriteLine(e3c.ToString());
  210. IntExpr e3d = y - (x == 1);
  211. Console.WriteLine(e3d.ToString());
  212. IntExpr e3e = (x == 1) * y;
  213. Console.WriteLine(e3e.ToString());
  214. IntExpr e3f = y * (x == 1);
  215. Console.WriteLine(e3f.ToString());
  216. IntExpr e4 = -(x == 1);
  217. Console.WriteLine(e4.ToString());
  218. IntExpr e5 = (x == 1) / 4;
  219. Console.WriteLine(e5.ToString());
  220. IntExpr e6 = (x == 1).Abs();
  221. Console.WriteLine(e6.ToString());
  222. IntExpr e7 = (x == 1).Square();
  223. Console.WriteLine(e7.ToString());
  224. Constraint c8a = (x == 1) == 1;
  225. Console.WriteLine(c8a.ToString());
  226. Constraint c8b = 1 == (x == 1);
  227. Console.WriteLine(c8b.ToString());
  228. Constraint c8c = (x == 1) != 1;
  229. Console.WriteLine(c8c.ToString());
  230. Constraint c8d = 1 != (x == 1);
  231. Console.WriteLine(c8d.ToString());
  232. Constraint c8e = (x == 1) >= 1;
  233. Console.WriteLine(c8e.ToString());
  234. Constraint c8f = 1 >= (x == 1);
  235. Console.WriteLine(c8f.ToString());
  236. Constraint c8g = (x == 1) > 1;
  237. Console.WriteLine(c8g.ToString());
  238. Constraint c8h = 1 > (x == 1);
  239. Console.WriteLine(c8h.ToString());
  240. Constraint c8i = (x == 1) <= 1;
  241. Console.WriteLine(c8i.ToString());
  242. Constraint c8j = 1 <= (x == 1);
  243. Console.WriteLine(c8j.ToString());
  244. Constraint c8k = (x == 1) < 1;
  245. Console.WriteLine(c8k.ToString());
  246. Constraint c8l = 1 < (x == 1);
  247. Console.WriteLine(c8l.ToString());
  248. Constraint c9a = (x == 1) == y;
  249. Console.WriteLine(c9a.ToString());
  250. Constraint c9b = y == (x == 1);
  251. Console.WriteLine(c9b.ToString());
  252. Constraint c9c = (x == 1) != y;
  253. Console.WriteLine(c9c.ToString());
  254. Constraint c9d = y != (x == 1);
  255. Console.WriteLine(c9d.ToString());
  256. Constraint c9e = (x == 1) >= y;
  257. Console.WriteLine(c9e.ToString());
  258. Constraint c9f = y >= (x == 1);
  259. Console.WriteLine(c9f.ToString());
  260. Constraint c9g = (x == 1) > y;
  261. Console.WriteLine(c9g.ToString());
  262. Constraint c9h = y > (x == 1);
  263. Console.WriteLine(c9h.ToString());
  264. Constraint c9i = (x == 1) <= y;
  265. Console.WriteLine(c9i.ToString());
  266. Constraint c9j = y <= (x == 1);
  267. Console.WriteLine(c9j.ToString());
  268. Constraint c9k = (x == 1) < y;
  269. Console.WriteLine(c9k.ToString());
  270. Constraint c9l = y < (x == 1);
  271. Console.WriteLine(c9l.ToString());
  272. Constraint c10a = (x == 1) == (y == 2);
  273. Console.WriteLine(c10a.ToString());
  274. Constraint c10c = (x == 1) != (y == 2);
  275. Console.WriteLine(c10c.ToString());
  276. Constraint c10e = (x == 1) >= (y == 2);
  277. Console.WriteLine(c10e.ToString());
  278. Constraint c10g = (x == 1) > (y == 2);
  279. Console.WriteLine(c10g.ToString());
  280. Constraint c10i = (x == 1) <= (y == 2);
  281. Console.WriteLine(c10i.ToString());
  282. Constraint c10k = (x == 1) < (y == 2);
  283. Console.WriteLine(c10k.ToString());
  284. }
  285. static void TestBaseEqualityWithExpr()
  286. {
  287. Console.WriteLine("TestBaseEqualityWithExpr");
  288. Solver solver = new Solver("test");
  289. IntVar x = solver.MakeIntVar(0, 10, "x");
  290. IntVar y = solver.MakeIntVar(0, 10, "y");
  291. IntExpr e2a = (x >= 1) + 1;
  292. Console.WriteLine(e2a.ToString());
  293. IntExpr e2b = 1 + (x >= 1);
  294. Console.WriteLine(e2b.ToString());
  295. IntExpr e2c = (x >= 1) - 1;
  296. Console.WriteLine(e2c.ToString());
  297. IntExpr e2d = 1 - (x >= 1);
  298. Console.WriteLine(e2d.ToString());
  299. IntExpr e2e = (x >= 1) * 2;
  300. Console.WriteLine(e2e.ToString());
  301. IntExpr e2f = 2 * (x >= 1);
  302. Console.WriteLine(e2f.ToString());
  303. IntExpr e3a = (x >= 1) + y;
  304. Console.WriteLine(e3a.ToString());
  305. IntExpr e3b = y + (x >= 1);
  306. Console.WriteLine(e3b.ToString());
  307. IntExpr e3c = (x >= 1) - y;
  308. Console.WriteLine(e3c.ToString());
  309. IntExpr e3d = y - (x >= 1);
  310. Console.WriteLine(e3d.ToString());
  311. IntExpr e3e = (x >= 1) * y;
  312. Console.WriteLine(e3e.ToString());
  313. IntExpr e3f = y * (x >= 1);
  314. Console.WriteLine(e3f.ToString());
  315. IntExpr e4 = -(x >= 1);
  316. Console.WriteLine(e4.ToString());
  317. IntExpr e5 = (x >= 1) / 4;
  318. Console.WriteLine(e5.ToString());
  319. IntExpr e6 = (x >= 1).Abs();
  320. Console.WriteLine(e6.ToString());
  321. IntExpr e7 = (x >= 1).Square();
  322. Console.WriteLine(e7.ToString());
  323. Constraint c8a = (x >= 1) >= 1;
  324. Console.WriteLine(c8a.ToString());
  325. Constraint c8b = 1 >= (x >= 1);
  326. Console.WriteLine(c8b.ToString());
  327. Constraint c8c = (x >= 1) != 1;
  328. Console.WriteLine(c8c.ToString());
  329. Constraint c8d = 1 != (x >= 1);
  330. Console.WriteLine(c8d.ToString());
  331. Constraint c8e = (x >= 1) >= 1;
  332. Console.WriteLine(c8e.ToString());
  333. Constraint c8f = 1 >= (x >= 1);
  334. Console.WriteLine(c8f.ToString());
  335. Constraint c8g = (x >= 1) > 1;
  336. Console.WriteLine(c8g.ToString());
  337. Constraint c8h = 1 > (x >= 1);
  338. Console.WriteLine(c8h.ToString());
  339. Constraint c8i = (x >= 1) <= 1;
  340. Console.WriteLine(c8i.ToString());
  341. Constraint c8j = 1 <= (x >= 1);
  342. Console.WriteLine(c8j.ToString());
  343. Constraint c8k = (x >= 1) < 1;
  344. Console.WriteLine(c8k.ToString());
  345. Constraint c8l = 1 < (x >= 1);
  346. Console.WriteLine(c8l.ToString());
  347. Constraint c9a = (x >= 1) >= y;
  348. Console.WriteLine(c9a.ToString());
  349. Constraint c9b = y >= (x >= 1);
  350. Console.WriteLine(c9b.ToString());
  351. Constraint c9c = (x >= 1) != y;
  352. Console.WriteLine(c9c.ToString());
  353. Constraint c9d = y != (x >= 1);
  354. Console.WriteLine(c9d.ToString());
  355. Constraint c9e = (x >= 1) >= y;
  356. Console.WriteLine(c9e.ToString());
  357. Constraint c9f = y >= (x >= 1);
  358. Console.WriteLine(c9f.ToString());
  359. Constraint c9g = (x >= 1) > y;
  360. Console.WriteLine(c9g.ToString());
  361. Constraint c9h = y > (x >= 1);
  362. Console.WriteLine(c9h.ToString());
  363. Constraint c9i = (x >= 1) <= y;
  364. Console.WriteLine(c9i.ToString());
  365. Constraint c9j = y <= (x >= 1);
  366. Console.WriteLine(c9j.ToString());
  367. Constraint c9k = (x >= 1) < y;
  368. Console.WriteLine(c9k.ToString());
  369. Constraint c9l = y < (x >= 1);
  370. Console.WriteLine(c9l.ToString());
  371. Constraint c10a = (x >= 1) >= (y >= 2);
  372. Console.WriteLine(c10a.ToString());
  373. Constraint c10c = (x >= 1) != (y >= 2);
  374. Console.WriteLine(c10c.ToString());
  375. Constraint c10e = (x >= 1) >= (y >= 2);
  376. Console.WriteLine(c10e.ToString());
  377. Constraint c10g = (x >= 1) > (y >= 2);
  378. Console.WriteLine(c10g.ToString());
  379. Constraint c10i = (x >= 1) <= (y >= 2);
  380. Console.WriteLine(c10i.ToString());
  381. Constraint c10k = (x >= 1) < (y >= 2);
  382. Console.WriteLine(c10k.ToString());
  383. }
  384. static void TestDowncast()
  385. {
  386. Solver solver = new Solver("TestDowncast");
  387. IntVar x = solver.MakeIntVar(0, 10, "x");
  388. IntExpr e = x + 1;
  389. IntVar y = e.Var();
  390. Console.WriteLine(y.ToString());
  391. }
  392. static void TestSequence()
  393. {
  394. Solver solver = new Solver("TestSequence");
  395. IntervalVar[] intervals =
  396. solver.MakeFixedDurationIntervalVarArray(10, 0, 10, 5, false, "task");
  397. DisjunctiveConstraint disjunctive = intervals.Disjunctive("Sequence");
  398. SequenceVar var = disjunctive.SequenceVar();
  399. Assignment ass = solver.MakeAssignment();
  400. ass.Add(var);
  401. ass.SetForwardSequence(var, new int[] { 1, 3, 5 });
  402. int[] seq = ass.ForwardSequence(var);
  403. Console.WriteLine(seq.Length);
  404. }
  405. // static void TestScheduling()
  406. // {
  407. // Solver solver = new Solver("Scheduling");
  408. // IntervalVar[] tasks = new IntervalVar[taskCount];
  409. // List<IntVar> all_ends = new List<IntVar>();
  410. // int i = 0;
  411. // foreach(Task t in myTaskList)
  412. // {
  413. // tasks[i] = solver.MakeFixedInterval(0, (long)t.Duration, t.Name);
  414. // if (t.Successors.Count <= 0)
  415. // all_ends.Add(tasks[i].EndExpr().Var());
  416. // //solver.Add(solver.MakeGreaterOrEqual(tasks[i].StartExpr(), 1)); // { 1 }
  417. // i++;
  418. // }
  419. // IntVar objective_var = solver.MakeMax(all_ends.ToArray()).Var();
  420. // OptimizeVar objective_monitor = solver.MakeMinimize(objective_var, 1);
  421. // DecisionBuilder obj_phase = solver.MakePhase(objective_var, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE);
  422. // const int kLogFrequency = 999999999;
  423. // SearchMonitor search_log = solver.MakeSearchLog(kLogFrequency, objective_monitor);
  424. // SolutionCollector collector = solver.MakeLastSolutionCollector();
  425. // collector.AddObjective(objective_var);
  426. // if (solver.Solve(obj_phase, objective_monitor))
  427. // {
  428. // Console.Out.WriteLine("Solution: ");
  429. // foreach(IntervalVar t in tasks)
  430. // Console.Out.WriteLine(t.ToString());
  431. // }else
  432. // {
  433. // Console.Out.WriteLine("Can not find a solution");
  434. // }
  435. // }
  436. static void Main()
  437. {
  438. TestConstructors();
  439. TestConstraintWithExpr();
  440. TestWrappedConstraintWithExpr();
  441. TestBaseEqualityWithExpr();
  442. TestDowncast();
  443. TestSequence();
  444. }
  445. }