fork download
  1. # -*- coding: utf-8 -*-
  2. import math
  3. import random
  4. import copy
  5.  
  6. # グローバル変数
  7. MAXVALUE = 100
  8. N = 30
  9. WEIGHTLIMIT = N * MAXVALUE / 4
  10. POOLSIZE = 30
  11. LASTG = 50
  12. MRATE = 0.01
  13. SEED = 32767
  14. parcel = [[0 for i in range(2)] for j in range(N)]
  15.  
  16. # 必要な関数のみ定義
  17. def rndn(n):
  18. return random.randint(0, n - 1)
  19.  
  20. def initparcel():
  21. i = 0
  22. while i < N:
  23. try:
  24. line = input()
  25. if not line: break
  26. except EOFError:
  27. break
  28. parts = line.split()
  29. if len(parts) >= 2:
  30. parcel[i] = [int(parts[0]), int(parts[1])]
  31. i += 1
  32.  
  33. def evalfit(g):
  34. value = 0
  35. weight = 0
  36. for pos in range(N):
  37. weight += parcel[pos][0] * g[pos]
  38. value += parcel[pos][1] * g[pos]
  39. if weight >= WEIGHTLIMIT:
  40. value = 0
  41. return value
  42.  
  43. def selectp(roulette, totalfitness):
  44. acc = 0
  45. ball = rndn(totalfitness)
  46. for i in range(POOLSIZE):
  47. acc += roulette[i]
  48. if acc > ball: break
  49. return i
  50.  
  51. def crossing(m, p, c1, c2):
  52. cp = rndn(N)
  53. for j in range(cp):
  54. c1[j] = m[j]
  55. c2[j] = p[j]
  56. for j in range(cp, N):
  57. c2[j] = m[j]
  58. c1[j] = p[j]
  59.  
  60. def mutation(ngpool):
  61. for i in range(POOLSIZE * 2):
  62. for j in range(N):
  63. if random.random() < MRATE:
  64. ngpool[i][j] = 1 if ngpool[i][j] == 0 else 0
  65.  
  66. def mating(pool, ngpool):
  67. roulette = [evalfit(pool[i]) for i in range(POOLSIZE)]
  68. totalfitness = sum(roulette)
  69. for i in range(POOLSIZE):
  70. while True:
  71. mama = selectp(roulette, totalfitness)
  72. papa = selectp(roulette, totalfitness)
  73. if mama != papa: break
  74. crossing(pool[mama], pool[papa], ngpool[i * 2], ngpool[i * 2 + 1])
  75.  
  76. def selectng(ngpool, pool):
  77. roulette = [evalfit(ngpool[i]) for i in range(POOLSIZE * 2)]
  78. totalfitness = sum(roulette)
  79. for i in range(POOLSIZE):
  80. ball = rndn(totalfitness)
  81. acc = 0
  82. for c in range(POOLSIZE * 2):
  83. acc += roulette[c]
  84. if acc > ball: break
  85. pool[i] = copy.deepcopy(ngpool[c])
  86.  
  87. # 結果出力(グラフ用データのみ)
  88. def print_graph_data(generation, pool):
  89. totalfitness = 0
  90. bestfit = 0
  91. elite = 0
  92. for i in range(POOLSIZE):
  93. fitness = evalfit(pool[i])
  94. if fitness > bestfit:
  95. bestfit = fitness
  96. elite = i
  97. totalfitness += fitness
  98. # 世代, エリート番号, 最良値, 平均値
  99. print(generation, elite, bestfit, totalfitness / POOLSIZE)
  100.  
  101. # メイン実行部
  102. random.seed(SEED)
  103. pool = [[rndn(2) for i in range(N)] for j in range(POOLSIZE)]
  104. ngpool = [[0 for i in range(N)] for j in range(POOLSIZE * 2)]
  105. initparcel()
  106.  
  107. # 0世代目のデータ出力
  108. print_graph_data(0, pool)
  109.  
  110. for generation in range(1, LASTG):
  111. mating(pool, ngpool)
  112. mutation(ngpool)
  113. selectng(ngpool, pool)
  114. print_graph_data(generation, pool)
Success #stdin #stdout 0.25s 14308KB
stdin
65 27
39 82
9 85
72 71
87 91
91 28
34 92
58 79
3 27
12 82
92 15
39 49
83 54
76 43
6 26
77 2
68 6
24 60
60 47
6 40
91 58
44 68
50 33
91 92
57 62
97 49
96 68
39 77
89 6
24 97
stdout
0 16 788 255.7
1 7 883 724.7333333333333
2 9 854 715.0333333333333
3 3 877 689.4333333333333
4 20 877 712.4
5 11 883 741.0333333333333
6 5 877 693.8666666666667
7 9 873 706.1333333333333
8 25 917 755.0666666666667
9 18 925 774.2
10 5 991 802.1666666666666
11 12 1083 819.3666666666667
12 9 1083 856.4333333333333
13 3 1038 836.8333333333334
14 23 1038 853.8333333333334
15 10 1038 821.0333333333333
16 14 986 808.2666666666667
17 0 1053 853.9
18 11 1014 846.4
19 8 985 861.2666666666667
20 19 1109 868.6666666666666
21 6 966 848.6
22 28 966 823.3666666666667
23 12 973 792.4333333333333
24 8 973 834.9333333333333
25 19 1011 866.8333333333334
26 11 999 849.7666666666667
27 3 978 840.5
28 25 1012 853.2666666666667
29 15 1050 857.8
30 25 954 856.1
31 24 995 849.5333333333333
32 1 956 852.9
33 2 956 854.1333333333333
34 29 956 847.0
35 5 994 865.3
36 10 956 860.1333333333333
37 10 932 850.7
38 10 915 808.2333333333333
39 8 919 803.3333333333334
40 22 919 824.4
41 17 988 830.6
42 15 932 829.5333333333333
43 22 938 822.1
44 26 944 818.8
45 18 928 841.5666666666667
46 15 952 832.2666666666667
47 26 958 821.4333333333333
48 12 981 814.0333333333333
49 26 965 839.5333333333333