SDC C-Project CF Review 프로그램
kojingeun
2023-07-14 f77e8008cac062596058fca2aeddda62b80bedbf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "stdafx.h"
#include "GreedyTSP.h"
 
#include <math.h>
#include <float.h>
 
CGreedyTSP::CGreedyTSP(void)
{
    m_pPathGraph = NULL;
 
    Reset();
}
 
CGreedyTSP::~CGreedyTSP(void)
{
    Reset();
}
 
void CGreedyTSP::Reset()
{
    // vector
    m_vecPathData.clear();
    m_vecPathResult.clear();
    m_vecPathAdded.clear();    
 
    // graph
    if (m_pPathGraph)
    {
        for (int i=0; i<m_nPathCount; i++)
        {
            delete [] m_pPathGraph[i];
        }
        delete [] m_pPathGraph;
    }
    m_pPathGraph = NULL;
 
    m_nStartIndex = -1;
    m_nPathCount = 0;
    m_dTotalDistance = 0.0;
 
    m_ptStart.nPosX = -1;
    m_ptStart.nPosY = -1;
}
 
void CGreedyTSP::SetPathData(const VectorPathData& vecPathData, const SPathData& ptStart)
{
    if (vecPathData.size()<0) return;
 
    Reset();
 
    m_nPathCount        = (int)vecPathData.size();
    m_pPathGraph        = new double*[m_nPathCount];
 
    m_ptStart            = ptStart;
 
    // path data copy
    m_vecPathData = vecPathData;
    m_vecPathResult.resize(m_nPathCount);
    m_vecPathAdded.resize(m_nPathCount);
 
    // path graph
    for (int i=0; i<m_nPathCount; i++)
    {
        m_vecPathAdded[i] = false;
        m_pPathGraph[i] = new double [m_nPathCount];
        for (int j=0; j<m_nPathCount; j++)
        {
            if (i==j)
                m_pPathGraph[i][j] = 0.0;
            else
                m_pPathGraph[i][j] = CalculateDistance(m_vecPathData[i], m_vecPathData[j]);
        }
    }
 
    // path result
    InitPathResult(ptStart);
 
    m_dTotalDistance = GetTotalDistance();
 
}
 
void CGreedyTSP::SetPathData(const VectorPathData& vecPathData, const VectorInteger& vecPathResult)
{
    if (vecPathData.size()<1 || vecPathData.size()!=vecPathResult.size()) return;
 
    Reset();
 
    m_nPathCount    = (int)vecPathData.size();
    m_pPathGraph    = new double*[m_nPathCount];
 
    // path data
    m_vecPathData    = vecPathData;
    m_vecPathResult    = vecPathResult;
    m_vecPathAdded.resize(m_nPathCount);
 
    // path graph
    for (int i=0; i<m_nPathCount; i++)
    {
        m_vecPathAdded[i] = false;
        m_pPathGraph[i] = new double[m_nPathCount];
        for (int j=0; j<m_nPathCount; j++)
        {
            if (i==j)
                m_pPathGraph[i][j] = 0.0;
            else
                m_pPathGraph[i][j] = CalculateDistance(m_vecPathData[i], m_vecPathData[j]);
        }
    }
 
    m_dTotalDistance = GetTotalDistance();
}
 
double CGreedyTSP::CalculateTSP()
{
    if (m_nPathCount<1) return -1.0;
 
    SortingPathResult();
 
    return m_dTotalDistance;
}
 
int CGreedyTSP::GetPathCount() const
    return m_nPathCount; 
}
 
int    CGreedyTSP::GetPathResult(int nIndex) const
{
    if (nIndex<0 || nIndex>=m_nPathCount) return -1;
    return m_vecPathResult[nIndex];
}
 
const SPathData* CGreedyTSP::GetPathData(int nIndex) const
{
    if (nIndex<0 || nIndex>=m_nPathCount) return NULL;
    return &(m_vecPathData[nIndex]);
}
 
VectorInteger* CGreedyTSP::GetPathResult()
{
    return &m_vecPathResult;
}
 
VectorPathData* CGreedyTSP::GetPathData()
{
    return &m_vecPathData;
}
 
double CGreedyTSP::GetPathResult(VectorInteger& vecPathResult)
{
    vecPathResult.resize(m_nPathCount);
    for (int i=0; i<m_nPathCount; i++)
    {
        vecPathResult[i] = m_vecPathResult[i];
    }
 
    return m_dTotalDistance;
}
 
double CGreedyTSP::GetTotalDistance()
{
    double distance = 0.0;
 
    if (m_ptStart.nPosX!=-1 || m_ptStart.nPosX!=-1)
    {
        distance += CalculateDistance(m_ptStart, m_vecPathData[m_vecPathResult[0]]);
    }
 
    for (int i=1; i<m_nPathCount; i++)
    {
        distance += m_pPathGraph[m_vecPathResult[i-1]][m_vecPathResult[i]];
    }
    return distance;
}
 
double CGreedyTSP::GetTotalDistance(const VectorInteger& vecPathResult)
{
    int nPathCount = (int)vecPathResult.size();
 
    double distance = 0.0;
    for (int i=1; i<nPathCount; i++)
    {
        distance += m_pPathGraph[vecPathResult[i-1]][vecPathResult[i]];
    }
    return distance;
}
 
double CGreedyTSP::CalculateDistance(const POINT& pt1, const POINT &pt2)
{
    double dx = double(pt1.x-pt2.x) / 1000.0;
    double dy = double(pt1.y-pt2.y) / 1000.0;
    return sqrt(dx*dx + dy*dy);
}
 
double CGreedyTSP::CalculateDistance(const SPathData& pt1, const SPathData &pt2)
{
    double dx = double(pt1.nPosX-pt2.nPosX) / 1000.0;
    double dy = double(pt1.nPosY-pt2.nPosY) / 1000.0;
    return sqrt(dx*dx + dy*dy);
}
 
double CGreedyTSP::CalculateDistanceX( const POINT& pt1, const POINT &pt2 )
{
    double dx = double(pt1.x-pt2.x) / 1000.0;
    return fabs(dx);
}
 
double CGreedyTSP::CalculateDistanceX( const SPathData& pt1, const SPathData &pt2 )
{
    double dx = double(pt1.nPosX-pt2.nPosX) / 1000.0;
    return fabs(dx);
}
 
double CGreedyTSP::CalculateDistanceY( const POINT& pt1, const POINT &pt2 )
{
    double dy = double(pt1.y-pt2.y) / 1000.0;
    return fabs(dy);
}
 
double CGreedyTSP::CalculateDistanceY( const SPathData& pt1, const SPathData &pt2 )
{
    double dy = double(pt1.nPosY-pt2.nPosY) / 1000.0;
    return fabs(dy);
}
 
int CGreedyTSP::SelectPath(int nCurIdx)
{
    int nMinIdx        = -1;
    double dTempMin = DBL_MAX;
    for (int i=0; i<m_nPathCount; i++)
    {
        // Ãß°¡µÈ pathÀÎÁö È®ÀÎ.
        if (m_vecPathAdded[i]==true) continue;
 
        if (dTempMin > m_pPathGraph[nCurIdx][i])
        {
            dTempMin    = m_pPathGraph[nCurIdx][i];
            nMinIdx        = i;
        }
    }
 
    if (nMinIdx==-1)
    {
        return -1;
    }
 
    // Ãß°¡‰çÀ½À» ÀúÀå.
    m_vecPathAdded[nMinIdx] = true;
 
    return nMinIdx;
}
 
void CGreedyTSP::InitPathResult(const SPathData& ptStart)
{
    if (m_nPathCount<1) return;
 
    m_nStartIndex        = -1;
    double dMinDist        = DBL_MAX;
    double dTempDist    = 0.0;
 
    for (int i=0; i<m_nPathCount; i++)
    {
        dTempDist = CalculateDistance(ptStart, m_vecPathData[i]);
        if (dMinDist>dTempDist)
        {
            m_nStartIndex    = i;
            dMinDist        = dTempDist;
        }
    }
 
    if (m_nStartIndex==-1) 
    {
        return;
    }
 
    m_vecPathResult[0] = m_nStartIndex;
 
    // Ãß°¡‰çÀ½À» ÀúÀå.
    m_vecPathAdded[m_nStartIndex] = true;
 
    for (int i=1; i<m_nPathCount; i++)
    {
        m_vecPathResult[i] = SelectPath(m_vecPathResult[i-1]);
    }
}
 
BOOL CGreedyTSP::SortingPathResult()
{
    if (m_nPathCount<1) return FALSE;
 
    int nStartIndex        = -1;
    double dMinDist        = DBL_MAX;
    double dTempDist    = 0.0;
 
    for (int i=0; i<m_nPathCount; i++)
    {
        dTempDist = CalculateDistance(m_ptStart, m_vecPathData[i]);
        if (dMinDist>dTempDist)
        {
            nStartIndex    = i;
            dMinDist    = dTempDist;
        }
    }
 
    for (int i=0; i<m_nPathCount; i++)
    {
        if (nStartIndex==m_vecPathResult[i])
        {
            nStartIndex = i;
            break;
        }
    }
    
    VectorInteger    vecPathResult;
    vecPathResult.reserve(m_nPathCount);
 
    for (int i=nStartIndex; i<m_nPathCount; i++)
    {
        vecPathResult.push_back(m_vecPathResult[i]);
    }
 
    for (int i=0; i<nStartIndex; i++)
    {
        vecPathResult.push_back(m_vecPathResult[i]);
    }
 
    m_vecPathResult = vecPathResult;
 
    return TRUE;
}