genetic-programming
所属分类:人工智能/神经网络/深度学习
开发工具:Common Lisp
文件大小:24KB
下载次数:0
上传日期:2016-01-07 17:01:18
上 传 者:
sh-1993
说明: Common Lisp中的遗传编程
(Genetic programming in Common Lisp)
文件列表:
LICENSE (35147, 2016-01-08)
gene.lisp (49199, 2016-01-08)
genetic-programming.asd (334, 2016-01-08)
#+Title: Genetic Programming
#+Author: Graham Dobbins
A work in progress Common Lisp program facilitating genetic programming.
* Installation
** Requirements
* [[https://common-lisp.net/project/alexandria/][Alexandria]]
* [[http://lparallel.org][lparallel]]
** Quicklisp
Install with quicklisp by using the local projects feature.
Alexandria and lparallel will be installed automatically if not already present.
#+BEGIN_SRC sh
cd ~/quicklisp/local-projects/
git clone https://github.com/gambyte/genetic-programming.git
#+END_SRC
To use, start your favorite common-lisp implementation and:
#+BEGIN_SRC lisp
(ql:quickload :genetic-programming)
(in-package :genetic-programming)
#+END_SRC
* Documentation
** Examples
*** Genetic programming
First define a fitness function
#+BEGIN_SRC lisp
(defun difference-from-parabola (x)
(let ((test-function (first x)) ;Extract the function
(avg 0))
(dotimes (i 10 (/ avg i))
(incf avg
(eval ;Evaluate after splicing in the function
`(let ((x (random 100))) ;Bind the variables used in the function
(- (abs ;bigger is fitter
(-
(+ (expt x 2) (* 5 x) 3) ;The target parabola
,test-function)))))))))
#+END_SRC
Next decide what functions, variables, and constants can be used as building blocks
#+BEGIN_SRC lisp
(defvar *functions* (make-functions :function-list '(+ - *) :arg-num-list '(2 2 2)))
(defvar *terminals* '(x 1 10))
#+END_SRC
Initialize and run
#+BEGIN_SRC lisp
(gene-prog :operation :initialize :init-functions *functions* :init-terminals *terminals*)
(gene-prog :operation :next-generations)
(gene-prog :operation :best-of-generation)
#+END_SRC
Sample output:
#+BEGIN_SRC lisp
(+ (+ (+ X (+ 10 (+ X (+ (* (+ 1 (+ X X)) 10) (* X 10))))) 10) (- 10 X))
#+END_SRC
Care must be taken to choose good base functions, variables, and constants.
Any lisp functions can be used, including user defined ones.
Currently no simplification of the result is performed.
Increasing the population size will typically give better results than increasing the number of generations.
*** Genetic Algorithm
The genetic-algorithm starts with a population of randomly generated sequences which are bred by splicing the fittest sequences in each generation together.
#+BEGIN_SRC lisp
(genetic-algorithm '(1 0) ;Terminals
50 ;Gene length
100 ;Population size
50 ;Number of generations
(lambda (x) (count 1 x)) ;Fitness function
)
#+END_SRC
yields
#+BEGIN_SRC lisp
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
#+END_SRC
The best results are found when the number of terminals is as small as possible:
#+BEGIN_SRC lisp
(genetic-algorithm '(1 0 2) 50 100 50 (lambda (x) (count 1 x)))
#+END_SRC
yields
#+BEGIN_SRC lisp
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
#+END_SRC
*** Simulated Annealing
Specify an initial value, an energy function, and a step function
#+BEGIN_SRC lisp
(simulated-annealing 1d2 ;Initial value
1d10 ;Initial Temperature
(lambda (x) ;Energy function, lower is better, min @ 0
(+ (expt (* (+ (sin x) x) (cos x)) 2) (expt x 2)))
(lambda (x) ;Step function
(+ x (random 2d0) -1d0)) ;Equal probability of direction
2.0d-10 ;Minimum temperature
1.0003d0)
#+END_SRC
and a minimum is found, not always the global min:
#+BEGIN_SRC lisp
1.119***43731265049d-5
#+END_SRC
The simulated-annealing function is sufficiently generic to allow for all manner of optimization problems:
#+BEGIN_SRC lisp
(simulated-annealing '() 1d10
(lambda (x)
(abs (- (length x) 5)))
(lambda (x)
(if (= 0 (random 2))
(cdr x)
(cons 'Z x))))
#+END_SRC
yields
#+BEGIN_SRC lisp
(Z Z Z Z Z)
#+END_SRC
* Contributing
Pull requests are welcome. Please report any issues or bugs.
* License
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
近期下载者:
相关文件:
收藏者: