Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 3.01 KB | None | 0 0
  1.  
  2. include("Permutations.jl")
  3. using Permutations
  4. using PyPlot
  5.  
  6.  
  7. function simulate(N::Int64, M::Int64, K::Int64)
  8.  
  9.   E1 = zeros(Float64,N)
  10.   Eq1 = zeros(Float64,N)
  11.   V1 = zeros(Float64,N)
  12.   D1 = zeros(Float64,N)
  13.  
  14.   E2 = zeros(Float64,N)
  15.   Eq2 = zeros(Float64,N)
  16.   V2 = zeros(Float64,N)
  17.   D2 = zeros(Float64,N)
  18.  
  19.   E3 = zeros(Float64,N)
  20.   Eq3 = zeros(Float64,N)
  21.   V3 = zeros(Float64,N)
  22.   D3 = zeros(Float64,N)
  23.  
  24.   for n = 1:N
  25.     for step = 1:M
  26.       perm = Permutations.RandomPermutation(n)
  27.       tmp1 = getNoOfFixedPoints(perm)
  28.       E1[n] += tmp1
  29.       Eq1[n] += tmp1^2
  30.  
  31.       tmp2 = 0
  32.       if K != 0
  33.         tmp2 = getKSizeCycles(K, perm)
  34.         E2[n] += tmp2
  35.       else
  36.         tmp2 = length(Permutations.cycles(perm))
  37.         E2[n] += tmp2
  38.       end
  39.       Eq2[n] += tmp2^2
  40.  
  41.       tmp3 = getNumberOfRecords(perm)
  42.       E3[n] += tmp3
  43.       Eq3[n] += tmp3^2
  44.     end
  45.  
  46.     E1[n] /= M
  47.     Eq1[n] /= M
  48.     V1[n] = Eq1[n] - (E1[n])^2
  49.     D1[n] = sqrt(V1[n])
  50.  
  51.     E2[n] /= M
  52.     Eq2[n] /= M
  53.     V2[n] = Eq2[n] - (E2[n])^2
  54.     D2[n] = sqrt(V2[n])
  55.  
  56.     E3[n] /= M
  57.     Eq3[n] /= M
  58.     V3[n] = Eq3[n] - (E3[n])^2
  59.     D3[n] = sqrt(V3[n])
  60.  
  61.  
  62.   end
  63.  
  64.   printDiagram(E1, V1, D1, 0)
  65.   printDiagram(E2, V2, D2, 1)
  66.   printDiagram(E3, V3, D3, 2)
  67.  
  68. end
  69.  
  70. function getNoOfFixedPoints(perm::Permutations.Permutation)
  71.   noOfFixedPoints = 0
  72.  
  73.   for i = 1:length(perm)
  74.     if perm[i] == i
  75.       noOfFixedPoints +=1
  76.     end
  77.   end
  78.  
  79.   return noOfFixedPoints
  80. end
  81.  
  82. function getKSizeCycles(k::Int64, perm::Permutations.Permutation)
  83.   cycles = Permutations.cycles(perm)
  84.   noOfKSizeCycles = 0
  85.  
  86.   for i = 1:length(cycles)
  87.     if length(cycles[i]) == k
  88.       noOfKSizeCycles += 1
  89.     end
  90.   end
  91.  
  92.   return noOfKSizeCycles
  93. end
  94.  
  95. function getNumberOfRecords(perm::Permutations.Permutation)
  96.   noOfRecords = 1
  97.   max = perm[1]
  98.  
  99.   for element = 2:length(perm)
  100.     if element > max
  101.       max = element
  102.       noOfRecords += 1
  103.     end
  104.   end
  105.  
  106.   return noOfRecords
  107. end
  108.  
  109. function Var(E::Array, Eq::Array)
  110.   var = 0
  111.   for i = 1:length(E)
  112.     var = Eq[i] - (E[i])^2
  113.   end
  114.   return var
  115. end
  116.  
  117.  
  118. function Deviation(Var::Float64)
  119.     return sqrt(Var)
  120. end
  121.  
  122. function printDiagram(E::Array, V::Array, D::Array, case::Int64)
  123.  
  124.   D1 = zeros(Float64,N)
  125.   D2 = zeros(Float64,N)
  126.   for i = 1:length(D)
  127.     D1[i] = D[i] + 3
  128.     D2[i] = D[i] - 3
  129.   end
  130.   msg = ""
  131.   if case == 0
  132.     msg = " fixed points number"
  133.   elseif case == 1
  134.     msg = " cycles number"
  135.   elseif case == 2
  136.     msg = " records number"
  137.   end
  138.   x = 1:N
  139.   fig, ax = subplots()
  140.   suptitle("Permutations")
  141.  
  142.   ax[:plot](x, E, linewidth=2, alpha=0.6, label="Average value of$msg")
  143.   ax[:plot](x, V, linewidth=2, alpha=0.6, label="Variation of$msg")
  144.   ax[:plot](x, D1, linewidth=2, alpha=0.6, label="(+)Deviation of$msg")
  145.   ax[:plot](x, D2, linewidth=2, alpha=0.6, label="(-)Deviation of$msg")
  146.   ax[:legend]()
  147.   fileName = string("permutations_$msg.png")
  148.   savefig(fileName, dpi=72)
  149.  
  150. end
  151.  
  152.  
  153.  
  154. N = 100
  155. M = 100000
  156. K = 0
  157.  
  158. simulate(N, M, K)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement