Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prog_kmeans
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
boris.stefanov
prog_kmeans
Commits
ab4e705b
Commit
ab4e705b
authored
2 years ago
by
Boris Stefanovic
Browse files
Options
Downloads
Patches
Plain Diff
DEBUG: removed cause of infinite loop in kmeans algorithm
parent
0d07376d
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/cluster.c
+7
-4
7 additions, 4 deletions
src/cluster.c
src/kmeans.c
+10
-22
10 additions, 22 deletions
src/kmeans.c
src/main.c
+1
-8
1 addition, 8 deletions
src/main.c
with
18 additions
and
34 deletions
src/cluster.c
+
7
−
4
View file @
ab4e705b
#include
"cluster.h"
#include
<assert.h>
#include
<math.h>
#include
<stdbool.h>
#include
"vector.h"
#ifdef DEBUG
#include
<assert.h>
#endif
#define EPSILON 0.001
cluster_int_t
*
cluster_create_int
(
vector_int_t
*
center
)
{
...
...
@@ -58,6 +59,8 @@ bool cluster_update_center_int(cluster_int_t* cluster) {
bool
cluster_update_center_fpt
(
cluster_fpt_t
*
cluster
)
{
// save old center
vector_fpt_t
*
old_center
=
cluster
->
center
;
assert
(
old_center
!=
NULL
);
assert
(
cluster
!=
NULL
);
// create new center
list_points_node_fpt_t
*
node
=
cluster
->
points
->
head
;
// if cluster is empty
...
...
@@ -75,7 +78,7 @@ bool cluster_update_center_fpt(cluster_fpt_t* cluster) {
// check whether center has changed
bool
changed
=
false
;
for
(
size_t
p
=
0
;
p
<
cluster
->
center
->
dim
;
++
p
)
{
if
(
cluster
->
center
->
data
[
p
]
!=
old_center
->
data
[
p
])
{
if
(
fabs
(
cluster
->
center
->
data
[
p
]
-
old_center
->
data
[
p
])
<
EPSILON
)
{
changed
=
true
;
break
;
}
...
...
This diff is collapsed.
Click to expand it.
src/kmeans.c
+
10
−
22
View file @
ab4e705b
#include
"kmeans.h"
#include
"vector.h"
#ifdef DEBUG
#include
<assert.h>
#include
"io.h"
#endif
#define EPSILON 0.001
fpt_t
abs_fpt
(
const
fpt_t
x
)
{
return
x
>=
0
?
x
:
-
x
;
}
#include
"vector.h"
cluster_int_t
**
kmeans_init_clusters_int
(
const
vector_int_t
**
points
,
const
size_t
point_count
,
const
size_t
nclusters
)
{
...
...
@@ -88,7 +77,7 @@ void kmeans_fpt(vector_fpt_t** points, const size_t point_count, cluster_fpt_t**
// find closest cluster
cluster_fpt_t
*
cmin
=
clusters
[
0
];
fpt_t
dmin
=
distance_function
(
point
,
cmin
->
center
);
for
(
size_t
k
=
1
;
k
<
nb_clusters
;
++
k
)
{
for
(
size_t
k
=
0
;
k
<
nb_clusters
;
++
k
)
{
cluster_fpt_t
*
current_cluster
=
clusters
[
k
];
fpt_t
dist
=
distance_function
(
point
,
current_cluster
->
center
);
if
(
dist
<
dmin
)
{
...
...
@@ -98,15 +87,14 @@ void kmeans_fpt(vector_fpt_t** points, const size_t point_count, cluster_fpt_t**
}
// add point to closest cluster
cluster_add_point_fpt
(
cmin
,
point
);
// update all cluster centers
for
(
size_t
k
=
0
;
k
<
nb_clusters
;
++
k
)
{
#ifdef DEBUG
assert
(
clusters
[
k
]
!=
NULL
);
assert
(
clusters
[
k
]
->
points
!=
NULL
);
#endif
if
(
cluster_update_center_fpt
(
clusters
[
k
]))
{
changed
=
true
;
}
}
// update all cluster centers
for
(
size_t
k
=
0
;
k
<
nb_clusters
;
++
k
)
{
assert
(
clusters
[
k
]
!=
NULL
);
assert
(
clusters
[
k
]
->
points
!=
NULL
);
if
(
cluster_update_center_fpt
(
clusters
[
k
]))
{
changed
=
true
;
printf
(
"%lud
\n
<%lf %lf %lf>
\n\n
"
,
nb_clusters
,
clusters
[
k
]
->
center
->
data
[
0
],
clusters
[
k
]
->
center
->
data
[
1
],
clusters
[
k
]
->
center
->
data
[
2
]);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/main.c
+
1
−
8
View file @
ab4e705b
...
...
@@ -9,10 +9,6 @@
#include
"linkedlist.h"
#include
"vector.h"
#ifdef DEBUG
#include
<assert.h>
#endif
enum
DistanceFunctionType
{
EUCLID
=
0
,
MANHATTAN
=
1
,
CHEBYSHEV
=
2
...
...
@@ -105,16 +101,13 @@ int main_fpt(const char* ipath, const char* opath, const enum DistanceFunctionTy
printf
(
"INIT: ... "
);
cluster_fpt_t
**
clusters
=
kmeans_init_clusters_fpt
((
const
vector_fpt_t
**
)
points
,
point_count
,
nb_clusters
);
printf
(
"DONE
\n
"
);
#ifdef DEBUG
for
(
size_t
i
=
0
;
i
<
nb_clusters
;
++
i
)
assert
(
clusters
[
i
]
!=
NULL
);
#endif
printf
(
"STARTING KMEANS ALGORITHM: ...
\n
"
);
kmeans_fpt
(
points
,
point_count
,
clusters
,
nb_clusters
,
DIST_FUNC_FPT
[
dist_func_type
]);
printf
(
"KMEANS DONE !
\n
"
);
// WRITE
FILE
*
ofile
=
opath
!=
NULL
?
fopen
(
opath
,
"w"
)
:
stdout
;
fprintf
(
ofile
,
"%lud
\n
%lud
\n
"
,
dim
,
nb_clusters
);
io_write_clusters_to_file_fpt
(
ofile
,
clusters
,
point_count
);
io_write_clusters_to_file_fpt
(
ofile
,
clusters
,
nb_clusters
);
fclose
(
ofile
);
return
EXIT_SUCCESS
;
}
...
...
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
register
or
sign in
to comment