$darkmode
DENOPTIM
DynamicCentroidCluster.java
Go to the documentation of this file.
1package denoptim.fragmenter;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.List;
6
7import org.apache.commons.math3.ml.distance.DistanceMeasure;
8
9import denoptim.utils.MathUtils;
10
20{
24 private List<ClusterableFragment> points;
25
30
35 private boolean updateCentroid = false;
36
37//------------------------------------------------------------------------------
38
45 {
46 this.points = new ArrayList<ClusterableFragment>();
47 }
48
49//------------------------------------------------------------------------------
50
57 {
58 this.points = new ArrayList<ClusterableFragment>();
59 this.centroid = centroid.clone();
60 this.points.add(centroid);
61 }
62
63//------------------------------------------------------------------------------
64
70 {
72 {
73 List<double[]> membersCoords = new ArrayList<double[]>();
75 {
76 membersCoords.add(cf.getPoint());
77 }
78 double[] refCentroidCoords = MathUtils.centroidOf(membersCoords,
79 centroid.getPoint().length);
80 centroid.setCoordsVector(refCentroidCoords);
81
82 updateCentroid = false;
83 }
84 return centroid;
85 }
86
87//------------------------------------------------------------------------------
88
96 public ClusterableFragment getNearestToCentroid(DistanceMeasure measure)
97 {
98 if (points.size() == 1)
99 return points.get(0);
100
102
103 ClusterableFragment nearest = null;
104 double smallestDistance = Double.MAX_VALUE;
105 for (ClusterableFragment cf : points)
106 {
107 double distance = measure.compute(centroid.getPoint(),cf.getPoint());
108 if (distance < smallestDistance)
109 {
110 nearest = cf;
111 smallestDistance = distance;
112 }
113 }
114 return nearest;
115 }
116
117//------------------------------------------------------------------------------
118
123 public void addPoint(ClusterableFragment point)
124 {
125 if (centroid == null)
126 {
127 // First time ever we add a point
128 centroid = point.clone();
129 } else {
130 updateCentroid = true;
131 }
132 points.add(point);
133 }
134
135//------------------------------------------------------------------------------
136
141 public void removeAll(Collection<ClusterableFragment> points)
142 {
143 updateCentroid = true;
144 points.removeAll(points);
145 }
146
147//------------------------------------------------------------------------------
148
153 public List<ClusterableFragment> getPoints()
154 {
155 return points;
156 }
157
158//------------------------------------------------------------------------------
159
160}
Represents a fragment that can be clustered based on the 3*N coordinate of atoms and attachment point...
ClusterableFragment clone()
Returns a shallow copy where the vector of coordinates has a new reference.
void setCoordsVector(double[] coords)
Sets the coordinates of each atom and attachment point in this object.
A cluster with a centroid that can be updated after definition of the cluster.
ClusterableFragment getCentroid()
Get the point chosen to be the centroid of this cluster.
DynamicCentroidCluster()
Constructor that defines an empty cluster.
DynamicCentroidCluster(ClusterableFragment centroid)
Constructor that defines the current centroid to be a clone of the given fragment and adds the given ...
void removeAll(Collection< ClusterableFragment > points)
Remove the given cluster member, if it is a member of this cluster.
ClusterableFragment getNearestToCentroid(DistanceMeasure measure)
Gets the original data that upon clustering is closest to the cluster centroid according to the given...
void addPoint(ClusterableFragment point)
Add a new member of this cluster.
List< ClusterableFragment > getPoints()
Get the points contained in the cluster.
ClusterableFragment centroid
Current centroid.
boolean updateCentroid
Flag requesting the update of the centroid.
List< ClusterableFragment > points
The points contained in this cluster.
Some useful math operations.
Definition: MathUtils.java:39
static double[] centroidOf(Collection< double[]> points, int dimension)
Definition: MathUtils.java:457