28 #ifndef SHARK_EA_GRID_H 29 #define SHARK_EA_GRID_H 39 Hypercube() : m_size( 0. ),
41 m_isOccupied( false ) {
45 unsigned int m_noSolutions;
49 typedef std::map< std::size_t, Hypercube >::iterator iterator;
50 typedef std::map< std::size_t, Hypercube >::const_iterator const_iterator;
52 std::map< std::size_t, Hypercube > m_denseGrid;
53 iterator m_mostOccupiedHypercube;
58 unsigned int m_noBisections;
64 RealVector m_lowerBounds;
69 RealVector m_upperBounds;
82 AdaptiveGrid(
unsigned int noBisections = 0,
unsigned int noObjectives = 0 ) {
83 reset( noBisections, noObjectives );
86 void reset(
unsigned int noBisections,
unsigned int noObjectives ) {
87 m_noBisections = noBisections;
89 m_lowerBounds = RealVector( noObjectives, std::numeric_limits<double>::max() );
90 m_upperBounds = RealVector( noObjectives, -std::numeric_limits<double>::max() );
92 m_extents = RealVector( noObjectives, 0 );
96 m_mostOccupiedHypercube = m_denseGrid.end();
104 template<
typename Set>
105 void updateLimits(
const Set & solutionSet ) {
107 m_lowerBounds = RealVector( m_lowerBounds.size(), std::numeric_limits<double>::max() );
108 m_upperBounds = RealVector( m_lowerBounds.size(), -std::numeric_limits<double>::max() );
110 typename Set::const_iterator it;
111 for( it = solutionSet.begin(); it != solutionSet.end(); ++it ) {
113 for(
unsigned int i = 0; i < it->fitness( shark::tag::PenalizedFitness() ).size(); i++ ) {
114 m_lowerBounds( i ) = std::min( m_lowerBounds( i ), it->fitness( shark::tag::PenalizedFitness() )[i] );
115 m_upperBounds( i ) = std::max( m_upperBounds( i ), it->fitness( shark::tag::PenalizedFitness() )[i] );
118 m_extents = m_upperBounds - m_lowerBounds;
127 template<
typename Set>
128 void addSolutionSet(
const Set & solutionSet) {
129 m_mostOccupiedHypercube = m_denseGrid.begin();
131 typename Set::const_iterator it;
132 for( it = solutionSet.begin(); it != solutionSet.end(); ++it ) {
133 itt = location( *it );
136 throw(
shark::Exception(
"AdaptiveGrid::addSolutionSet: The grid limits need to be calculated before." ) );
154 template<
typename Set>
155 void updateGrid(
const Set & solutionSet ){
158 updateLimits( solutionSet );
161 m_mostOccupiedHypercube = m_denseGrid.end();
164 addSolutionSet(solutionSet);
176 template<
typename Solution,
typename Set>
177 void updateGrid(
const Solution & solution,
const Set & solutionSet ) {
179 int it = location( solution );
182 updateLimits( solutionSet );
185 for( std::size_t i = 0; i < solution.fitness( shark::tag::PenalizedFitness() ).size(); i++ ){
186 m_lowerBounds( i ) = std::min( m_lowerBounds( i ), solution.fitness( shark::tag::PenalizedFitness() )[i] );
187 m_upperBounds( i ) = std::max( m_upperBounds( i ), solution.fitness( shark::tag::PenalizedFitness() )[i] );
190 m_extents = m_upperBounds - m_lowerBounds;
193 m_mostOccupiedHypercube = m_denseGrid.end();
196 addSolutionSet(solutionSet);
205 template<
typename Solution>
206 int location(
const Solution & solution )
const {
208 std::vector< std::size_t > positions( solution.fitness( shark::tag::PenalizedFitness() ).size(), 0 );
211 for( std::size_t obj = 0; obj < solution.fitness( shark::tag::PenalizedFitness() ).size(); obj++ ) {
213 if( solution.fitness( shark::tag::PenalizedFitness() )[ obj ] > m_upperBounds( obj ) )
215 if( solution.fitness( shark::tag::PenalizedFitness() )[ obj ] < m_lowerBounds( obj ) )
218 if( solution.fitness( shark::tag::PenalizedFitness() )[ obj ] == m_lowerBounds[obj] ) {
219 positions[ obj ] = 0;
223 if( solution.fitness( shark::tag::PenalizedFitness() )[ obj ] == m_upperBounds[ obj ] ) {
224 positions[obj] =
static_cast<std::size_t
>( (::pow( 2.0, static_cast<double>( m_noBisections ) ) )-1 );
229 double tmpSize = m_extents( obj );
230 double value = solution.fitness( shark::tag::PenalizedFitness() )[ obj ];
231 double account = m_lowerBounds( obj );
232 std::size_t ranges =
static_cast<std::size_t
>( (::pow( 2.0, static_cast<double>( m_noBisections ) ) ) );
234 for(
unsigned int b = 0; b < m_noBisections; b++ ) {
237 if( value > (account + tmpSize) ) {
238 positions[ obj ] += ranges;
245 std::size_t location = 0;
246 for(
unsigned int obj = 0; obj < solution.fitness( shark::tag::PenalizedFitness() ).size(); obj++ ) {
247 location += positions[obj] *
static_cast<std::size_t
>( (::pow( 2.0, obj * static_cast<double>( m_noBisections ) ) ) );
256 iterator mostPopulated() {
257 return( m_mostOccupiedHypercube );
265 unsigned int locationDensity( std::size_t idx ) {
266 iterator it = m_denseGrid.find( idx );
267 if( it == m_denseGrid.end() )
269 return( it->second.m_noSolutions );
276 int removeSolution( std::size_t location ) {
277 iterator it = m_denseGrid.find( location );
278 if( it == m_denseGrid.end() )
281 it->second.m_noSolutions--;
283 if( m_mostOccupiedHypercube == it )
284 for( iterator itt = m_denseGrid.begin(); itt != m_denseGrid.end(); ++itt )
285 if( itt->second.m_noSolutions > m_mostOccupiedHypercube->second.m_noSolutions )
286 m_mostOccupiedHypercube = itt;
289 if( it->second.m_noSolutions == 0 ) {
290 m_denseGrid.erase( it );
295 return( it->second.m_noSolutions );
302 void addSolution( std::size_t location ) {
303 Hypercube &
h = m_denseGrid[location];
306 if( m_mostOccupiedHypercube != m_denseGrid.end() ) {
307 if( h.m_noSolutions > m_mostOccupiedHypercube->second.m_noSolutions ) {
308 m_mostOccupiedHypercube = m_denseGrid.find( location );
311 m_mostOccupiedHypercube = m_denseGrid.find( location );
315 if( h.m_noSolutions == 1 )
323 unsigned int noBisections() {
324 return( m_noBisections );
363 std::size_t calculateOccupied() {
364 return( m_denseGrid.size() );
371 std::size_t occupiedHypercubes(){
372 return( m_denseGrid.size() );