File: ga_optimization.txt

package info (click to toggle)
gamera 1:3.4.2+git20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (344 lines) | stat: -rw-r--r-- 10,823 bytes parent folder | download | duplicates (2)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
================================
Evolutionary Optimization Module
================================

Introduction
------------

Feature Selection, Weighting and Genetic Algorithms
'''''''''''''''''''''''''''''''''''''''''''''''''''

Feature selection is the technique of selecting a subset of good features
out of a larger featureset to obtain the features which are suitable
for the best possible classification. Feature weighting is a generalization
of feature selection with real-valued weights between [0,1] instead of the
binary values 0/1.

Due to the fact that feature selection and weighting are NP-hard optimization
problems, a complete and correct solution is not easy to determine. One way
to get an approximate solution is by using genetic algorithms. Even though
feature weighting is a generalization of feature selection and should thus
be able to produce better classification results, practice has shown that
feature selection produces better recognition rates in most cases. It is
thus recommended to use feature selection rather than feature weighting
[Bolten2012]_.

Genetic algorithms, proposed by Holland, are adaptive problem
solvers that find solutions based on the mechanics of natural selection and
natural genetics. For more information, see a genetic algorithms
text such as [Holland1975]_

How the optimization can be used after training the classifier is described in the `training
tutorial`__.

.. __: training_tutorial.html

Usage
-----

There are two ways to use the optimization:

- Graphical User Interface: `Biollante`__

- Python `Script API`__

These two approaches are equivalent, although due to the required runtime
the script API is recommended.

.. __: #user-interface-biollante
.. __: #script-usage


User Interface `Biollante`
''''''''''''''''''''''''''

  "Biollante is the result of an experiment in genetic
  engineering. The creature was created by splicing together the DNA
  of three organisms: a rose, a human female (Erica, daughter of the
  scientist), and Godzilla himself."

For our purposes, Biollante is a GUI for optimizing kNN classifiers using
genetic algorithms. A genetic algorithm is used to adjust the selection
and weight of each feature.

Overview
````````

To use Biollante from the Gamera console window select **File ->
Biollante**.  From the Biollante window select **File -> Open from XML** to
open a `Gamera XML file`__ containing training data or load with
**File -> Open existing classifier** an existing classifier from the gamera main
window. Once the training data has been loaded you can start and
stop the optimization process. After the optimizer has finished, the results
can be saved using **File -> Save to XML** or **File -> Save to XML as**.

Classifier specific settings are configurable through the **Classifier** menu.
With **Classifier -> Copy classifier** it is possible to create an
copy from the loaded classifier for the optimization process. The menu entry
**Classifier -> Classifier to Gamera-GUI** allows the usage of the
optimized classifier in the Gamera main window. Through **Classifier ->
Load settings from XML** an settings xml file will be loaded and applied.

The used feature set can be modified with **Classifier -> Change feature set**.
With the menu entries **Classifier -> Reset selections** and
**Classifier -> Reset weights** it is possible to discard already choosen
optimizations.

.. __: gamera_xml.html

Settings Page
`````````````

  .. image:: images/biollante_settings.png 

The Biollante settings page offers the control over the evolutionary
optimization process. It is possible to select which kind of optimization is applied:
selection or weighting.

Additionally, the basic parameters **population size**, **crossover rate** and
**mutation rate** can be adjusted. In the optional available
**Expert GA settings** it is possible to select and fine-tune each used
genetic algorithms operator (for further information see below). These settings
are predefined with useful default values.


The crossover rate and mutation rate are expressed in percentages.

Status Page
```````````

  .. image:: images/biollante_status_sub.png

The Biollante status page displays information about the current state
of the optimization and allows to stop the process manually. 

**Current Status**
    Flag which indicates whether the optimization is currently
    running or not.

**Initial leave one out rate**
    The initial leave-one-out performance of the classifier.

**Elapsed time**
    Clock which measures how much time elapsed since optimization start.

**Current generation**
    The current number of generations (of the genetic algorithm).

**Best leave one out rate**
    The best leave-one-out performance achieved so far.

**GA statistics**
    Text field which shows statistical information about the
    optimization process like the number of fitness evaluations, average
    and stdev of fitness values within each generation.

Result Page
```````````

  .. image:: images/biollante_results_sub.png

The result page graphically displays the best selections or weights in a "bar graph" style.

Script Usage
''''''''''''

The optimization module is also fully operable from Python
directly.

The following listing clarifies the usage of the optimization module. All
functions are explained in the `function reference`__.

.. __: #function-reference

.. code:: Python

    from gamera.core import *
    init_gamera()

    from gamera import knn, knnga

    if __name__ == "__main__":
        # Load the trainingdata and create a classifier object
        classifier = knn.kNNNonInteractive("classifiers/traindata.xml",
                                           features = 'all',
                                           normalize = False)

        # Create and configure the GA objects and settings
        baseSettings = knnga.GABaseSetting()
        baseSettings.opMode = knnga.GA_SELECTION
        baseSettings.popSize = 75
        baseSettings.crossRate = 0.95
        baseSettings.mutRate = 0.05

        selection = knnga.GASelection()
        selection.setRoulettWheelScaled(2.0)

        crossover = knnga.GACrossover()
        crossover.setUniformCrossover(0.5)

        mutation = knnga.GAMutation()
        mutation.setSwapMutation()
        mutation.setBinaryMutation(0.5, False)

        replacement = knnga.GAReplacement()
        replacement.setSSGAdetTournament(3)

        stop = knnga.GAStopCriteria()
        stop.setSteadyStateStop(40,10)

        parallel = knnga.GAParallelization()
        parallel.mode = True
        parallel.thredNum = 4

        # Combine each setting object into one main object
        ga = knnga.GAOptimization(classifier, baseSettings, selection,
                                  crossover, mutation, replacement,
                                  stop, parallel)

        # Run the optimization
        ga.startCalculation()

        # Lets have a look on the reported optimization progress and the results
        print "Generation statistics", ga.monitorString
        print "Selections:", classifier.get_selections_by_features()

It is recommended to use the settings from the *Biollante GUI*.

Function Reference
''''''''''''''''''

The optimization module follows a highly object oriented design.

Capsle Object `GAOptimization`
``````````````````````````````

.. docstring:: gamera.knnga GAOptimization

Getter
~~~~~~

.. docstring:: gamera.knnga GAOptimization.status
.. docstring:: gamera.knnga GAOptimization.generation
.. docstring:: gamera.knnga GAOptimization.bestFitness
.. docstring:: gamera.knnga GAOptimization.monitorString
.. docstring:: gamera.knnga GAOptimization.bestIndiString


Functions
~~~~~~~~~

.. docstring:: gamera.knnga GAOptimization.startCalculation
.. docstring:: gamera.knnga GAOptimization.stopCalculation


Base Settings
`````````````

.. docstring:: gamera.knnga GABaseSetting

Getter and Setter
~~~~~~~~~~~~~~~~~

.. docstring:: gamera.knnga GABaseSetting.opMode
.. docstring:: gamera.knnga GABaseSetting.popSize
.. docstring:: gamera.knnga GABaseSetting.crossRate
.. docstring:: gamera.knnga GABaseSetting.mutRate

Individuals Selection Settings
``````````````````````````````

.. docstring:: gamera.knnga GASelection

Functions
~~~~~~~~~

.. docstring:: gamera.knnga GASelection.setRoulettWheel
.. docstring:: gamera.knnga GASelection.setRoulettWheelScaled
.. docstring:: gamera.knnga GASelection.setStochUniSampling
.. docstring:: gamera.knnga GASelection.setRankSelection
.. docstring:: gamera.knnga GASelection.setTournamentSelection
.. docstring:: gamera.knnga GASelection.setRandomSelection

Crossover Settings
``````````````````

.. docstring:: gamera.knnga GACrossover

Functions
~~~~~~~~~

.. docstring:: gamera.knnga GACrossover.setNPointCrossover
.. docstring:: gamera.knnga GACrossover.setUniformCrossover
.. docstring:: gamera.knnga GACrossover.setSBXcrossover
.. docstring:: gamera.knnga GACrossover.setSegmentCrossover
.. docstring:: gamera.knnga GACrossover.setHypercubeCrossover

Mutation
````````

.. docstring:: gamera.knnga GAMutation

Functions
~~~~~~~~~

.. docstring:: gamera.knnga GAMutation.setShiftMutation
.. docstring:: gamera.knnga GAMutation.setSwapMutation
.. docstring:: gamera.knnga GAMutation.setInversionMutation
.. docstring:: gamera.knnga GAMutation.setBinaryMutation
.. docstring:: gamera.knnga GAMutation.setGaussMutation

Replacement
```````````

.. docstring:: gamera.knnga GAReplacement

Functions
~~~~~~~~~

.. docstring:: gamera.knnga GAReplacement.setGenerationalReplacement
.. docstring:: gamera.knnga GAReplacement.setSSGAworse
.. docstring:: gamera.knnga GAReplacement.setSSGAdetTournament


Stop Criteria
`````````````

.. docstring:: gamera.knnga GAStopCriteria

Functions
~~~~~~~~~

.. docstring:: gamera.knnga GAStopCriteria.setBestFitnessStop
.. docstring:: gamera.knnga GAStopCriteria.setMaxGenerations
.. docstring:: gamera.knnga GAStopCriteria.setMaxFitnessEvals
.. docstring:: gamera.knnga GAStopCriteria.setSteadyStateStop


Parallelization
```````````````

.. docstring:: gamera.knnga GAParallelization

Getter and Setter
~~~~~~~~~~~~~~~~~

.. docstring:: gamera.knnga GAParallelization.mode
.. docstring:: gamera.knnga GAParallelization.thredNum


References
----------

.. [Holland1975] Holland, J. H. (1975). *Adaptation in natural and
   artifical systems.*  University of Michigan Press, Ann Arbor.

.. [EOdev] Keijzer, M., Merelo, J. J., Romero, G., & Schoenauer, M. (2002).
   `Evolving objects: A general purpose evolutionary computation library`__.
   Artificial Evolution, 2310, pp. 829–888.

.. [Bolten2012] Bolten, T. (2012) *Genetische Algorithmen zur Auswahl und
   Gewichtung von Features in der Mustererkennung.* Master thesis, Niederrhein
   University of Applied Sciences, Krefeld, Germany

.. __:  http://eodev.sourceforge.net/