The AIMA java classes Introduction The AIMA java classes It is a java class package that allows to define and solve search problems It implements most of the algorithms explained in the course Uninformed search: Breadth First search, Deep First search, Iterative Deepening Search Heuristic search: A * , IDA * Local Search: Hill Climbing, Simulated Annealing The implementation uses genericity to separate the representation of the problem from the search algorithms Javier B´ ejar cbea (LSI - FIB) Heuristic Search Artif. Intelligence (Fall 2010) 1 / 29
29
Embed
The AIMA java classes Introduction The AIMA java classesbejar/ia/transpas/lab/aima-eng.pdf · The AIMA java classes Introduction The AIMA java classes It is a java class package that
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
The AIMA java classes Introduction
The AIMA java classes
It is a java class package that allows to define and solve searchproblems
It implements most of the algorithms explained in the course
Uninformed search: Breadth First search, Deep First search, IterativeDeepening SearchHeuristic search: A∗, IDA∗
Local Search: Hill Climbing, Simulated Annealing
The implementation uses genericity to separate the representation ofthe problem from the search algorithms
Defined inside the package aima.search.eightpuzzle
You can find the 4 classes needed to solve the problem:
EightPuzzleBoard, represents the board (an array with 9 positions,numbers from 0 to 8, 0 represents the blank tile)ManhattanHeuristicFunction, implement an heuristic function (sumof Manhattan distance of tiles positions)EightPuzzleSuccessorFuncion, implements the function generatingall the accessible states from a given one (all possible movements ofthe blank tile)EightPuzzleGoalTest, defines the function that test for the goalstate
The class aima.search.demos.EightPuzzleDemo implements somefunctions that solve the problems using different search algorithms
You can find the 4 classes needed to solve the problem:
ProbIA15Board, implementation of the board (an array of 5 positionswith a given configuration)ProbIA15HeuristicFunction, implements the heuristic function(number of white tiles)ProbIA15SuccessorFunction, implements the function generating allthe accessible states from a given one (jump and shift)probIA15GoalTest, defines the function that test for the goal state
The class IA.probIA15.ProbIA15Demo implements some functionsthat solve the problems using different search algorithms
1 pub l i c c l a s s ProbIA15Board {2 /∗ S t r i n g s used i n the t r a c e ∗/3 pub l i c s t a t i c S t r i n g DESP DERECHA = ” D e s p l a z a r Derecha ” ;4 . . .5 p r i v a t e char [ ] board = { ’N ’ , ’N ’ , ’B ’ , ’B ’ , ’O ’ } ;67 /∗ Con s t r u c t o r ∗/8 pub l i c ProbIA15Board ( char [ ] b ) {9 f o r ( i n t i =0; i <5; i ++) board [ i ]=b [ i ] ;
10 }1112 /∗ Au x i l i a r y f u n c t i o n s ∗/1314 pub l i c char [ ] g e t C o n f i g u r a t i o n (){15 r e t u r n board ;16 }1718 /∗ Get the t i l e i n p o s i t i o n i ∗/19 p r i v a t e char getPos ( i n t i ){20 r e t u r n ( board [ i ] ) ;21 }2223 /∗ Po s i t i o n o f the b l ank t i l e ∗/24 pub l i c i n t getGap (){25 i n t v =0;2627 f o r ( i n t i =0; i <5; i ++) i f ( board [ i ]== ’O ’ ) v=i ;28 r e t u r n v ;29 }30 . . .
31 /∗ Func t i on s t ha t t e s t the c o n d i t i o n s o f the o p e r a t o r s ∗/32 pub l i c boolean p u e d e D e s p l a z a r D e r e c h a ( i n t i ) {33 i f ( i ==4) r e t u r n ( f a l s e ) ;34 e l s e r e tu rn ( board [ i +1]== ’O ’ ) ;35 }3637 . . .3839 /∗ Func t i on s imp lement ing the o p e r a t o r s ∗/40 pub l i c vo id d e s p l a z a r D e r e c h a ( i n t i ){41 board [ i +1]=board [ i ] ;42 board [ i ]= ’O ’ ;43 }4445 . . .4647 /∗ Funct i on tha t t e s t s f o r the goa l s t a t e ∗/48 pub l i c boolean i s G o a l (){49 boolean n o b l a n c o=t rue ;5051 f o r ( i n t i =0; i <5; i ++) n o b l a n c o=n o b l a n c o && ( board [ i ] != ’B ’ ) ;52 r e t u r n n o b l a n c o ;53 }
1 package IA . probIA15 ;23 import j a v a . u t i l . Comparator ;4 import j a v a . u t i l . A r r a y L i s t ;5 import IA . probIA15 . ProbIA15Board ;6 import aima . s e a r c h . f ramework . H e u r i s t i c F u n c t i o n ;78 pub l i c c l a s s P r o b I A 1 5 H e u r i s t i c F u n c t i o n implements H e u r i s t i c F u n c t i o n {9
10 pub l i c double g e t H e u r i s t i c V a l u e ( Object n ) {11 ProbIA15Board board=(ProbIA15Board ) n ;12 char [ ] c o n f ;13 double sum=0;1415 c o n f=board . g e t C o n f i g u r a t i o n ( ) ;16 f o r ( i n t i =0; i <5; i ++) i f ( c o n f [ i ]== ’B ’ ) sum++;1718 r e t u r n ( sum ) ;19 }20 }
1 package IA . probIA15 ;23 import aima . s e a r c h . f ramework . S u c c e s s o r ;4 import aima . s e a r c h . f ramework . S u c c e s s o r F u n c t i o n ;5 import IA . probTSP . P r o b T S P H e u r i s t i c F u n c t i o n ;67 pub l i c c l a s s P r o b I A 1 5 S u c c e s s o r F u n c t i o n implements S u c c e s s o r F u n c t i o n {89 pub l i c L i s t g e t S u c c e s s o r s ( Object a S t a t e ) {
10 A r r a y L i s t r e t V a l= new A r r a y L i s t ( ) ;11 ProbIA15Board board=(ProbIA15Board ) a S t a t e ;1213 f o r ( i n t i =0; i <5; i ++){14 i f ( board . p u e d e D e s p l a z a r D e r e c h a ( i )){15 ProbIA15Board newBoard= new ProbIA15Board ( board . g e t C o n f i g u r a t i o n ( ) ) ;16 newBoard . d e s p l a z a r D e r e c h a ( i ) ;17 r e t V a l . add (new S u c c e s s o r (new S t r i n g ( ProbIA15Board . DESP DERECHA+” ”+18 newBoard . t o S t r i n g ( ) ) , newBoard ) ) ;19 }20 . . .21 }22 r e t u r n ( r e t V a l ) ;23 }24 }
1 package IA . probIA15 ;23 import j a v a . u t i l . A r r a y L i s t ;4 import aima . s e a r c h . f ramework . Goa lTest ;56 pub l i c c l a s s ProbIA15GoalTest implements Goa lTest {78 pub l i c boolean i s G o a l S t a t e ( Object a S t a t e ) {9 boolean g o a l ;
10 ProbIA15Board board= ( ProbIA15Board ) a S t a t e ;1112 r e t u r n board . i s G o a l ( ) ;13 }
You can see the procedure from the examples, you have to:
Define an object of type Problem that receives as a parameter a setof objects representing the initial state, the generator of successorstates function, the goal state function and, if an informed searchalgorithm is used, the heuristic function
Define an object of type Search as an instance of the class of thealgorithm you are going to use
Define an object of type SearchAgent that receives the objectsProblem and Search
The functions printActions and printInstrumentation printsthe solution search path and some statistical information dependingon the algorithm
1 p r i v a t e s t a t i c vo id I A P 1 5 B r e a d t h F i r s t S e a r c h ( ProbIA15Board IAP15 ) {23 Problem problem = new Problem ( IAP15 ,4 new P r o b I A 1 5 S u c c e s s o r F u n c t i o n ( ) ,5 new ProbIA15GoalTest ( ) ) ;6 Search s e a r c h = new B r e a d t h F i r s t S e a r c h (new TreeSearch ( ) ) ;7 SearchAgent agent = new SearchAgent ( problem , s e a r c h ) ;8 . . .9
10 }1112 p r i v a t e s t a t i c vo id IAP15AStarSearchH1 ( ProbIA15Board TSPB) {13 Problem problem = new Problem (TSPB,14 new P r o b I A 1 5 S u c c e s s o r F u n c t i o n ( ) ,15 new ProbIA15GoalTest ( ) ,16 new P r o b I A 1 5 H e u r i s t i c F u n c t i o n ( ) ) ;17 Search s e a r c h = new AStarSearch (new GraphSearch ( ) ) ;18 SearchAgent agent = new SearchAgent ( problem , s e a r c h ) ;19 . . .20 }
You can find 4 classes defining the problem:ProbTSPBoard, implementation of the problem (an array of size nrepresenting the path that visits the n cities)ProbTSPHeuristicFunction, implement an heuristic function (lengthof the path)ProbTSPSuccessorFunction, implements the function generating allthe accessible states from a given one (All possible two citiesinterchange)ProbTSPSuccessorFunctionSA, implements the function generatingall the accessible states from a given one optimized for SimulatedAnnealingprobTSPGoalTest, this functions always returns false (this is becausethe goal state is unknown)
The class ProbTSPJFrame, the main program, it shows a GUI thatallows to run the hill climbing and simulated annealing algorithmswith a random TSP problem
In the case of the Hill Climbing algorithm, to pick the best successorall successor states have to be generated
In the case of the Simulated Annealing algorithm, its strategy doesnot need to see all the successors since it picks one at random
From computational cost perspective, for the Simulated Annealingalgorithm is more efficient to generate only one successor by applyingan operator pick at random than to generate all the successors andthen to pick one at random
1 pub l i c L i s t g e t S u c c e s s o r s ( Object a S t a t e ) {2 A r r a y L i s t r e t V a l = new A r r a y L i s t ( ) ;3 ProbTSPBoard board = ( ProbTSPBoard ) a S t a t e ;4 P r o b T S P H e u r i s t i c F u n c t i o n TSPHF = new P r o b T S P H e u r i s t i c F u n c t i o n ( ) ;56 f o r ( i n t i = 0 ; i < board . g e t N C i t i e s ( ) ; i ++) {7 f o r ( i n t j = i + 1 ; j < board . g e t N C i t i e s ( ) ; j ++) {8 ProbTSPBoard newBoard = new ProbTSPBoard ( board . g e t N C i t i e s ( ) ,9 board . getPath ( ) , board . g e t D i s t s ( ) ) ;
1011 newBoard . s w a p C i t i e s ( i , j ) ;1213 i n t v = TSPHF . g e t H e u r i s t i c V a l u e ( newBoard ) ;14 S t r i n g S = ProbTSPBoard . INTERCAMBIO + ” ” + i + ” ” + j15 + ” Coste ( ” + v + ” ) −−−> ” + newBoard . t o S t r i n g ( ) ;1617 r e t V a l . add (new S u c c e s s o r ( S , newBoard ) ) ;18 }19 }2021 r e t u r n r e t V a l ;22 }
1 pub l i c L i s t g e t S u c c e s s o r s ( Object a S t a t e ) {2 A r r a y L i s t r e t V a l = new A r r a y L i s t ( ) ;3 ProbTSPBoard board = ( ProbTSPBoard ) a S t a t e ;4 P r o b T S P H e u r i s t i c F u n c t i o n TSPHF = new P r o b T S P H e u r i s t i c F u n c t i o n ( ) ;5 Random myRandom=new Random ( ) ;6 i n t i , j ;78 i=myRandom . n e x t I n t ( board . g e t N C i t i e s ( ) ) ;9
10 do{11 j=myRandom . n e x t I n t ( board . g e t N C i t i e s ( ) ) ;12 } wh i l e ( i==j ) ;131415 ProbTSPBoard newBoard = new ProbTSPBoard ( board . g e t N C i t i e s ( ) , board . getPath ( ) ,16 board . g e t D i s t s ( ) ) ;1718 newBoard . s w a p C i t i e s ( i , j ) ;1920 i n t v = TSPHF . g e t H e u r i s t i c V a l u e ( newBoard ) ;21 S t r i n g S = ProbTSPBoard . INTERCAMBIO + ” ” + i + ” ” + j22 + ” Coste ( ” + v + ” ) −−−> ”+ newBoard . t o S t r i n g ( ) ;2324 r e t V a l . add (new S u c c e s s o r ( S , newBoard ) ) ;2526 r e t u r n r e t V a l ;27 }
The AIMA java classes The classes of the algorithms
The classes of the search algorithms (informed)
aima.search.informed
AStarSearch, A* search, it has a parameter of type GraphSearch
IterativeDeepeningAStarSearch, IDA* search, it has no parametersHillClimbingSearch, Hill Climbing search, it has no parametersSimulatedAnnealingSearch, Simulated Annealing search, it has 4parameters:
Maximum number of iterations,Number of iterations for each temperature decreasing stepThe parameters k and λ that determine the behaviour of thetemperature function