From ec9e4b4c763994b5e8e2b52a69c38cb110db48dc Mon Sep 17 00:00:00 2001 From: SaraP Date: Thu, 18 Nov 2021 09:34:24 +0100 Subject: [PATCH] EgtNumKernel 2.3k1 : - in ShortestPath aumentato il numero max di punti consentiti. --- EgtNumKernel.rc | Bin 11662 -> 11670 bytes Fn.cpp | 2 +- Hybrid.cpp | 2 +- Nn.cpp | 2 +- PntOpt.cpp | 4 ++-- ShortestPath.cpp | 2 +- ShortestPath.h | 24 ++++++++++------------ ShortestPathTsp.cpp | 44 ++++++++++++++++++++--------------------- ShortestPathZigZag.cpp | 10 +++++----- TwoOpt.cpp | 2 +- 10 files changed, 44 insertions(+), 48 deletions(-) diff --git a/EgtNumKernel.rc b/EgtNumKernel.rc index d0c16d698e5a04ed6852c5ad5bd3401799c73438..4d7bf3b0a9a68711b74a2966735c218ef77b27b1 100644 GIT binary patch delta 254 zcmeB+o)*1f9kaS8LmopqLk>eKLopC5FlaCYGn6nS0&ymg7YdZk1hFUIR%_iH$1;P7 z*^t3-^J+PHX69@L!^sDgbvOS~bYa}w#qGchmO9O6jV$HFxVb{u371rcG*k-BKr^D< mwK-a~m{Bkv>~K%8!*hTRPTYK3Z6heGLoq`Jg93vFLnu%rlc9tml_3Sl4+gRl!Mx(h3$>&- ze_&=}X0+J6UQV8wF=O+2#bU+|g`ozdlIn(o plh^6Yo4iX+gcsymPq6!P7%D-ooBV+}YjcXa4l}CM=7m~cnE;+OLkj=^ diff --git a/Fn.cpp b/Fn.cpp index 8ff1087..c7514c1 100644 --- a/Fn.cpp +++ b/Fn.cpp @@ -16,7 +16,7 @@ #include "ShortestPath.h" /*-------------------------------------------------------------------------- */ -unsigned +long long unsigned ShortestPath::FarNeighbor( void) { // abilito i collegamenti tra nodi diversi e disabilito gli auto-collegamenti diff --git a/Hybrid.cpp b/Hybrid.cpp index 3250325..d37620b 100644 --- a/Hybrid.cpp +++ b/Hybrid.cpp @@ -18,7 +18,7 @@ #include "ShortestPath.h" // ---------------------------------------------------------------------------- -unsigned +long long unsigned ShortestPath::Hybrid( NODE* pPath) { const int MAX_TRY = 2048 ; diff --git a/Nn.cpp b/Nn.cpp index f7e90cc..7c2773a 100644 --- a/Nn.cpp +++ b/Nn.cpp @@ -16,7 +16,7 @@ #include "ShortestPath.h" /* ------------------------------------------------------------------------- */ -unsigned +long long unsigned ShortestPath::NearNeighbor( void) { // abilito i collegamenti tra nodi diversi e disabilito gli auto-collegamenti diff --git a/PntOpt.cpp b/PntOpt.cpp index dd2f2b7..b3b16b4 100644 --- a/PntOpt.cpp +++ b/PntOpt.cpp @@ -21,7 +21,7 @@ #include "ShortestPath.h" /* ------------------------------------------------------------------------- */ -unsigned +long long unsigned ShortestPath::PointOpt( NODE* pPath) { // ciclo di tentativi di spostamento di un punto @@ -40,7 +40,7 @@ ShortestPath::PointOpt( NODE* pPath) ArcCost( pLast->nPos, pKth->pNext->nPos) ; if ( nTEST < nEXIST && ( nEXIST - nTEST) > nBestImprove) { nBestImprove = nEXIST - nTEST ; - pBest = pKth ; + pBest = pKth ; } } if ( nBestImprove == 0) { diff --git a/ShortestPath.cpp b/ShortestPath.cpp index 3870c9e..e4f2372 100644 --- a/ShortestPath.cpp +++ b/ShortestPath.cpp @@ -232,6 +232,6 @@ ShortestPath::GetMinLength( double& dMinLen) if ( ! m_bSolved) return false ; // assegno risultato - dMinLen = m_nMinCost ; + dMinLen = ( double)m_nMinCost ; return true ; } diff --git a/ShortestPath.h b/ShortestPath.h index a893774..5dd8d1e 100644 --- a/ShortestPath.h +++ b/ShortestPath.h @@ -15,10 +15,6 @@ #include "/EgtDev/Include/ENkShortestPath.h" - -//---------------------------------------------------------------------------- -#define MAXDIST 1048576U // 2^20 per evitare overflow con int e 1024 somme - //---------------------------------------------------------------------------- struct SpPoint { double dXi ; @@ -76,24 +72,24 @@ class ShortestPath : public IShortestPath void PreparePath( NODE* pPath) ; void SavePath( NODE* pPath) ; void RestorePath( NODE* pPath) ; - unsigned TotalCost( NODE* pPath) ; + long long unsigned TotalCost( NODE* pPath) ; void UpdateOrder( NODE* pPath) ; unsigned Index( unsigned i, unsigned j) { return ( i * m_nNumPnts + j) ; } unsigned ArcCost( unsigned i, unsigned j) { return m_Dists[ Index( i, j)] ; } - unsigned NearNeighbor( void) ; + long long unsigned NearNeighbor( void) ; unsigned CheapArc( unsigned i) ; - unsigned FarNeighbor( void) ; + long long unsigned FarNeighbor( void) ; unsigned HighArc( unsigned i) ; - unsigned PointOpt( NODE* pPath) ; - unsigned TwoOpt( NODE* pPath) ; - unsigned Hybrid( NODE* pPath) ; + long long unsigned PointOpt( NODE* pPath) ; + long long unsigned TwoOpt( NODE* pPath) ; + long long unsigned Hybrid( NODE* pPath) ; bool ZigZag( void) ; - unsigned TotalCost( void) ; + long long unsigned TotalCost( void) ; private : - static const int MAX_SPPS = 1024 ; + static const int MAX_SPPS = 32766 ; // 2^15-2 per evitare problemi nell'allocazione di m_Dists a 32bit private : int m_nType ; @@ -112,6 +108,6 @@ class ShortestPath : public IShortestPath bool* m_Available ; NODE* m_pMain ; NODE* m_pDupl ; - unsigned m_nTotMin ; - unsigned m_nMinCost ; + long long unsigned m_nTotMin ; + long long unsigned m_nMinCost ; } ; diff --git a/ShortestPathTsp.cpp b/ShortestPathTsp.cpp index 840a783..10c936f 100644 --- a/ShortestPathTsp.cpp +++ b/ShortestPathTsp.cpp @@ -81,11 +81,11 @@ ShortestPath::Tsp( void) PreparePath( m_pDupl) ; // inizializzo minimo costo - m_nMinCost = INT_MAX ; + m_nMinCost = LLONG_MAX ; // ---- applico algoritmo NearNeighbor con miglioramenti aggiuntivi ---- - unsigned nMinNN = NearNeighbor() ; - string sOut = "-- NearNeighbor : TotalCost = " + ToString( (int)nMinNN) ; + long long unsigned nMinNN = NearNeighbor() ; + string sOut = "-- NearNeighbor : TotalCost = " + to_string( nMinNN) ; MY_LOG( sOut.c_str()) ; // se migliora, salvo l'ordinamento if ( nMinNN < m_nMinCost) { @@ -111,8 +111,8 @@ ShortestPath::Tsp( void) pCurr = pCurr->pPrev ; } // ne calcolo il risultato - unsigned nMinInv = TotalCost( m_pMain) ; - string sOut = "-- Inverted NN : TotalCost = " + ToString( (int)nMinInv) ; + long long unsigned nMinInv = TotalCost( m_pMain) ; + string sOut = "-- Inverted NN : TotalCost = " + to_string( nMinInv) ; MY_LOG( sOut.c_str()) ; // se migliora, salvo l'ordinamento if ( nMinInv < m_nMinCost) { @@ -132,8 +132,8 @@ ShortestPath::Tsp( void) const double COEFF_FARNEIG = 1.4 ; const int MIN_PNTS_FARNEIG = 128 ; if ( m_nMinCost > COEFF_FARNEIG * m_nTotMin || m_nNumPnts < MIN_PNTS_FARNEIG) { - unsigned nMinFN = FarNeighbor() ; - string sOut = "-- FarNeighbor : TotalCost = " + ToString( (int)nMinFN) ; + long long unsigned nMinFN = FarNeighbor() ; + string sOut = "-- FarNeighbor : TotalCost = " + to_string( nMinFN) ; MY_LOG( sOut.c_str()) ; // se migliora, salvo l'ordinamento if ( nMinFN < m_nMinCost) { @@ -158,8 +158,8 @@ ShortestPath::CalculateImprovements( NODE* pPath) { // Ottimizzazione con movimento di singolo punto RestorePath( pPath) ; - unsigned nPntOpt = PointOpt( pPath) ; - string sOut = " PointOpt : TotalCost = " + ToString( (int)nPntOpt) ; + long long unsigned nPntOpt = PointOpt( pPath) ; + string sOut = " PointOpt : TotalCost = " + to_string( nPntOpt) ; MY_LOG( sOut.c_str()) ; // se migliora, salvo l'ordinamento if ( nPntOpt < m_nMinCost) { @@ -172,8 +172,8 @@ ShortestPath::CalculateImprovements( NODE* pPath) const int MAX_NODES_2OPT = 768 ; if ( m_nNumPnts <= MAX_NODES_2OPT) { RestorePath( pPath) ; - unsigned nTwoOpt = TwoOpt( pPath) ; - string sOut = " TwoOpt : TotalCost = " + ToString( (int)nTwoOpt) ; + long long unsigned nTwoOpt = TwoOpt( pPath) ; + string sOut = " TwoOpt : TotalCost = " + to_string( nTwoOpt) ; MY_LOG( sOut.c_str()) ; // se migliora, salvo l'ordinamento if ( nTwoOpt < m_nMinCost) { @@ -187,8 +187,8 @@ ShortestPath::CalculateImprovements( NODE* pPath) const int MAX_NODES_HYBRID = 512 ; if ( m_nNumPnts <= MAX_NODES_HYBRID) { RestorePath( pPath) ; - unsigned nHybOpt = Hybrid( pPath) ; - string sOut = " Hybrid : TotalCost = " + ToString( (int)nHybOpt) ; + long long unsigned nHybOpt = Hybrid( pPath) ; + string sOut = " Hybrid : TotalCost = " + to_string( nHybOpt) ; MY_LOG( sOut.c_str()) ; // se migliora, salvo l'ordinamento if ( nHybOpt < m_nMinCost) { @@ -297,7 +297,7 @@ ShortestPath::CalcDistances( void) nDist = 0 ; break ; } - m_Dists[ Index( m_nNumPnts - 1, i)] = min( nDist, MAXDIST) ; + m_Dists[ Index( m_nNumPnts - 1, i)] = nDist ; // verifico se nuovo minimo di linea if ( nDist < nRowMinDist) nRowMinDist = nDist ; @@ -333,10 +333,10 @@ ShortestPath::CalcDistances( void) nDist = 0 ; break ; } - m_Dists[ Index( i, m_nNumPnts - 1)] = min( nDist, MAXDIST) ; + m_Dists[ Index( i, m_nNumPnts - 1)] = nDist ; } // punto su diagonale principale - m_Dists[ Index( m_nNumPnts - 1, m_nNumPnts - 1)] = MAXDIST ; + m_Dists[ Index( m_nNumPnts - 1, m_nNumPnts - 1)] = UINT_MAX ; // devo ancora calcolare le righe/colonne precedenti nLimit = m_nNumPnts - 1 ; } @@ -352,14 +352,14 @@ ShortestPath::CalcDistances( void) float dDz = float( m_Points[i].dZf - m_Points[j].dZi) ; float dDh = float( m_Points[i].dHf - m_Points[j].dHi) ; float dDv = float( m_Points[i].dVf - m_Points[j].dVi) ; - unsigned nDist = min( GetDistance( dDx, dDy, dDz, dDh, dDv), MAXDIST) ; + unsigned nDist = GetDistance( dDx, dDy, dDz, dDh, dDv) ; m_Dists[ Index( i, j)] = nDist ; // verifico se nuovo minimo di linea if ( nDist < nRowMinDist) nRowMinDist = nDist ; } else - m_Dists[ Index( i, j)] = MAXDIST ; + m_Dists[ Index( i, j)] = UINT_MAX ; } // aggiorno il totale delle minime distanze m_nTotMin += nRowMinDist ; @@ -368,7 +368,7 @@ ShortestPath::CalcDistances( void) // nel caso di percorso aperto senza vincoli, il numero delle distanze è uno meno di quello dei veri nodi if ( m_nType == SP_OPEN && m_ObStart.nF == OB_NONE && m_ObEnd.nF == OB_NONE) - m_nTotMin = (unsigned) ( m_nTotMin / (double) nLimit * ( nLimit - 1)) ; + m_nTotMin = (long long unsigned) ( m_nTotMin / (double) nLimit * ( nLimit - 1)) ; // stampe di debug #if 0 @@ -381,7 +381,7 @@ ShortestPath::CalcDistances( void) MY_LOG( sOut.c_str()) ; } #endif - string sOut = "MinCost = " + ToString( int( m_nTotMin)) ; + string sOut = "MinCost = " + to_string( m_nTotMin) ; MY_LOG( sOut.c_str()) ; } @@ -424,14 +424,14 @@ ShortestPath::RestorePath( NODE* pPath) } //---------------------------------------------------------------------------- -unsigned +long long unsigned ShortestPath::TotalCost( NODE* pPath) { // ci devono essere almeno due nodi if ( pPath == nullptr || pPath->pNext == nullptr) return 0 ; // lunghezza del primo arco - unsigned nCost = ArcCost( pPath->nPos, pPath->pNext->nPos) ; + long long unsigned nCost = ArcCost( pPath->nPos, pPath->pNext->nPos) ; // ciclo sui nodi successivi (calcolo la lunghezza degli archi che li uniscono) NODE* pCurr = pPath->pNext ; while ( pCurr != pPath) { diff --git a/ShortestPathZigZag.cpp b/ShortestPathZigZag.cpp index d49ac0a..f262b80 100644 --- a/ShortestPathZigZag.cpp +++ b/ShortestPathZigZag.cpp @@ -159,23 +159,23 @@ ShortestPath::ZigZag( void) // ne calcolo il risultato m_nMinCost = TotalCost() ; - string sOut = "-- ZigZag/OneWay : TotalCost = " + ToString( (int)m_nMinCost) ; + string sOut = "-- ZigZag/OneWay : TotalCost = " + to_string( m_nMinCost) ; MY_LOG( sOut.c_str()) ; return true ; } //---------------------------------------------------------------------------- -unsigned -ShortestPath::TotalCost( void) +long long unsigned +ShortestPath::TotalCost( void) { - unsigned nCost = 0 ; + long long unsigned nCost = 0 ; // ciclo sui nodi for ( unsigned i = 1 ; i < m_nNumPnts ; ++ i) { double dDx = m_Points[m_nOrder[i]].dXi - m_Points[m_nOrder[i-1]].dXi ; double dDy = m_Points[m_nOrder[i]].dYi - m_Points[m_nOrder[i-1]].dYi ; double dDz = m_Points[m_nOrder[i]].dZi - m_Points[m_nOrder[i-1]].dZi ; - nCost += unsigned( sqrt( dDx * dDx + dDy * dDy + dDz * dDz)) ; + nCost += (long long unsigned)( sqrt( dDx * dDx + dDy * dDy + dDz * dDz)) ; } return nCost ; } diff --git a/TwoOpt.cpp b/TwoOpt.cpp index 6f8bfce..1b35da8 100644 --- a/TwoOpt.cpp +++ b/TwoOpt.cpp @@ -22,7 +22,7 @@ #include "ShortestPath.h" // ---------------------------------------------------------------------------- -unsigned +long long unsigned ShortestPath::TwoOpt( NODE* pPath) { const int MAX_TRY = 2048 ;