Sie sind auf Seite 1von 10

Artificial Intelligence Assignment No.

4
Assignment No.4: GENETIC ALGORITHM
Subject: Artificial Intelligence Assigned: May 8,2014 Ma!s: --
Instucto: Tanveer Ahme "ue "ate: May 1!,2014
C#ass : $CS%
&'MCS%4
O$(ECTI)E:
The o"#ective of this assignment is to give yo$ some %ractice regaring:
To intro$ce st$ents &ith 'enetic Algorithms $se in AI
(nerstaning ho& to $se 'enetic Algorithms %ractically
INSTR*CTIONS:
)lease rea the follo&ing instr$ctions "efore attem%ting to solve this assignment
1. This is an inivi$al assignment. *o$ &ill s$"mit yo$r &or+ inivi$ally thro$gh yo$r logins.
Also s$"mit harco%y of the assignment.
2. ,rite yo$r name an roll n$m"er at the start of the assignment.
-- *o$ sho$l concern recommene "oo+s for clarify yo$r conce%ts, as hano$ts are not s$fficient.
4. Try to ma+e sol$tion "y yo$rself an %rotect yo$r &or+ from other st$ents. If I fo$n the
sol$tion files of some st$ents are same then I &ill re&ar +eo ma!s to all those st$ents.
!. .ealine for this assignment is Ma, -./ 01-4. This ealine &ill not "e e/tene.
2uestion No. -
'iven %airs of 0/, y1 coorinates, fin the "est %ossi"le m, c %arameters of the line y 2 m/ 3 c that
generates them. (se m$tation only. )resent the "est %ossi"le sol$tion given the ata after at least
three iterations of 'A or e/it if yo$ fin the sol$tion earlier.
0/, y1 : 401,2.!1 02, -.5!16
Initial %o%$lation 72 087- 18
2uestion No. 0
9$%%ose yo$ have a large n$m"er of 0/, y1 ata %oints namely 01.0, 4.11, 0-.1, :.!1, 0-!.2, 8.;1. *o$
&o$l li+e to fit a %olynomial of $% to egree ! thro$gh these ata %oints, i.e., yo$ &ant a form$la y
2 a/
!
3 "/
4
3 c/
-
3 /
2
3e/ 3 f that gives yo$ a reasona"ly goo fit to the act$al ata. There are some
stanar c$rve fitting techni<$es, "$t let=s ass$me yo$ on=t +no& a"o$t them. *o$ only +no& the
$se of genetic algorithm to fin a >%retty goo? sol$tion
@ *o$r >genes? are : a, ", c, , e, an f
@ *o$r >chromosome? is the array 7a, ", c, , e, f8
@ *o$r >fitness f$nction? for one array is:
Aor every act$al ata %oint 0/, y1
@ Bom%$te C 2 a/
!
3 "/
4
3 c/
-
3 /
2
3e/ 3 f
@ Ain the s$m of 0y D C1
2
over all /
@ The lo&est s$m re%resents the "est fit
The initial %o%$lation siEe is - chosen ranomly. No& r$n at least three iterations of 'A or e/it if
BFM9AT9 Gehari 1
Artificial Intelligence Assignment No. 4
yo$ fin the sol$tion earlier "y a%%lying "oth m$tation an crossover ta+ing chosen initial
%o%$lation an give the "est t&o genes after r$nning three iterations.
2uestion No. 3
9olve the o%timiEation %ro"lem given "elo& $sing 'A:
Ma/imiEe A0H1 2 Hsin
2
010

H1 31
9$"#ect to
-1 IHI2
Re4esentation:
In this %ro"lem, a "inary string 0chromosome1 has "een consiere to re%resent the real val$es of the
varia"le. The length o the vector e%ens on the re<$ire %recision, &hich, in this %ro"lem, is si/
%laces after the ecimal %oint. The omain of the varia"le H has a length e<$al to -J the %recision
re<$irement im%lies that the range 7-1, 28 sho$l "e ivie into at least -,000,000 e<$al siEe
intervals. This means that 22 "its are re<$ire to re%resent one "inary string 0chromosome1. The
ma%%ing from "inary string 0"
21
,"
20
,KK."
2
,"
1
,"
0
1 to a real n$m"er H from the range is
straightfor&ar an is %erforme in t&o ste%s:
Bonvert the "inary string 0"
21
,"
20
,KK."
2
,"
1
,"
0
1 from "ase 2 to "ase 10 an set it e<$al to H=
Ain the corres%oning real n$m"er $sing the follo&ing form$la
H 2 -1.0 3 7-L02
22
-118H=
Aor e/am%le, a chromosome 010001011101101010001111 re%resents the n$m"er 0.;-51:5.
Initia# 4o4u#ation
Ass$me that the %o%$lation siEe is -. 9$%%ose &e have the follo&ing initial %o%$lation
B
0
2 010001011101101010001111
B
1
2 00000001110000000010000 1
B
2
2 01110000000111111000101 1
5itness 5unction:
The fitness f$nction Mfitness= for "inary string B
i
is e<$ivalent to the f$nction A0H1.
Genetic O4eato:
(se "oth m$tation an crossover o%eration for re%ro$ction of ne& generation0offs%ring1
M$tation: Ali%%e one or more "it ranomly
Brossover: (se t&o %oint cross over o%erator 0 see $%loae hano$ts1
Temination: )erform atleast three iterations
2uestion No. 4
9olve the o%timiEation %ro"lem given "elo& $sing 'A:
Ma/imiEe A0H1 2 Hsin
2
010

H1 31
9$"#ect to
-1 IHI2
Initia# 4o4u#ation
Ass$me that the %o%$lation siEe is 4. 9$%%ose &e have the follo&ing initial %o%$lation
Bhromosome 'ene
H
B
0
1.!
B
1
-1
B
2
1.5!
B
-
0.:8
5itness 5unction:
The fitness f$nction Mfitness= for B
i
is e<$ivalent to the f$nction A0H1. The larger the val$e res$lting
BFM9AT9 Gehari 2
Artificial Intelligence Assignment No. 4
from the given f$nction, the higher the fitness of the chromosome. Ain the 6itness 7a#ues of the
a"ove chromosomes an give the "est t&o of them.
Genetic O4eato:
A%%ly arithmetic crossover o%erator on t&o "est %arent chromosome vectors to %ro$ce t&o ne&
offs%ring accoring to the e<$ations: 09ee $%loae hano$ts1
Fffs%ring
1
2 a N )arent
1
3 01- a1 N )arent
2
Fffs%ring
2
2 01 D a1 N )arent
1
3 a N )arent
2
&here a is a ranom &eighting factor chosen "efore each crossover o%eration. Oet the &eighting
factor a 2 0.4!.
Temination: P$n at least three iterations of 'A or e/it if yo$ fin the sol$tion earlier
2uestion No. .
Ain the %arameters x an y that ma/imiEe the follo&ing f$nction:

2 2
1
0 , 1
1
f x y
x y
=
+ +
01.11
9$"#ect to

2 2
2 -
x
y


9$%%ose &e have the follo&ing initial %o%$lation
BFM9AT9 Gehari -
Bhromosome Genes
x y
B
0
-1 2
B
1
-2 -
B
2
1.! 0
B
-
0.! -1
Artificial Intelligence Assignment No. 4
Oet the fitness for this %ro"lem is sim%ly e<$ation 01.11. The larger the val$e res$lting from the
given f$nction, the higher the fitness of the chromosome.
a1 Ain the 6itness 7a#ues of the a"ove chromosomes an give the "est t&o of them.
"1 A%%ly arithmetic crossover o%erator on t&o "est %arent chromosome vectors to %ro$ce t&o
ne& offs%ring accoring to the e<$ations:
Fffs%ring
1
2 a N )arent
1
3 01- a1 N )arent
2
Fffs%ring
2
2 01 D a1 N )arent
1
3 a N )arent
2
&here a is a ranom &eighting factor chosen "efore each crossover o%eration. Oet the &eighting
factor a 2 0.5. P$n at least three iterations of 'A or e/it if yo$ fin the sol$tion earlier
2uestion No. 8
9olve the o%timiEation %ro"lem given "elo& $sing 'A:
Ma/imiEe A0H,*1 2 Hsin04

H1 3 *sin020

H1
9$"#ect to
--.0 IHI12.1
4.1 I*I!.8
Re4esentation:
In this %ro"lem, a "inary string 0chromosome1 has "een consiere to re%resent the real val$es of the
varia"le. The length o the vector e%ens on the re<$ire %recision, &hich, in this %ro"lem, is si/
%laces after the ecimal %oint.
Initia# 4o4u#ation
Ass$me that the %o%$lation siEe is 4. 9$%%ose &e have the follo&ing initial %o%$lation
5itness 5unction
BFM9AT9 Gehari 4
Bhromosome 'enes
H *
B
0
-2 4.!
B
1
11 -
B
2
-1.! 0
B
-
; 2.!
Artificial Intelligence Assignment No. 4
The fitness f$nction Mfitness= for B
i
is e<$ivalent to the f$nction A0H1. The larger the val$e res$lting
from the given f$nction, the higher the fitness of the chromosome.
Genetic O4eato:
9elect t&o %airs of %arents for recom"ination. )ic+ three "est chromosome "ase on fitness val$e
an ma+e t&o %air as follo&s:
5ist 4ai o6 4aents: 401
st
"est chromosome1, 02
n
"est chromosome16
Second 4ai o6 4aent: 401
st
"est chromosome1, 0-
r
"est chromosome16
Qach %arent &ill contri"$te via crossover to chilren in the ne/t generation. No& ne& generation
&ill "e %ro$ce as follo&s:
B
0
0/ val$e of 1
st
"est chromosome, y val$e of 2
n
"est chromosome1
B
1
0/ val$e of 2
n
"est chromosome, y val$e of 1
st
"est chromosome1
B
2
0/ val$e of 1
st
"est chromosome, y val$e of -
r
"est chromosome1
B
-
0/ val$e of -
r
"est chromosome, y val$e of 1
st
"est chromosome1
Aor e/am%le:
Rase on the fitness, &e select B- an B2 as the first set of %arents an B- an B0 as the secon set
of %arents. Reca$se only t&o genes e/ist &ithin the chromosome, &e s&a% at gene level, res$lting in
each chromosome s&a%%ing its / an y elements. Aollo&ing ta"le sho&s the ne& %o%$lation
BFM9AT9 Gehari !
Bhromosome 'enes
H *
B
0
0.! 0
B
1
1.! -1
B
2
0.! 2
B
-
-1 -1
Bhromosome 'enes
Aitness H *
B
0
-1 2 0.1;5
B
1
-2 - 0.005
B
2
1.! 0 0.-1
B
-
0.! -1 0.44
Artificial Intelligence Assignment No. 4
Temination: P$n at least three iterations of 'A or e/it if yo$ fin the sol$tion earlier
2uestion No. 8
9$%%ose &e are esigning a single %layer Tic Tac Toe game, the secon %layer &ill "e the com%$ter,
&e &ant to assist the com%$ter in ta+ing ecision &hat %ossi"le &inning %ath com%$ter sho$l try to
follo&, the all %ossi"le &inning %aths are given "elo&,
Re4esentation
,e re%resent the chromosome as follo&s:
Qach chromosome has the follo&ing str$ct$re:
BFM9AT9 Gehari ;
Artificial Intelligence Assignment No. 4
Initia# 9o4u#ation
9$%%ose &e have the follo&ing initial %o%$lation,
5itness 5unction:
The fitness val$e of the gene 2 9$m of all %ossi"le &inning %aths that &e can ma+e from a cell.
Aor e/am%le the fitness val$e for 'ene 1 is calc$late as,
Aitness val$e 2 - 0F in col$mn 1 can "e $se for - &inning %aths1 3 4 0F in col$mn 2 can "e $se
for 4 &inning %aths1 3 2 0F in col$mn - can "e $se for 2 &inning %aths1 2 :
9imilarly fitness val$e for other genes can "e calc$late.
Genetic O4eato:
9elect "est t&o chromosomes from %o%$lation on the "asis of fitness val$e. Airst a%%ly m$tation as
follo&s:
Ta+e one=s com%lement of first %arent
A%%ly "it&ise HFP of secon %arent &ith mas+ 100111
After a%%lying m$tation, a%%ly t&o %oint cross over o%erator.
Temination: P$n at least three iterations of 'A.
BFM9AT9 Gehari 5
Artificial Intelligence Assignment No. 4
2uestion No. : ;$ON*S 2*ESTION<
9$%%ose &e are $sing eight "it "inary n$m"er 0a gene1 to re%resent the state of vieo lect$re
elivere "y the %rofessor,
'oal:
9$%%ose o$r goal is,
,here Eero 0 re%resents tr$e conition an 1 re%resent false conition.
Initia# 9o4u#ation:
9$%%ose &e have initial %o%$lation as follo&s,
BFM9AT9 Gehari 8
Artificial Intelligence Assignment No. 4
0All 1=s an 0=s re%resent some conition 0 means tr$e an 1 means false1
5itness 5unction:
9$%%ose o$r fitness f$nction is N$m"er of Seros at their correct %laces as in the goal.
Aor e/am%le the fitness f$nction val$e for gene 1 of initial %o%$lation is,
No. of Eeros of gene1 at correct %lace as com%are to 'oal are 2 -. 9o Aitness val$e of gene1 is 2 -
No& r$n at least three iterations a%%lying "oth m$tation an crossover ta+ing given initial
%o%$lation an give the "est t&o genes after r$nning three iterations.
2uestion No. = ;$ON*S 2*ESTION<
9ome %ro"lems in coing theory re<$ire the constr$ction of "inary coe &ors of minimal &eight,
i.e., containing as fe& ones as %ossi"le 0&ith certain constraints1. Oetting &0n1 2 n$m"er of ones in
the "inary re%resentation of integer n, s$%%ose &e efine the follo&ing eval$ation f$nction for
integers:
&0n1 if n is evil "$t not %ernicio$s
f0n1 2 &0n1N2 if n is oio$s "$t not %ernicio$s
&0n1N- if n is %ernicio$s

f0n1 2 &0n1 if n is evil "$t not pernicious


2 &0n1N2 if n is odious "$t not pernicious
BFM9AT9 Gehari :
Artificial Intelligence Assignment No. 4
2 &0n1N- if n is pernicious
7&here a n$m"er is evil if it has an even n$m"er of ones in its "inary re%resentation, a n$m"er is
odious if it has an o n$m"er of ones, an a n$m"er is pernicious if it has a %rime n$m"er of ones8.
Aor those of yo$ &itho$t a com%rehensive school e$cation, %rime n$m"ers are efine as having
e/actly t&o ivisors, &hich means that 1 is not a %rime 0an hasn=t "een consiere so for over a
h$nre years1. Note that 2 is a %rime 0the only even one1.
a1 ,hich of these n$m"ers is %ernicio$sT ,hich is evilT ,hat &o$l they score $sing the eval$ation
f$nctionT
-0, -1, -2, --, -4
"1 9$%%ose an agent is $sing a 'A-style search for "it strings of length ;, an the fitness f$nction
ivies the a"ove eval$ation f$nction, f, for an integer "y the average of the eval$ation f$nction over
all the %o%$lation 7as &e escri"e in the 'A lect$re8. 9$%%ose that the "it strings for the n$m"ers
-0 to -4 ma+e $% the entire %o%$lation 0&ith an e/tra Eero %$t on the OQAT han sie for integers
&hich can "e re%resente &ith ! "its1. ,hat n$m"er &o$l the fitness f$nctions assign each integerT
Aor each integer, ho& many co%ies of each are guaranteed to go into the intermeiate %o%$lationT
c1 )erform one-%oint crossover on the "it-strings for -1 an -2, &ith the crossover %oint after the
secon "it 0co$nting "its from the OQAT in their re%resentation. ,hat n$m"ers are re%resente "y
the "it-strings for the t&o offs%ringT Balc$late the val$e of the eval$ation f$nction for the t&o
offs%ring.
1 )erform t&o-%oint crossover on the "it-strings for -0 an --, &ith the crossover %oints after the
first an the fo$rth "its 0co$nting "its from the OQAT1. ,hat integers are re%resente "y the "it-
strings of the t&o offs%ringT Balc$late the val$e of the eval$ation f$nction for the t&o offs%ring.
e1 ,hy can there "e no other inivi$al &hich scores higher on the eval$ation f$nction than the
inivi$al -1T
f1 7F%tional D for the mathletes amongst yo$8. 9ho& that even %erfect n$m"ers 0s$ch as ;, 28 an
4:;, &hich are e<$al to half the s$m of their ivisors1 are %ernicio$s.
NNNNNNNNNNNNNNNNNNNNNNNNNNBEST OF LUCKNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
BFM9AT9 Gehari 10