Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cuteLib-hsrt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tobias Glaser
cuteLib-hsrt
Commits
2ffca211
Commit
2ffca211
authored
6 months ago
by
tobiglaser
Browse files
Options
Downloads
Patches
Plain Diff
added TSP (BruteForce) example
parent
d782f948
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/TSP.cpp
+82
-3
82 additions, 3 deletions
examples/TSP.cpp
with
82 additions
and
3 deletions
examples/TSP.cpp
+
82
−
3
View file @
2ffca211
#include
"cute.h"
#include
"cute.h"
#include
<algorithm>
#include
<algorithm>
#include
<vector>
#include
<cmath>
#include
<cmath>
#include
<vector>
using
namespace
cute
;
using
namespace
cute
;
using
namespace
std
;
using
namespace
std
;
void
nearestNeighbour
();
void
nearestNeighbour
();
void
twoOpt
();
void
twoOpt
();
void
bruteForce
();
void
setup
()
void
setup
()
{
{
registerAlgorithm
(
nearestNeighbour
,
"NearestNeighbour"
);
registerAlgorithm
(
nearestNeighbour
,
"NearestNeighbour"
);
registerAlgorithm
(
twoOpt
,
"2-Opt :D"
);
registerAlgorithm
(
twoOpt
,
"2-Opt :D"
);
registerAlgorithm
(
bruteForce
,
"Brute Force!"
);
setProblemSize
(
15
);
setProblemSize
(
15
);
setPlotTheme
(
3
);
setPlotTheme
(
3
);
setPlotVisible
(
1
,
false
);
setPlotVisible
(
1
,
false
);
...
@@ -26,12 +28,89 @@ float length (const Point& a, const Point& b)
...
@@ -26,12 +28,89 @@ float length (const Point& a, const Point& b)
return
sqrt
(
pow
((
a
.
x
-
b
.
x
),
2
)
+
pow
((
a
.
y
-
b
.
y
),
2
));
return
sqrt
(
pow
((
a
.
x
-
b
.
x
),
2
)
+
pow
((
a
.
y
-
b
.
y
),
2
));
}
}
float
circ_pythagoras
(
const
vector
<
pair
<
int
,
Point
>>&
vec
)
{
float
sum
=
0
;
for
(
size_t
i
=
0
;
i
<
(
vec
.
size
()
-
1
);
++
i
)
{
float
distance
=
length
(
vec
[
i
].
second
,
vec
[
i
+
1
].
second
);
sum
+=
distance
;
}
if
(
vec
.
back
().
first
!=
vec
.
front
().
first
)
{
float
distance
=
length
(
vec
.
back
().
second
,
vec
.
front
().
second
);
//close circle
sum
+=
distance
;
}
return
sum
;
}
namespace
global
namespace
global
{
{
vector
<
Point
>
result
;
vector
<
Point
>
result
;
}
//namespace global
}
//namespace global
void
plotMyVectorLines
(
vector
<
pair
<
int
,
Point
>>&
vec
,
int
plot
,
bool
loop
)
{
vector
<
Point
>
result
;
result
.
reserve
(
vec
.
size
());
for
(
auto
&&
p
:
vec
)
{
result
.
emplace_back
(
p
.
second
);
}
plotLines
(
result
,
plot
,
loop
);
}
void
bruteForce
()
{
resetPlot
(
0
);
vector
<
Point
>
data
=
getProblemData2D
();
plotPoints
(
data
);
plotLines
(
data
);
long
long
n_fac
=
1
;
for
(
size_t
i
=
1
;
i
<=
data
.
size
()
-
1
;
n_fac
*=
i
++
)
{
}
vector
<
pair
<
int
,
Point
>>
vec
;
for
(
size_t
i
=
0
;
i
<
data
.
size
();
++
i
)
{
vec
.
push_back
({
i
,
data
[
i
]});
}
vec
.
push_back
(
vec
.
front
());
unsigned
long
long
i
=
0
;
setStatusMessage
(
to_string
(
i
)
+
" of "
+
to_string
(
n_fac
));
setProgress
(
100
*
i
/
n_fac
);
float
bestCirc
=
std
::
numeric_limits
<
float
>
().
max
();
vector
<
pair
<
int
,
Point
>>
bestVec
=
vec
;
do
{
int
circ
=
circ_pythagoras
(
vec
);
if
(
circ
<
bestCirc
)
{
bestCirc
=
circ
;
bestVec
=
vec
;
undo
(
0
);
plotMyVectorLines
(
vec
,
0
,
true
);
}
if
(
++
i
%
1000
==
0
)
{
setProgress
(
100
*
i
/
n_fac
);
setStatusMessage
(
to_string
(
i
)
+
" of "
+
to_string
(
n_fac
));
}
}
while
(
cute
::
ok
()
&&
std
::
prev_permutation
(
++
vec
.
begin
(),
--
vec
.
end
(),
[](
pair
<
int
,
Point
>&
a
,
pair
<
int
,
Point
>&
b
)
{
return
a
.
first
>
b
.
first
;
}));
setStatusMessage
(
to_string
(
i
)
+
" of "
+
to_string
(
n_fac
));
setProgress
(
100
*
i
/
n_fac
);
logMessage
(
"DONE grrr"
);
}
void
nearestNeighbour
()
void
nearestNeighbour
()
{
{
...
...
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment