Skip to content
Snippets Groups Projects
Commit 5b1d0f68 authored by Alexandre's avatar Alexandre
Browse files

Fix infinite loop, Add lastModified field in db

parent 08264b03
Branches
No related tags found
No related merge requests found
...@@ -148,6 +148,7 @@ public class ClusterAgent<T extends DataPoint> extends Agent<DynamicClusteringAM ...@@ -148,6 +148,7 @@ public class ClusterAgent<T extends DataPoint> extends Agent<DynamicClusteringAM
} }
if (!otherSimilarClusterAgents.isEmpty()) { if (!otherSimilarClusterAgents.isEmpty()) {
this.requestDistanceMessageSentAgents.clear(); this.requestDistanceMessageSentAgents.clear();
distancesReceived.clear();
this.requestDistanceMessageSentAgents.addAll(otherSimilarClusterAgents); this.requestDistanceMessageSentAgents.addAll(otherSimilarClusterAgents);
nextState = State.WAITING_FOR_DISTANCE; nextState = State.WAITING_FOR_DISTANCE;
} else { } else {
...@@ -209,7 +210,6 @@ public class ClusterAgent<T extends DataPoint> extends Agent<DynamicClusteringAM ...@@ -209,7 +210,6 @@ public class ClusterAgent<T extends DataPoint> extends Agent<DynamicClusteringAM
logger.info(this + " absorbs the data point " + otherDataPoint); logger.info(this + " absorbs the data point " + otherDataPoint);
T newRepresentative = (T) amas.getMasSettings().dataPointFuser().apply(cluster.getRepresentative(), otherDataPoint); T newRepresentative = (T) amas.getMasSettings().dataPointFuser().apply(cluster.getRepresentative(), otherDataPoint);
cluster.setRepresentative(newRepresentative);
cluster.addDataPoint(otherDataPoint); cluster.addDataPoint(otherDataPoint);
} }
...@@ -217,15 +217,14 @@ public class ClusterAgent<T extends DataPoint> extends Agent<DynamicClusteringAM ...@@ -217,15 +217,14 @@ public class ClusterAgent<T extends DataPoint> extends Agent<DynamicClusteringAM
if (logger.isLoggable(Level.INFO)) if (logger.isLoggable(Level.INFO))
logger.info(this + " absorbs the cluster " + otherCluster); logger.info(this + " absorbs the cluster " + otherCluster);
T newRepresentative = (T) amas.getMasSettings().dataPointFuser().apply(cluster.getRepresentative(), otherCluster.getRepresentative()); amas.getMasSettings().dataPointFuser().apply(cluster.getRepresentative(), otherCluster.getRepresentative());
cluster.setRepresentative(newRepresentative);
if (getAmas().getMasSettings().amasOptions().contains(AMASOption.KeepAllDataPoints)) if (getAmas().getMasSettings().amasOptions().contains(AMASOption.KeepAllDataPoints))
((ExtendedCluster<T>) cluster).addClusterContent((ExtendedCluster<T>) otherCluster); ((ExtendedCluster<T>) cluster).addClusterContent((ExtendedCluster<T>) otherCluster);
else else
cluster.addClusterContent(otherCluster); cluster.addClusterContent(otherCluster);
amas.getMasSettings().database().setLastModifiedDataPoint(cluster.getRepresentative());
} }
@Override @Override
......
...@@ -25,7 +25,7 @@ public class DataPointAgent<T extends DataPoint> extends Agent<DynamicClustering ...@@ -25,7 +25,7 @@ public class DataPointAgent<T extends DataPoint> extends Agent<DynamicClustering
public DataPointAgent(DynamicClusteringAMAS<T> amas, T dataPoint) { public DataPointAgent(DynamicClusteringAMAS<T> amas, T dataPoint) {
super(amas); super(amas);
if (logger.isLoggable(Level.INFO)) if (logger.isLoggable(Level.INFO))
logger.info(this + " created"); logger.info(this + " created for datapoint "+dataPoint);
this.dataPoint = dataPoint; this.dataPoint = dataPoint;
} }
......
...@@ -14,7 +14,7 @@ import java.util.UUID; ...@@ -14,7 +14,7 @@ import java.util.UUID;
public class Cluster<T extends DataPoint> { public class Cluster<T extends DataPoint> {
@Getter @Getter
@Setter @Setter
private T representative; private final T representative;
@Getter @Getter
private int size; private int size;
......
package fr.irit.smac.amas4dc.cluster; package fr.irit.smac.amas4dc.cluster;
import java.util.ArrayList; import java.util.*;
import java.util.Collections; import java.util.logging.Level;
import java.util.List; import java.util.logging.Logger;
public class DataPointDatabase<T extends DataPoint> { public class DataPointDatabase<T extends DataPoint> {
private List<T> content = new ArrayList<>(); private static final Logger logger = Logger.getLogger(DataPointDatabase.class.getName());
private Set<T> content = new HashSet<>();
private T lastModifiedDataPoint;
public void add(T newDataPoint) { public void add(T newDataPoint) {
if (logger.isLoggable(Level.INFO))
logger.info("Adding an element to database");
content.add(newDataPoint); content.add(newDataPoint);
} }
...@@ -14,11 +19,16 @@ public class DataPointDatabase<T extends DataPoint> { ...@@ -14,11 +19,16 @@ public class DataPointDatabase<T extends DataPoint> {
content.remove(dataPoint); content.remove(dataPoint);
} }
public List<T> get() { public Set<T> get() {
return Collections.unmodifiableList(content); return content;
} }
public boolean contains(T dataPoint) { public boolean contains(T dataPoint) {
return content.contains(dataPoint); return content.contains(dataPoint);
} }
public void setLastModifiedDataPoint(T dataPoint) {
this.lastModifiedDataPoint = dataPoint;
}
} }
...@@ -2,21 +2,24 @@ import fr.irit.smac.amas4dc.AMAS4DC ...@@ -2,21 +2,24 @@ import fr.irit.smac.amas4dc.AMAS4DC
import fr.irit.smac.amas4dc.amas.AMASOption import fr.irit.smac.amas4dc.amas.AMASOption
import fr.irit.smac.amas4dc.amas.MASSettings import fr.irit.smac.amas4dc.amas.MASSettings
import fr.irit.smac.amas4dc.cluster.DataPoint import fr.irit.smac.amas4dc.cluster.DataPoint
import fr.irit.smac.amas4dc.cluster.DataPointDatabase
import fr.irit.smac.amas4dc.cluster.DistanceMethod import fr.irit.smac.amas4dc.cluster.DistanceMethod
import spock.lang.Specification import spock.lang.Specification
class SimpleClusteringIT extends Specification { class SimpleClusteringIT extends Specification {
def "Clustering of two similar 1D objects"() { def "Clustering of two similar 1D objects"() {
def database = new DataPointDatabase<OneDDataPoint>()
given: given:
def amas4dc = new AMAS4DC<OneDDataPoint>(new MASSettings(new OneDDistanceMethod(), 1f, EnumSet.noneOf(AMASOption), (OneDDataPoint dp1, OneDDataPoint dp2) -> { def amas4dc = new AMAS4DC<OneDDataPoint>(new MASSettings(new OneDDistanceMethod(), 1f, EnumSet.noneOf(AMASOption), (OneDDataPoint dp1, OneDDataPoint dp2) -> {
return new OneDDataPoint(dp1.value + (dp2.value - dp1.value) * 0.1f) return new OneDDataPoint(dp1.value + (dp2.value - dp1.value) * 0.1f)
})) }, database))
when: when:
amas4dc.fit([new OneDDataPoint(1), new OneDDataPoint(2)]) amas4dc.fit([new OneDDataPoint(1), new OneDDataPoint(2)])
var results = amas4dc.retrieveClusters() var results = amas4dc.retrieveClusters()
then: then:
database.get().size() == results.clusters().size()
results.clusters().size() == 1 results.clusters().size() == 1
results.clusters().get(0).getSize() == 2 results.clusters().get(0).getSize() == 2
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment