001 /*
002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package de.kumpe.hadooptimizer.impl;
017
018 import static java.lang.String.*;
019
020 import java.util.Collection;
021
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024
025 import de.kumpe.hadooptimizer.EvaluationResult;
026 import de.kumpe.hadooptimizer.Halter;
027 import de.kumpe.hadooptimizer.Stoppable;
028
029 /**
030 * A {@link Halter} halting the evolution process when the evaluation has gone
031 * below a given level.
032 *
033 * @param <I>
034 * the individuals' type
035 *
036 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
037 */
038 public final class EvaluationLimitHalter<I> implements Halter<I>, Stoppable {
039 private static final long serialVersionUID = 1L;
040 private static final Log log = LogFactory
041 .getLog(EvaluationLimitHalter.class);
042
043 private final double limit;
044
045 private volatile boolean stop = false;
046
047 /**
048 * Creates a new {@link EvaluationLimitHalter} halting at the given
049 * evaluation value.
050 *
051 * @param limit
052 * the evaluation value to reach before halting
053 */
054 public EvaluationLimitHalter(final double limit) {
055 if (log.isDebugEnabled()) {
056 log.debug(format(
057 "Constructing new EvaluationLimitHalter which halts after a evaluation of %f is reached.",
058 limit));
059 }
060
061 this.limit = limit;
062 }
063
064 @Override
065 public boolean halt(final Collection<EvaluationResult<I>> evaluationResults) {
066 return stop
067 || limit >= evaluationResults.iterator().next().getEvaluation();
068 }
069
070 @Override
071 public void stop() {
072 stop = true;
073 }
074
075 @Override
076 public String toString() {
077 return "EvaluationLimitHalter [limit=" + limit + "]";
078 }
079 }