Skip to content

Commit

Permalink
Remove history from result class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Alfonso committed Dec 4, 2023
1 parent f7c24ac commit f3de31a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 36 deletions.
4 changes: 0 additions & 4 deletions include/CResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ class CResult

explicit CResult( const CTableauState& aInitialTableauState );

const history& GetHistory() const;
const CTableauState& GetTableauState() const;
const std::atomic_ulong& GetCounter() const;

void FindBest( const unsigned int& aNTries, const CTableau& aTableau, std::mt19937_64& aRNG, const bool aSpeed );

bool IsBetterResult( const history::size_type& aCountHits, const unsigned short& aScore ) const;

void SaveHistory( std::string_view aOutputFileName, const CTableau& aInitialTableau ) const;

private:
history mHistory;
CTableauState mTableauState;
std::mutex mMutex;
std::atomic_ulong mCounter;
Expand Down
34 changes: 2 additions & 32 deletions src/CResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ CResult::CResult( const CTableauState& aInitialTableauState ) :
{
}

const CResult::history& CResult::GetHistory() const
{
return mHistory;
}

const CTableauState& CResult::GetTableauState() const
{
return mTableauState;
Expand All @@ -29,46 +24,21 @@ void CResult::FindBest( const unsigned int& aNTries, const CTableau& aTableau, s
{
const auto initialTableauState = mTableauState;
const auto& targetPieces = aTableau.CountPieces() + ( aSpeed ? 0 : 1 );
for( ; mCounter < aNTries && mHistory.size() < targetPieces; mCounter++ ) // Iterate many times
for( ; mCounter < aNTries && mTableauState.GetHistory().size() < targetPieces; mCounter++ ) // Iterate many times
{
auto tableauState = initialTableauState;
history posHistory{ initialTableauState.GetCurrentPosition().value_or( tableauState.SetCurrentPositionAtRandom( aTableau, aRNG ) ) };
while( const auto& nextPosition = tableauState.Move( aTableau, aRNG ) )
posHistory.emplace_back( *nextPosition );
std::lock_guard lock( mMutex );
if( IsBetterResult( posHistory.size(), tableauState.GetScore() ) ) // New record!
{
mHistory = std::move( posHistory );
mTableauState = std::move( tableauState );
}
}
}

bool CResult::IsBetterResult( const history::size_type& aCountHits, const unsigned short& aScore ) const
{
return ( aCountHits > mHistory.size() ) || ( aCountHits == mHistory.size() && aScore > mTableauState.GetScore() );
}

void CResult::SaveHistory( std::string_view aOutputFileName, const CTableau& aInitialTableau ) const
{
std::ofstream outfile;
outfile.open( aOutputFileName.data() );
outfile << "Step # (i,j) Piece" << std::endl;
outfile << "----------------------" << std::endl;

unsigned int index = 0;
for( const auto& epoch : mHistory )
outfile << std::setw( 4 ) << index++ << " (" << epoch.first << "," << epoch.second << ") " << std::setw( 1 ) << CTableau::PieceToString( aInitialTableau.GetPiece( epoch.first, epoch.second ) ) << std::endl;
// Write also remaining pieces
outfile << "----------------------" << std::endl;
outfile << " Remaining Pieces " << std::endl;
outfile << "----------------------" << std::endl;
for( CTableau::index i = 0; i < aInitialTableau.GetRows(); i++ )
for( CTableau::index j = 0; j < aInitialTableau.GetRows(); j++ )
if( aInitialTableau.GetPiece( i, j ) != CTableau::E_PIECE_TYPE::EMPTY && std::find( mTableauState.GetHistory().cbegin(), mTableauState.GetHistory().cend(), std::make_pair( i, j ) ) == mTableauState.GetHistory().cend() )
outfile << "(" << i << "," << j << "): " << CTableau::PieceToString( aInitialTableau.GetPiece( i, j ) ) << std::endl;
outfile.close();
return ( aCountHits > mTableauState.GetHistory().size() ) || ( aCountHits == mTableauState.GetHistory().size() && aScore > mTableauState.GetScore() );
}


};

0 comments on commit f3de31a

Please sign in to comment.