001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.algorithms.gp;
021    
022    import org.dllearner.core.owl.Description;
023    import org.dllearner.learningproblems.ScorePosNeg;
024    
025    /**
026     * This class represents a program, i.e. an individual.
027     * 
028     * @author Jens Lehmann
029     * 
030     */
031    public class Program {
032    
033            // public static int fitnessEvaluations = 0;
034    
035            private Description hypothesis;
036    
037            // private Concept extendedHypothesis;
038    
039            private Description adc;
040    
041            private ScorePosNeg score;
042    
043            // private Score scoreAdc;
044            
045            // private LearningProblem learningProblem;
046    
047            private double fitness;
048            
049            /**
050             * Create a new program.
051             * 
052             */
053            public Program(ScorePosNeg score, Description hypothesis) {
054                    this(score, hypothesis, null);
055            }
056    
057            public Program(ScorePosNeg score, Description hypothesis, Description adc) {
058                    // this.learningProblem = learningProblem;
059                    this.score = score;
060                    this.hypothesis = hypothesis;
061                    this.adc = adc;
062                    // TODO: es sind Prozent pro Längeneinheit, also ist hier die
063                    // Implementierung falsch !!
064                    // fitness = score.getScore() - hypothesis.getLength() * Config.percentPerLengthUnit;
065                    // => in getScore() ist jetzt schon der length penalty integriert
066                    fitness = score.getScoreValue();
067                    // fitnessEvaluations++;
068                    
069                    // System.out.println("new program: " + hypothesis);
070                    
071                    /*
072                    // falls R�ckgabetyp spezifiziert ist, dann muss hier der Baum
073                    // entsprechend ver�ndert werden
074                    if (!Config.returnType.equals("")) {
075                            // newRoot.addChild(new AtomicConcept(Config.returnType));
076                            // newRoot.addChild(hypothesis);
077                            Concept newRoot = new Conjunction(new AtomicConcept(Config.returnType),hypothesis);                     
078                            // parent wieder auf null setzen, damit es nicht inkonsistent wird
079                            // TODO: ist nicht wirklich elegant und auch inkompatibel mit
080                            // dem Hill-Climbing-Operator
081                            hypothesis.setParent(null);
082                            extendedHypothesis = newRoot;
083                    } else
084                            extendedHypothesis = hypothesis;
085    
086                    // fitness evaluation on program creation
087                    calculateFitness();
088                    */
089            }
090    
091            // nur aufrufen, wenn Programm ver�ndert wird und deshalb die Fitness neu
092            // berechnet werden muss
093            /*
094            public void calculateFitness() {
095                    if(Config.GP.adc)
096                            score = learningProblem.computeScore(extendedHypothesis, adc);
097                    else
098                            score = learningProblem.computeScore(extendedHypothesis);
099    
100                    fitness = score.getScore() - 0.1 * hypothesis.getConceptLength();
101                    
102                    if (Config.GP.adc)
103                            fitness -= 0.1 * adc.getConceptLength();
104    
105                    // zus�tzliche Bestrafung f�r sehr lange Definitionen
106                    if(hypothesis.getNumberOfNodes()>50)
107                            fitness -= 10;
108                    fitnessEvaluations++;
109            }
110            */
111    
112            /**
113             * Returns the previously calculated fitness of the program.
114             * 
115             * @return The fitness of the program.
116             */
117            public double getFitness() {
118                    return fitness;
119            }
120    
121            /**
122             * Returns the program tree corresponding to this program.
123             * 
124             * @return The program tree.
125             */
126            public Description getTree() {
127                    return hypothesis;
128            }
129    
130            public ScorePosNeg getScore() {
131                    return score;
132            }
133    
134            public Description getAdc() {
135                    return adc;
136            }
137    }