-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.xml
172 lines (138 loc) · 52.5 KB
/
feed.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Narrative Consciousness</title>
<description>Quantitative Research on Quality of Consciousness</description>
<link>/</link>
<atom:link href="/feed.xml" rel="self" type="application/rss+xml" />
<pubDate>Tue, 29 Mar 2016 04:32:43 +0430</pubDate>
<lastBuildDate>Tue, 29 Mar 2016 04:32:43 +0430</lastBuildDate>
<generator>Jekyll v3.0.1</generator>
<item>
<title>8 Queens Puzzle مساله هشت وزیر</title>
<description><p>Solving Problem of 8 Queens using genetic algorithm in Clojure.
<div align="right"> حل مساله 8 وزیر توسط الگوریتم ژنتیک</div></p>
<!--more-->
<p>Source Code is on my Github : [https://github.com/zorvan/N-Queens]</p>
<figure class="highlight"><pre><code class="language-clojure" data-lang="clojure"> <span class="c1">; Written by Amin Razavi , Dec 2015</span>
<span class="c1">; Amirkabir University of Technology (Tehran Polytechnic)</span>
<span class="p">(</span><span class="kd">ns </span><span class="nv">core</span>
<span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">clojure.math.numeric-tower</span> <span class="ss">:only</span> <span class="p">(</span><span class="nf">abs</span><span class="p">)</span> <span class="ss">:as</span> <span class="nv">tower</span><span class="p">])</span>
<span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">clojure.set</span> <span class="ss">:only</span> <span class="p">(</span><span class="nf">difference</span><span class="p">)</span> <span class="ss">:as</span> <span class="nv">Set</span><span class="p">])</span>
<span class="p">(</span><span class="ss">:gen-class</span><span class="p">))</span>
<span class="c1">; Produce a Random Gene</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">init-gene</span> <span class="p">[</span><span class="nv">NumQueens</span> <span class="nv">GENE</span><span class="p">]</span>
<span class="p">(</span><span class="nb">reduce </span><span class="o">#</span><span class="p">(</span><span class="nb">assoc </span><span class="nv">%</span> <span class="nv">%2</span> <span class="p">(</span><span class="nb">rand-int </span><span class="nv">NumQueens</span><span class="p">))</span> <span class="nv">GENE</span> <span class="p">(</span><span class="nb">range </span><span class="p">(</span><span class="nb">count </span><span class="nv">GENE</span><span class="p">))))</span>
<span class="c1">; [It&#39;s Optional] Placing two queens in the same place is not allowed (at least at the beginning!)</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">validate-chromosome</span> <span class="p">[</span><span class="nv">chromosome</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">SetRange</span> <span class="p">(</span><span class="nb">set </span><span class="p">(</span><span class="nb">range </span><span class="p">(</span><span class="nb">count </span><span class="nv">chromosome</span><span class="p">)))]</span>
<span class="p">(</span><span class="k">loop </span><span class="p">[</span><span class="nv">distchrom</span> <span class="p">(</span><span class="nb">distinct </span><span class="nv">chromosome</span><span class="p">)]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nb">count </span><span class="nv">distchrom</span><span class="p">)</span> <span class="p">(</span><span class="nb">count </span><span class="nv">chromosome</span><span class="p">))</span> <span class="p">(</span><span class="nf">vec</span> <span class="p">(</span><span class="nb">sort </span><span class="nv">distchrom</span><span class="p">))</span>
<span class="p">(</span><span class="nf">recur</span> <span class="p">(</span><span class="nb">conj </span><span class="nv">distchrom</span> <span class="p">[(</span><span class="nb">first </span><span class="p">(</span><span class="nf">Set/difference</span> <span class="nv">SetRange</span> <span class="p">(</span><span class="nb">set </span><span class="p">(</span><span class="nb">map first </span> <span class="nv">distchrom</span><span class="p">))))</span>
<span class="p">(</span><span class="nb">first </span><span class="p">(</span><span class="nf">Set/difference</span> <span class="nv">SetRange</span> <span class="p">(</span><span class="nb">set </span><span class="p">(</span><span class="nb">map second </span><span class="nv">distchrom</span><span class="p">))))]))))))</span>
<span class="c1">; Initialize Genes in the Chromosome</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">init-chromosome</span> <span class="p">[</span><span class="nv">chromosome</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">GENE</span> <span class="p">(</span><span class="nb">first </span><span class="nv">chromosome</span><span class="p">)</span>
<span class="nv">NUMQ</span> <span class="p">(</span><span class="nb">count </span><span class="nv">chromosome</span><span class="p">)]</span>
<span class="p">(</span><span class="nb">reduce </span><span class="o">#</span><span class="p">(</span><span class="nb">assoc </span><span class="nv">%</span> <span class="nv">%2</span> <span class="p">(</span><span class="nf">init-gene</span> <span class="nv">NUMQ</span> <span class="nv">GENE</span><span class="p">))</span> <span class="nv">chromosome</span> <span class="p">(</span><span class="nb">range </span><span class="p">(</span><span class="nb">count </span><span class="nv">chromosome</span><span class="p">)))))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">codiagonal?</span> <span class="p">[</span><span class="nv">q1</span> <span class="nv">q2</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span> <span class="nb">= </span><span class="p">(</span><span class="nf">tower/abs</span> <span class="p">(</span><span class="nb">- </span><span class="p">(</span><span class="nb">first </span><span class="nv">q1</span> <span class="p">)</span> <span class="p">(</span><span class="nb">first </span><span class="nv">q2</span><span class="p">)))</span> <span class="p">(</span><span class="nf">tower/abs</span> <span class="p">(</span><span class="nb">- </span><span class="p">(</span><span class="nb">second </span><span class="nv">q1</span><span class="p">)</span> <span class="p">(</span><span class="nb">second </span><span class="nv">q2</span><span class="p">))))</span> <span class="nv">true</span> <span class="nv">false</span><span class="p">))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">cocolumn?</span> <span class="p">[</span><span class="nv">q1</span> <span class="nv">q2</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nb">first </span><span class="nv">q1</span><span class="p">)</span> <span class="p">(</span><span class="nb">first </span><span class="nv">q2</span><span class="p">))</span> <span class="nv">true</span> <span class="nv">false</span><span class="p">))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">corow?</span> <span class="p">[</span><span class="nv">q1</span> <span class="nv">q2</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nb">second </span><span class="nv">q1</span><span class="p">)</span> <span class="p">(</span><span class="nb">second </span><span class="nv">q2</span><span class="p">))</span> <span class="nv">true</span> <span class="nv">false</span><span class="p">))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">fitness</span> <span class="p">[</span><span class="nv">chromosome</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">N</span> <span class="p">(</span><span class="nb">count </span><span class="nv">chromosome</span><span class="p">)]</span>
<span class="p">(</span><span class="nb">reduce </span><span class="nv">+</span>
<span class="p">(</span><span class="nb">for </span><span class="p">[</span><span class="nv">i</span> <span class="p">(</span><span class="nb">range </span><span class="nv">N</span><span class="p">)</span> <span class="nv">j</span> <span class="p">(</span><span class="nb">range </span><span class="nv">N</span><span class="p">)</span> <span class="ss">:when</span> <span class="p">(</span><span class="nb">&gt; </span><span class="nv">j</span> <span class="nv">i</span><span class="p">)]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">q1</span> <span class="p">(</span><span class="nb">get </span><span class="nv">chromosome</span> <span class="nv">i</span><span class="p">)</span> <span class="nv">q2</span> <span class="p">(</span><span class="nb">get </span><span class="nv">chromosome</span> <span class="nv">j</span><span class="p">)]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">or </span><span class="p">(</span><span class="nb">= </span><span class="nv">q1</span> <span class="nv">q2</span><span class="p">)</span> <span class="p">(</span><span class="nf">corow?</span> <span class="nv">q1</span> <span class="nv">q2</span><span class="p">)</span> <span class="p">(</span><span class="nf">cocolumn?</span> <span class="nv">q1</span> <span class="nv">q2</span><span class="p">)</span> <span class="p">(</span><span class="nf">codiagonal?</span> <span class="nv">q1</span> <span class="nv">q2</span><span class="p">))</span> <span class="mi">1</span> <span class="mi">0</span><span class="p">))))))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">update-fitness</span> <span class="p">[</span><span class="nv">chromosome</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">fc</span> <span class="p">(</span><span class="nb">first </span><span class="nv">chromosome</span><span class="p">)]</span>
<span class="p">(</span><span class="nb">vector </span><span class="nv">fc</span> <span class="p">(</span><span class="nf">fitness</span> <span class="nv">fc</span><span class="p">))))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">init-population</span> <span class="p">[</span><span class="nv">population</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">RN</span> <span class="p">(</span><span class="nb">range </span><span class="p">(</span><span class="nb">count </span><span class="nv">population</span><span class="p">))</span>
<span class="nv">ipop</span> <span class="p">(</span><span class="nb">reduce </span><span class="o">#</span><span class="p">(</span><span class="nf">update-in</span> <span class="nv">%</span> <span class="p">[</span><span class="nv">%2</span> <span class="mi">0</span><span class="p">]</span> <span class="nv">init-chromosome</span><span class="p">)</span> <span class="nv">population</span> <span class="nv">RN</span><span class="p">)]</span>
<span class="p">(</span><span class="nf">vec</span> <span class="p">(</span><span class="nb">sort-by </span><span class="o">#</span><span class="p">(</span><span class="nb">second </span><span class="nv">%</span><span class="p">)</span> <span class="nb">&lt; </span><span class="p">(</span><span class="nb">reduce </span><span class="o">#</span><span class="p">(</span><span class="nf">update</span> <span class="nv">%</span> <span class="nv">%2</span> <span class="nv">update-fitness</span><span class="p">)</span> <span class="nv">ipop</span> <span class="nv">RN</span><span class="p">)))))</span>
<span class="c1">; sequence of partial sum of Fitnesses (last number in the seq is sum of all fitnesses)</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">SumFit</span> <span class="p">[</span><span class="nv">population</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">Fits</span> <span class="p">(</span><span class="nb">map second </span><span class="nv">population</span><span class="p">)]</span>
<span class="p">(</span><span class="nb">rest </span><span class="p">(</span><span class="nb">reduce </span><span class="o">#</span><span class="p">(</span><span class="nb">conj </span><span class="nv">%</span> <span class="p">(</span><span class="nb">+ </span><span class="nv">%2</span> <span class="p">(</span><span class="nb">last </span><span class="nv">%</span><span class="p">)))</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="nv">Fits</span><span class="p">))))</span>
<span class="c1">; Probability of selection is proportional to fitness</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">ChooseParent</span> <span class="p">[</span><span class="nv">population</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">SFQ</span> <span class="p">(</span><span class="nb">reverse </span><span class="p">(</span><span class="nf">SumFit</span> <span class="nv">population</span><span class="p">))</span>
<span class="nv">RND</span> <span class="p">(</span><span class="nb">rand-int </span><span class="p">(</span><span class="nb">first </span><span class="nv">SFQ</span><span class="p">))]</span>
<span class="p">(</span><span class="nb">dec </span><span class="p">(</span><span class="nb">count </span><span class="p">(</span><span class="nb">take-while </span><span class="o">#</span><span class="p">(</span><span class="nb">&gt; </span><span class="nv">%</span> <span class="nv">RND</span><span class="p">)</span> <span class="nv">SFQ</span><span class="p">)))))</span>
<span class="c1">; an Auxiliary function for selecting parents</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">AuxSelectParent</span> <span class="p">[</span><span class="nv">population</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">P1</span> <span class="p">(</span><span class="nf">ChooseParent</span> <span class="nv">population</span><span class="p">)</span>
<span class="nv">P2</span> <span class="p">(</span><span class="nf">ChooseParent</span> <span class="nv">population</span><span class="p">)]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">= </span><span class="nv">P1</span> <span class="nv">P2</span><span class="p">)</span> <span class="p">[</span><span class="nv">P1</span> <span class="p">(</span><span class="nf">ChooseParent</span> <span class="nv">population</span><span class="p">)]</span> <span class="p">[</span><span class="nv">P1</span> <span class="nv">P2</span><span class="p">])))</span>
<span class="c1">; n pairs of parents will be choosen</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">Select-Parents</span> <span class="p">[</span><span class="nv">population</span> <span class="nv">n</span><span class="p">]</span>
<span class="p">(</span><span class="nf">repeatedly</span> <span class="nv">n</span> <span class="o">#</span><span class="p">(</span><span class="nf">AuxSelectParent</span> <span class="nv">population</span><span class="p">)))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">Select-Mutants</span> <span class="p">[</span><span class="nv">population</span> <span class="nv">PopNum</span> <span class="nv">MutationRate</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">zero? </span><span class="nv">MutationRate</span><span class="p">)</span> <span class="p">[(</span><span class="nb">rand-int </span><span class="nv">PopNum</span><span class="p">)]</span>
<span class="p">(</span><span class="nf">vec</span> <span class="p">(</span><span class="nb">take </span><span class="nv">MutationRate</span> <span class="p">(</span><span class="nf">repeatedly</span> <span class="o">#</span><span class="p">(</span><span class="nb">rand-int </span><span class="nv">PopNum</span><span class="p">))))))</span>
<span class="c1">; Mutate a chromosome (random gene &amp; random value for coordinates)</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">mutate</span> <span class="p">[</span><span class="nv">chromosome</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">genes</span> <span class="p">(</span><span class="nb">first </span><span class="nv">chromosome</span><span class="p">)</span>
<span class="nv">N</span> <span class="p">(</span><span class="nb">count </span><span class="nv">genes</span><span class="p">)</span>
<span class="nv">rndgene</span> <span class="p">(</span><span class="nb">rand-int </span><span class="nv">N</span><span class="p">)</span>
<span class="nv">rndcoordinates</span> <span class="p">[(</span><span class="nb">rand-int </span><span class="nv">N</span><span class="p">)</span> <span class="p">(</span><span class="nb">rand-int </span><span class="nv">N</span><span class="p">)]</span> <span class="p">]</span>
<span class="p">(</span><span class="nf">update-fitness</span> <span class="p">[(</span><span class="nb">assoc </span><span class="nv">genes</span> <span class="nv">rndgene</span> <span class="nv">rndcoordinates</span><span class="p">)</span> <span class="mi">28</span><span class="p">])))</span>
<span class="c1">; Cross-over two chromosomes</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">xover</span> <span class="p">[</span><span class="nv">chrom1</span> <span class="nv">chrom2</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">N</span> <span class="p">(</span><span class="nb">count </span><span class="nv">chrom1</span><span class="p">)</span>
<span class="nb">node </span><span class="p">(</span><span class="nb">inc </span><span class="p">(</span><span class="nb">rand-int </span><span class="nv">N</span><span class="p">))</span> <span class="nv">cnode</span> <span class="p">(</span><span class="nb">- </span><span class="nv">N</span> <span class="nv">node</span><span class="p">)</span>
<span class="nv">intercombine</span> <span class="p">(</span><span class="nb">concat </span><span class="p">(</span><span class="nb">take node </span><span class="nv">chrom1</span><span class="p">)</span> <span class="p">(</span><span class="nf">take-last</span> <span class="nv">cnode</span> <span class="nv">chrom2</span><span class="p">)</span> <span class="p">(</span><span class="nf">take-last</span> <span class="nv">cnode</span> <span class="nv">chrom1</span><span class="p">)</span> <span class="p">(</span><span class="nb">take node </span><span class="nv">chrom2</span><span class="p">))]</span>
<span class="p">[[(</span><span class="nf">vec</span> <span class="p">(</span><span class="nb">take </span><span class="nv">N</span> <span class="nv">intercombine</span><span class="p">))</span> <span class="mi">0</span><span class="p">]</span>
<span class="p">[(</span><span class="nf">vec</span> <span class="p">(</span><span class="nf">take-last</span> <span class="nv">N</span> <span class="nv">intercombine</span><span class="p">))</span> <span class="mi">0</span><span class="p">]]))</span>
<span class="c1">; generate children</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">Children</span> <span class="p">[</span><span class="nv">population</span> <span class="nv">Parents</span><span class="p">]</span>
<span class="p">(</span><span class="nb">reduce into </span><span class="p">[]</span> <span class="p">(</span><span class="nf">pmap</span> <span class="o">#</span><span class="p">(</span><span class="nf">xover</span> <span class="p">(</span><span class="nf">get-in</span> <span class="nv">population</span> <span class="p">[(</span><span class="nb">first </span><span class="nv">%</span><span class="p">)</span> <span class="mi">0</span><span class="p">])</span> <span class="p">(</span><span class="nf">get-in</span> <span class="nv">population</span> <span class="p">[(</span><span class="nb">second </span><span class="nv">%</span><span class="p">)</span> <span class="mi">0</span><span class="p">]))</span> <span class="nv">Parents</span><span class="p">)))</span>
<span class="c1">; Produce New (Crossover) Children + Old Population </span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">New-Population</span> <span class="p">[</span><span class="nv">population</span> <span class="nv">XoverRate</span><span class="p">]</span>
<span class="p">(</span><span class="nb">into </span><span class="nv">population</span>
<span class="p">(</span><span class="nf">pmap</span> <span class="nv">update-fitness</span>
<span class="p">(</span><span class="nf">Children</span> <span class="nv">population</span> <span class="p">(</span><span class="nf">Select-Parents</span> <span class="nv">population</span> <span class="nv">XoverRate</span><span class="p">)))))</span>
<span class="c1">; Apply Mutation Operator on the New Populatuion</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">Mutate-Population</span> <span class="p">[</span><span class="nv">population</span> <span class="nv">PopNum</span> <span class="nv">MutationRate</span><span class="p">]</span>
<span class="p">(</span><span class="nf">vec</span> <span class="p">(</span><span class="nb">sort-by </span><span class="o">#</span><span class="p">(</span><span class="nb">second </span><span class="nv">%</span><span class="p">)</span> <span class="nb">&lt; </span><span class="p">(</span><span class="nb">reduce </span><span class="o">#</span><span class="p">(</span><span class="nf">update</span> <span class="nv">%</span> <span class="nv">%2</span> <span class="nv">mutate</span><span class="p">)</span> <span class="nv">population</span> <span class="p">(</span><span class="nf">Select-Mutants</span> <span class="nv">population</span> <span class="nv">PopNum</span> <span class="nv">MutationRate</span><span class="p">)))))</span>
<span class="c1">; if the boolean &quot;dynamic&quot; is true =&gt; population will grow with XOVER-RATE</span>
<span class="c1">; if the boolean &quot;dynamic&quot; is false =&gt; population will remain CTEPOP</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">Generation</span> <span class="p">[</span><span class="nv">population</span> <span class="nv">PopNum</span> <span class="nv">XoverRate</span> <span class="nv">MutationRate</span> <span class="nv">dynamic</span> <span class="nv">CTEPOP</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">mutedpop</span> <span class="p">(</span><span class="nf">Mutate-Population</span> <span class="p">(</span><span class="nf">New-Population</span> <span class="nv">population</span> <span class="nv">XoverRate</span><span class="p">)</span> <span class="nv">PopNum</span> <span class="nv">MutationRate</span><span class="p">)]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">true? </span><span class="nv">dynamic</span><span class="p">)</span> <span class="nv">mutedpop</span> <span class="p">(</span><span class="nf">vec</span> <span class="p">(</span><span class="nb">take </span><span class="nv">CTEPOP</span> <span class="nv">mutedpop</span><span class="p">)))))</span>
<span class="c1">; MAIN LOOP</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">-main</span> <span class="p">[</span><span class="o">&amp;</span> <span class="nv">args</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">empty?</span> <span class="nv">args</span><span class="p">)</span>
<span class="p">(</span><span class="nb">print </span><span class="s">&quot;Wrong Input arguments! \n USAGE : &#39;Number of Queens&#39; &#39;Number of Chromosomes&#39; &#39;Crossover-Rate[0.0-1.0]&#39; &#39;Mutation Rate[0.02-0.1]&#39;&quot;</span><span class="p">)</span>
<span class="p">(</span><span class="k">let </span><span class="p">[[</span><span class="nv">NUM-QUEENS</span> <span class="nv">NUM-CHROMOSOMES</span> <span class="nv">XOVER-RATE</span> <span class="nv">MUTATION-RATE</span><span class="p">]</span> <span class="nv">args</span>
<span class="nv">XR</span> <span class="p">(</span><span class="nb">* </span><span class="nv">XOVER-RATE</span> <span class="nv">NUM-CHROMOSOMES</span><span class="p">)</span>
<span class="nv">MR</span> <span class="p">(</span><span class="nb">* </span><span class="nv">MUTATION-RATE</span> <span class="nv">NUM-CHROMOSOMES</span><span class="p">)</span>
<span class="nv">MAXFIT</span> <span class="p">(</span><span class="nb">/ </span><span class="p">(</span><span class="nb">* </span><span class="nv">NUM-QUEENS</span> <span class="p">(</span><span class="nb">dec </span><span class="nv">NUM-QUEENS</span><span class="p">))</span> <span class="mi">2</span><span class="p">)</span>
<span class="nv">GENE</span> <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span><span class="p">]</span> <span class="c1">; Gene = Position Coordinates of each queen on the board</span>
<span class="nv">CHROMOSOME</span> <span class="p">(</span><span class="nf">vec</span> <span class="p">(</span><span class="nf">repeatedly</span> <span class="nv">NUM-QUEENS</span> <span class="o">#</span><span class="p">(</span><span class="nf">vec</span> <span class="nv">GENE</span><span class="p">)))</span> <span class="c1">; Chromosome = Board Configuration = Positions of Queens on the Board</span>
<span class="nv">POPULATION</span> <span class="p">(</span><span class="nf">vec</span> <span class="p">(</span><span class="nf">repeatedly</span> <span class="nv">NUM-CHROMOSOMES</span> <span class="o">#</span><span class="p">(</span><span class="nf">vec</span> <span class="p">[</span><span class="nv">CHROMOSOME</span> <span class="nv">MAXFIT</span><span class="p">])))]</span>
<span class="p">(</span><span class="nb">time </span>
<span class="p">(</span><span class="k">loop </span><span class="p">[</span><span class="nv">i</span> <span class="mi">1</span> <span class="nv">NewPop</span> <span class="p">(</span><span class="nf">Generation</span> <span class="p">(</span><span class="nf">init-population</span> <span class="nv">POPULATION</span><span class="p">)</span> <span class="nv">NUM-CHROMOSOMES</span> <span class="nv">XR</span> <span class="nv">MUTATION-RATE</span> <span class="nv">false</span> <span class="nv">NUM-CHROMOSOMES</span><span class="p">)]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">zero? </span><span class="p">(</span><span class="nb">second </span><span class="p">(</span><span class="nb">first </span><span class="nv">NewPop</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">print </span><span class="s">&quot;GOT IT! at&quot;</span> <span class="nv">i</span> <span class="s">&quot;th iteration:&quot;</span> <span class="p">(</span><span class="nb">first </span><span class="p">(</span><span class="nb">first </span><span class="nv">NewPop</span><span class="p">))</span> <span class="s">&quot;population=&quot;</span> <span class="p">(</span><span class="nb">count </span><span class="nv">NewPop</span><span class="p">)</span> <span class="s">&quot;\n\t=&gt;&quot;</span><span class="p">)</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">apply = </span><span class="p">(</span><span class="nb">map first </span><span class="p">(</span><span class="nb">take </span><span class="mi">5</span> <span class="nv">NewPop</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">print </span><span class="s">&quot;LOCAL MIN! at&quot;</span> <span class="nv">i</span> <span class="s">&quot;th iteration, fitness value=&quot;</span> <span class="p">(</span><span class="nb">second </span><span class="p">(</span><span class="nb">first </span><span class="nv">NewPop</span><span class="p">))</span> <span class="s">&quot;population=&quot;</span> <span class="p">(</span><span class="nb">count </span><span class="nv">NewPop</span><span class="p">)</span> <span class="s">&quot;\n\t=&gt;&quot;</span><span class="p">)</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">&gt; </span><span class="nv">i</span> <span class="mi">500</span><span class="p">)</span>
<span class="p">(</span><span class="nb">print </span><span class="s">&quot;TIMEOUT! fitness value=&quot;</span> <span class="p">(</span><span class="nb">second </span><span class="p">(</span><span class="nb">first </span><span class="nv">NewPop</span><span class="p">))</span> <span class="s">&quot;population=&quot;</span> <span class="p">(</span><span class="nb">count </span><span class="nv">NewPop</span><span class="p">)</span> <span class="s">&quot;\n\t=&gt;&quot;</span><span class="p">)</span>
<span class="p">(</span><span class="nf">recur</span> <span class="p">(</span><span class="nb">inc </span><span class="nv">i</span><span class="p">)</span> <span class="p">(</span><span class="nf">Generation</span> <span class="nv">NewPop</span> <span class="nv">NUM-CHROMOSOMES</span> <span class="nv">XR</span> <span class="nv">MR</span> <span class="nv">false</span> <span class="nv">NUM-CHROMOSOMES</span><span class="p">))))))))))</span>
<span class="c1">; Functional Version of Main</span>
<span class="c1">; (first (filter #(zero? (second (first %))) (iterate #(Generation % 0.2 0.2 false 10) (init-population p))))</span></code></pre></figure>
</description>
<pubDate>Sat, 21 Nov 2015 03:08:00 +0330</pubDate>
<link>/2015/11/21/Eight-Queens-Puzzle.html</link>
<guid isPermaLink="true">/2015/11/21/Eight-Queens-Puzzle.html</guid>
<category>AI</category>
<category>Genetic-Algorithms</category>
<category>Clojure</category>
</item>
</channel>
</rss>