Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. n = 4;
  2. lines = n (n - 1)/2;
  3. optimalelements = n - 1;
  4. gridpoints = 100;
  5. grid = Round[Sqrt[gridpoints]];
  6. fitness[x2_, y2_, x3_, y3_, x4_, y4_] :=
  7. (Clear[fitness, points, linepoints, d, c, penalty, threepoints,
  8. threetest, fourpoints, fourtest, p];
  9. points = {{0, 0}, {x2, y2}, {x3, y3}, {x4, y4}};
  10. linepoints = Subsets[points, {2}];
  11. d = EuclideanDistance @@@ linepoints;
  12. c = Sort[Tally[d][[All, 2]]];
  13. penalty = Total[Abs[Differences[c]]] - Length[c];
  14. If [c == Range[optimalelements], penalty = penalty + 1];
  15. p = penalty);
  16. {sol, pts} = Reap[
  17. NMaximize[{fitness[x2, y2, x3, y3, x4, y4], 0 <= x2 <= grid,
  18. 0 <= y2 <= grid, 0 <= x3 <= grid, 0 <= y3 <= grid,
  19. 0 <= x4 <= grid,
  20. 0 <= y4 <= grid}, {{x2, 0, grid}, {y2, 0, grid}, {x3, 0,
  21. grid}, {y3, 0, grid}, {x4, 0, grid}, {y4, 0, grid}}, Integers,
  22. Method -> {"SimulatedAnnealing", "SearchPoints" -> 1,
  23. "PerturbationScale" -> 1, "RandomSeed" -> 1},
  24. EvaluationMonitor :> Sow[{{x2, y2}, {x3, y3}, {x4, y4}, c, p}]]] //
  25. AbsoluteTiming
  26.  
  27. Out[7] {0.865882, {{-6., {x2 -> 5, y2 -> 6, x3 -> 2, y3 -> 4, x4 -> 7,
  28. y4 -> 4}}, {{{{0, 4}, {0, 4}, {2, 6}, {1, 1, 1, 1, 1,
  29. 1}, -6}, {{0, 6}, {0, 2}, {4, 5}, {1, 1, 1, 1, 1, 1}, -6}, {{2,
  30. 6}, {0, 4}, {4, 8}, {1, 1, 1, 1, 1, 1}, -6}, {{1, 6}, {0,
  31. 2}, {4, 9}, {1, 1, 1, 1, 1, 1}, -6}, {{3, 7}, {1, 4}, {3,
  32. 7}, {1, 1, 1, 1, 1, 1}, -6}, {{1, 9}, {2, 5}, {2, 7}, {1, 1, 1,
  33. 1, 1, 1}, -6}, {{5, 10}, {4, 7}, {0, 4}, {1, 1, 1, 1, 1,
  34. 1}, -6}...etc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement