/*
* Copyright (c) 2006 Thomas Weise
*
* E-Mail : tweise@gmx.de
* Creation Date : 2006-03-30 14:21:55
* Original Filename: org.dgpf.search.api.SearchStateBag.java
* Version : 2.0.0
* Last modification: 2006-05-08
* by: Thomas Weise
*
* License : GNU LESSER GENERAL PUBLIC LICENSE
* Version 2.1, February 1999
* You should have received a copy of this license along
* with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA or download the license under
* http://www.gnu.org/copyleft/lesser.html.
*
* Warranty : This software is provided "as is" without any
* warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose.
* See the Gnu Lesser General Public License for more
* details.
*/
package org.dgpf.search.api;
import java.io.Serializable;
import org.sfc.math.Mathematics;
/**
* The search state bag contains the search state. It is the only instance
* able to access the data of the SearchState
. You should
* place variables like the statistic bags into instances of
* SearchStateBag
while putting the statistic info records
* into SearchState
. This way you prevent them from being
* serialized/cloned/copied/accessed whenever this is done to a genetic
* state.
*
* @param The sort of genotype used to represent individuals.
* This must be a serializable type.
*
* @see SearchState
* @author Thomas Weise
*/
public class SearchStateBag
implements Serializable
{
/**
* The serial version uid.
*/
private static final long serialVersionUID = 1;
/**
* The internally hosted search state instance.
*/
final SearchState m_state ;
/**
* Create a new search state bag.
* @param p_ff_count The count of fitness functions evaluated by this
* search.
*/
protected SearchStateBag (final int p_ff_count)
{
super();
this.init(p_ff_count);
this.m_state = this.create_state(p_ff_count);
}
/**
* This method allows you to perform additional initialization before
* the hosted search state will be created.
* @param p_ff_count The count of fitness functions evaluated by this
* search.
*/
protected void init (final int p_ff_count)
{
//
}
/**
* This method is used internally to create a search state instance that
* can be hosted by this bag.
* @param p_ff_count The count of fitness functions evaluated by this
* search.
* @return The new search state.
*/
protected SearchState create_state(final int p_ff_count)
{
return new SearchState(p_ff_count);
}
/**
* Check wether the given element is a new supremum considering the given
* fitness function.
* @param p_individual The individual to check.
* @param p_function The function regarding to check the individual
* against.
*/
public final void check_best(final Individual p_individual,
final int p_function)
{
double l_dist;
Individual l_gt;
SearchState l_s;
l_s = this.m_state;
l_gt = l_s.m_bests[p_function];
l_dist = (p_individual.get_fitness(p_function) -
l_gt.get_fitness(p_function));
if(!(Mathematics.is_number(l_dist))) l_dist = -1.0d;
l_s.m_improvements[p_function] = l_dist;
if(l_dist >= 0.0d)
{
l_gt.assign(p_individual);
}
}
/**
* Check wether the given element is a new supremum considering the
* search's comparator.
* @param p_individual The individual to check.
* @param p_comp The search's comparator.
*/
final void check_best(final Individual p_individual,
final IndividualComparator p_comp)
{
Individual l_ig;
int l_i;
SearchState l_s;
l_s = this.m_state;
l_ig = l_s.m_best_2;
l_i = p_comp.compare(l_ig, p_individual);
if(l_i >= 0)
{
if(l_i > 0)
{
l_ig.assign(p_individual);
l_s.m_improvement_2 = 1;
l_ig = l_s.m_best;
l_i = p_comp.compare(l_ig, p_individual);
if(l_i >= 0)
{
if(l_i > 0)
{
l_ig.assign(p_individual);
l_s.m_improvement = 1;
}
else if(l_s.m_improvement < 0) l_s.m_improvement = 0;
}
}
else if(l_s.m_improvement_2 < 0) l_s.m_improvement_2 = 0;
}
}
}