The Fib multimedia system
Fib is a system for storing multimedia data (like images or films).
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cOperatorFitnessAlgorithmBasic.h
Go to the documentation of this file.
1 /**
2  * @file cOperatorFitnessAlgorithmBasic
3  * file name: cOperatorFitnessAlgorithmBasic.h
4  * @author Betti Oesterholz
5  * @date 22.03.2010
6  * @mail webmaster@BioKom.info
7  *
8  * System: C++
9  *
10  * This header specifies a class for gernerating the fitness for
11  * operators.
12  * Copyright (C) @c GPL3 2010 Betti Oesterholz
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License (GPL) as
16  * published by the Free Software Foundation, either version 3 of the
17  * License, or any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  *
27  *
28  * This header specifies a class for gernerating the fitness for operators.
29  * The fitness of operators is evalued by considering the information about
30  * the individuals the operation has created.
31  *
32  * Importent values and functions for the evaluation of the operation
33  * fitness of this class are:
34  * - NF(): This function normalizes the fitnessvalues of individuals,
35  * so it's greater 1; ( F_min = minimal individual fitness value
36  * = cObjectFitnessAlgorithm::getWorstCaseFitness() )
37  * N( F_I ) = max( 1 , (F_I - F_min) )
38  * - V_i: The improvement which the operator has created with an
39  * individual. This is the ratio of the normalized fitness F_N of
40  * the created individual diveded by the best fitness F_B of a
41  * individual at the creation time. V_i = NF( F_N ) / NF( F_B )
42  * - B_ins: The sum of all fitnessimprovments the operator has created.
43  * For every V_i value which is greater 1, the value (V_i - 1) is added.
44  * - A_ins: The cost sum the operator has needed. This is the sum of
45  * all cost (evalued with the benchmark value) operations of the
46  * operator have needed.
47  * - F_ins: The whool fitness. This is the ratio of the sum of all
48  * fitnessimprovments B_ins diveded by cost sum the operator has
49  * needed A_ins. F_ins = B_ins / A_ins
50  * - A_P: The cost sum the operator has needed for improvments. This
51  * is the sum of all cost (evalued with the benchmark value) operations
52  * of the operator have needed for generating a improved individual
53  * (an individual which is better as the best individual in the
54  * algorithm). This means the operator cost is added, if V_i is
55  * greater 1.
56  * - F_P: The improvment fitness. This is the ratio of the sum of all
57  * fitnessimprovments B_ins diveded by cost sum the operator has
58  * needed for inprovments A_P. F_P = B_ins / A_P
59  * - F: The fitness of the operator. This is the sum combination of
60  * the whool fitness F_ins multiplyed by the weight factor A and the
61  * improvment fitness F_P multiplyed by the weight factor B.
62  * F = A * F_ins + B * F_P
63  * The standardvalues for the weights A and B is 0.5 .
64  *
65  * The fitness values of th operators are stored persistent in an file in
66  * the xml -format. This file is loaded when this object is created and
67  * written if the object destroyed.
68  *
69  * @see cOperatorFitnessAlgorithmBasic
70  * @see cOperator
71  * @see cEnviroment
72  * @see cChoosOperator
73  */
74 /*
75 History:
76 22.03.2010 Oesterholz created
77 */
78 
79 
80 #ifndef ___C_OPERATION_FITNESS_ALGORITHM_BASIC_H__
81 #define ___C_OPERATION_FITNESS_ALGORITHM_BASIC_H__
82 
83 #include "version.h"
84 
86 #include "cIndividualInfo.h"
87 
88 #include "tinyxml.h"
89 
90 #include <string>
91 #include <map>
92 #include <iostream>
93 
94 using namespace std;
95 
96 namespace enviroment{
97 
98 
100 protected:
101 
102  /**
103  * The factor A for the whool fitness.
104  */
106 
107  /**
108  * The factor B for the improvment fitnesssum.
109  */
111 
112  /**
113  * The structur for the operator fitness values
114  * The fields are:
115  * - dB_ins: The sum of all fitnessimprovments the operator has created.
116  * For every V_i value which is greater 1, the value (V_i - 1) is added.
117  * - dA_ins: The cost sum the operator has needed. This is the sum of
118  * all cost (evalued with the benchmark value) operations of the
119  * operator have needed.
120  * - dF_ins: The whool fitness. This is the ratio of the sum of all
121  * fitnessimprovments B_ins diveded by cost sum the operator has
122  * needed A_ins. dF_ins = dB_ins / dA_ins
123  * - dA_p: The cost sum the operator has needed for improvments. This
124  * is the sum of all cost (evalued with the benchmark value) operations
125  * of the operator have needed for generating a improved individual
126  * (an individual which is better as the best individual in the
127  * algorithm). This means the operator cost ist added, if V_i is
128  * greater 1.
129  * - dF_p: The improvment fitness. This is the ratio of the sum of all
130  * fitnessimprovments B_ins diveded by cost sum the operator has
131  * needed for inprovments A_P. dF_p = dB_ins / dA_p
132  * - dF: The fitness of the operator. This is the sum combination of
133  * the whool fitness F_ins multiplyed by the weight factor A and the
134  * improvment fitness F_P multiplyed by the weight factor B.
135  * dF = A * dF_ins + B * dF_p
136  */
137  class cOperatorFitness;
138 
139  /**
140  * The operator fitnessvalues which have been loaded from a file.
141  *
142  * The key is the operatorname.
143  */
144  map< string, cOperatorFitness > mapLoadedOperatorFitness;
145 
146  /**
147  * The path to the file in which the operation data is and/or should
148  * be stored.
149  */
151 
152  /**
153  * The operator fitnessvalues which this object has evalued and loaded.
154  *
155  * The key is the operatorname.
156  */
157  map< string, cOperatorFitness > mapPastOperatorFitness;
158 
159  /**
160  * The summ of all fitnessvalues
161  */
162  double dFitnessSum;
163 
164  /**
165  * This list contains all operators with ther fitness values
166  * in an list. It is used for a faster evaluation of the
167  * getFitness() method.
168  *
169  * @see getFitness()
170  */
171  list< pair< const cOperation*, double > > liOperatorFitness;
172 
173 public:
174 
175  /**
176  * Constructor of the class cOperatorFitnessAlgorithmBasic.
177  *
178  * @param dInFaktorAllFitnessPart the factor A for the whool fitness
179  * @param dInFaktorPositivFitnessPart the factor B for the increased fitnesssum
180  * @param szOldOperationDataPath the path wher the operator fitness data is stored;
181  * if "" (standardvalue) the path is "./cOperatorFitnessAlgorithmBasic.xml";
182  * if the path ends with an '/', it is a folder in which the the operator
183  * fitness data should be stored into an file with the name
184  * "cOperatorFitnessAlgorithmBasic.xml"
185  */
186  cOperatorFitnessAlgorithmBasic( double dInFaktorAllFitnessPart=0.5,
187  double dInFaktorPositivFitnessPart=0.5,
188  const string szOldOperationDataPath="" );
189 
190  /**
191  * copyconstructor of the class cOperatorFitnessAlgorithmBasic.
192  *
193  * @param opFitnessAlgorithmBasic the cOperatorFitnessAlgorithmBasic to copy
194  */
195  cOperatorFitnessAlgorithmBasic( const cOperatorFitnessAlgorithmBasic & opFitnessAlgorithmBasic );
196 
197 
198  /**
199  * Destructor of the class cOperatorFitnessAlgorithmBasic.
200  */
202 
203  /**
204  * This method evalues the fitness of the given operator operation new.
205  *
206  * @param operation the operator for which the fitness is to be evalued new
207  * @param liCreatedIndividualInfos a list with all informations about
208  * all individuals the operator has created
209  * @return the new evalued fitness of the operation
210  */
211  virtual double evalueFitness( const cOperation & operation,
212  const list<cIndividualInfo> &liCreatedIndividualInfos );
213 
214 
215  /**
216  * This method updates the fitness of the given operator operation.
217  * For this update the information this cOperatorFitnessAlgorithmBasic
218  * object knows about the operator and the given
219  * createdIndividualInfo is considered to evalue the new fitness.
220  *
221  * @param operation the operator, for which the fitness is to be updated
222  * @param createdIndividualInfo the information about an individual
223  * the operator has created and which is not jet known by this
224  * cOperatorFitnessAlgorithmBasic object
225  * @return the new evalued fitness of the operation
226  */
227  virtual double updateFitness( const cOperation & operation,
228  const cIndividualInfo &createdIndividualInfo );
229 
230  /**
231  * This method updates the fitness of the given operator operation.
232  * For this update the information this cOperatorFitnessAlgorithmBasic
233  * object knows about the operator and the given information in
234  * liCreatedIndividualInfos is considered to evalue the new fitness.
235  *
236  * @param operation the operator, for which the fitness is to be updated
237  * @param liCreatedIndividualInfos a list with the information about
238  * the individuals the operator has created and which are not jet
239  * known by this cOperatorFitnessAlgorithmBasic object
240  * @return the new evalued fitness of the operation
241  */
242  virtual double updateFitness( const cOperation & operation,
243  const list<cIndividualInfo> &liCreatedIndividualInfos );
244 
245  /**
246  * This method returns the fitness of the given operator operation.
247  *
248  * @param operation the operator for which the fitness is to be returned
249  * @return the fitness of the operation
250  */
251  virtual double getFitness( const cOperation & operation );
252 
253  /**
254  * This method returns the fitness of all operators.
255  *
256  * @return a list with all possible operators with ther fitness
257  */
258  virtual list< pair< const cOperation*, double > > getFitness();
259 
260  /**
261  * @return the class name of this object
262  */
263  virtual string getClassName() const;
264 
265  /**
266  * @return the minimal fitness which this object can evalue
267  */
268  virtual double getMinFitness() const;
269 
270  /**
271  * @return the sum of all fitnessvalues of all operators
272  */
273  virtual double getFitnessSum() const;
274 
275  /**
276  * @see dFaktorAllFitnessPart
277  * @return the factor A for the whool fitness
278  */
279  double getFaktorAllFitnessPart() const;
280 
281  /**
282  * @see dFaktorPositivFitnessPart
283  * @return the factor B for the improvment fitnesssum
284  */
285  double getFaktorPositivFitnessPart() const;
286 
287 
288 protected:
289 
290  /**
291  * it's greater 1:
292  * N( dFitness ) = max( 1 , ( dFitness - dMinFitness ) )
293  *
294  * @param dFitness the fitnessvalue of an individual to normalize
295  * @param dMinFitness the minimal fitness; evalue with
296  * cObjectFitnessAlgorithm->getWorstCaseFitness()->getFitness()
297  * @return the normalized individual fitness
298  */
299  double normalizeIndividualFitness( double dFitness, double dMinFitness=0.0 ) const;
300 
301  /**
302  * This function reevalues operation fitness opFitness with the values
303  * of liCreatedIndividualInfos.
304  *
305  * Important values evalued internal:
306  * - V_i: The improvement which the operation has created with an
307  * individual from the list liCreatedIndividualInfos.
308  * This is the ratio of the normalized fitness F_N of
309  * the created individual diveded by the best fitness F_B of a
310  * individual at the creation time. V_i = NF( F_N ) / NF( F_B )
311  *
312  * Values of opFitness reevalued:
313  * - dB_ins: The sum of all fitnessimprovments the operator has created.
314  * For every V_i value which is greater 1, the values (V_i - 1)
315  * are added to the old opFitness.dB_ins value.
316  * - dA_ins: The cost sum the operator has needed. This is the sum of
317  * all cost (evalued with the benchmark value) from the
318  * list liCreatedIndividualInfos added to the opFitness.dA_ins value.
319  * - dF_ins: The whool fitness. This is the ratio of the sum of all
320  * fitnessimprovments B_ins diveded by cost sum the operation has
321  * needed A_ins. dF_ins = dB_ins / dA_ins
322  * - dA_p: The cost sum the operator has needed for improvments. This
323  * is the sum of all cost (evalued with the benchmark value)
324  * operations have needed for generating a improved individual
325  * (an individual which is better as the best individual in the
326  * algorithm, tihs means which V_i is greater 1) from the list
327  * liCreatedIndividualInfos added to the opFitness.dA_p value.
328  * - dF_p: The improvment fitness. This is the ratio of the sum of all
329  * fitnessimprovments B_ins diveded by cost sum the operator has
330  * needed for inprovments A_P. dF_p = dB_ins / dA_p
331  * - dF: The fitness of the operator. This is the sum combination of
332  * the whool fitness F_ins multiplyed by the weight factor A and the
333  * improvment fitness F_P multiplyed by the weight factor B.
334  * F = A * dF_ins + B * dF_p
335  *
336  * @see cOperatorFitness
337  * @see normalizeIndividualFitness()
338  * @param opFitness the operator fitness values for an operator
339  * @param liCreatedIndividualInfos the information about individual the
340  * operator to opFitness had created, but which are not jet
341  * included in opFitness
342  * @return the reevalued operator fitness
343  */
344  cOperatorFitness updateFitness( const cOperatorFitness & opFitness,
345  const list<cIndividualInfo> & liCreatedIndividualInfos ) const;
346 
347  /**
348  * This function stores the operator fitness data in xml -format into
349  * the given stream.
350  *
351  * Example Xml -structur:
352  ** <operatorFitness algorithm="cOperatorFitnessAlgorithmBasic">
353  ** <operator name="operator1" b_ins="1" a_ins="100" f_ins="0.01" a_p="10" f_p="0.1"/>
354  ** <operator name="operator2" b_ins="2" a_ins="100" f_ins="0.02" a_p="5" f_p="0.4"/>
355  ** <operatorFitness/>
356  *
357  * @see restoreXmlOperationsData()
358  * @param oStream the stream wher the operator data is to store to
359  * @param mapOperationFitness the operator fitness data to store
360  * @return true if this operation fitness data is stored, else false
361  */
362  bool storeXmlOperationsData( ostream & oStream,
363  const map< string, cOperatorFitness > & mapOperationFitness ) const;
364 
365  /**
366  * This function restores the operator fitness data from the given stream.
367  * In the stream the operator fitness data should be writen in the
368  * xml -format, like storeXmlOperationsData() stores it.
369  *
370  * @see storeXmlOperationsData()
371  * @param iStream the stream where the operator fitness data is stored in
372  * @param outStatus An reverence to an integervalue where the errorvalue
373  * can be stored to.
374  * possible errorvalues are:
375  * - 0 loading successful
376  * - -1 loading error, invalid stream
377  * - -2 loading error, invalid data in stream
378  * - 1 loading warning, invalid data in stream, error could be corrected
379  * - 2 loading warning, invalid data in stream, maybe the loaded
380  * data is wrong
381  * @return the restored operator fitness data
382  */
383  map< string, cOperatorFitness > restoreXmlOperatorData(
384  istream & iStream, int & outStatus ) const;
385 
386 
387  /**
388  * This function restores the operator fitness data from the given
389  * TinyXml node.
390  * In the TinyXml node the operator fitness data should be stored in
391  * the xml -format, like storeXmlOperationsData() stores it.
392  *
393  * @see storeXmlOperationsData()
394  * @param pXmlNode a pointer to the TinyXml node the operator fitness
395  * data is stored in
396  * @param outStatus An reverence to an integervalue where the errorvalue
397  * can be stored to.
398  * possible errorvalues are:
399  * - 0 loading successful
400  * - -1 loading error, invalid stream
401  * - -2 loading error, invalid data in stream
402  * - 1 loading warning, invalid data in stream, error could be corrected
403  * - 2 loading warning, invalid data in stream, maybe the loaded
404  * data is wrong
405  * @return the restored operator fitness data
406  */
407  map< string, cOperatorFitness > restoreXmlOperatorData(
408  const TiXmlNode * pXmlNode, int & outStatus ) const;
409 
410 
411  /**
412  * This method clones this object.
413  *
414  * @return a clone of this object
415  */
416  virtual cOperatorFitnessAlgorithmBasic * clone() const;
417 
418 
419 };//end class cOperatorFitnessAlgorithmBasic
420 
421 
422 };//end namespace enviroment
423 
424 #endif //___C_OPERATION_FITNESS_ALGORITHM_BASIC_H__
425 
426 
427 
428 
429 
430 
431