Fix building and installing package.
[genetic.git] / include / fitness / fitness.h
diff --git a/include/fitness/fitness.h b/include/fitness/fitness.h
new file mode 100644 (file)
index 0000000..a975034
--- /dev/null
@@ -0,0 +1,80 @@
+#ifndef __FITNESS_FITNESS_H
+#define __FITNESS_FITNESS_H
+
+#include <map>
+#include <string>
+
+#include "chromosome.h"
+
+namespace genetic {
+    /**
+     * Base Fitness template class. It should be a base class for any custom
+     * fitness functions.
+     */
+    template < typename _Chromosome, typename _Value = double >
+    class Fitness {
+        template<typename> friend class Selection;
+    public:
+        /**
+         * Type representing Chromosome Gene
+         */
+        typedef typename _Chromosome::GeneType GeneType;
+
+        /**
+         * Value type returned by the Fitness function
+         */
+        typedef _Value ValueType;
+    protected:
+        /**
+         * Chromosome on which calculations are made
+         */
+        _Chromosome chromosome;
+
+        /**
+         * Calculations should be done here...
+         *
+         * @return Fitness value of the current Chromosome
+         */
+        virtual _Value do_calculate() = 0;
+
+    public:
+        /**
+         * Class constructor
+         */
+        Fitness() {}
+
+        /**
+         * Copy constructor
+         */
+        Fitness(_Chromosome& _chromosome)
+            : chromosome(_chromosome) {
+        }
+
+        /**
+         * Invokes calculations
+         *
+         * @return Fitness value of the current Chromosome
+         */
+        _Value calculate() {
+            return this->do_calculate();
+        }
+
+        /**
+         * Method used to pass additional arguments needed by the function to
+         * run correctly.
+         */
+        virtual void parseArguments(std::map<string, string>) { }
+
+        /**
+         * Method used to get additional arguments needed by the function to
+         * run correctly
+         *
+         * @return map containing additional arguments, empty if do not use any
+         */
+        virtual std::map<string, string> getArguments() {
+            return std::map<string, string>();
+        }
+    };
+}
+
+#endif /* __FITNESS_FITNESS_H */