Commit 274045dd authored by lafabregue's avatar lafabregue

modification of Kmeans handling constraints

parent b8f42088
...@@ -20,7 +20,7 @@ abstract public class Cluster implements Serializable { ...@@ -20,7 +20,7 @@ abstract public class Cluster implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** Le resultat de la classification qui contient ce cluster */ /** Le resultat de la classification qui contient ce cluster */
protected final ClusteringResult result; protected ClusteringResult result;
/** The cluster's gravity center */ /** The cluster's gravity center */
protected DataObject clusterCenter = null; protected DataObject clusterCenter = null;
...@@ -254,6 +254,17 @@ abstract public class Cluster implements Serializable { ...@@ -254,6 +254,17 @@ abstract public class Cluster implements Serializable {
return result; return result;
} }
/**
* <p>
* Set the clustering result to which the cluster should be attached
* <p>
*
* @param result the clustering result
*/
public void setResult(ClusteringResult result) {
this.result = result;
}
/** /**
* <p> * <p>
* Test si la classe est lockee. * Test si la classe est lockee.
......
...@@ -63,6 +63,7 @@ public class SimpleClusteringResult extends ClusteringResult { ...@@ -63,6 +63,7 @@ public class SimpleClusteringResult extends ClusteringResult {
this.clusters = new Cluster[result.clusters.length]; this.clusters = new Cluster[result.clusters.length];
for (int i = 0; i < result.clusters.length; i++) { for (int i = 0; i < result.clusters.length; i++) {
this.clusters[i] = (Cluster) result.clusters[i].clone(); this.clusters[i] = (Cluster) result.clusters[i].clone();
this.clusters[i].setResult(this);
} }
this.weights = (ClassificationWeights) result.weights.clone(); this.weights = (ClassificationWeights) result.weights.clone();
......
...@@ -413,6 +413,7 @@ public class ClassifierKmeans extends LearningMethod { ...@@ -413,6 +413,7 @@ public class ClassifierKmeans extends LearningMethod {
public ClusteringResult injectConstraints(LearningResult learningResult, ClusteringResult _result, Data data, public ClusteringResult injectConstraints(LearningResult learningResult, ClusteringResult _result, Data data,
Vector<Constraint> constraints) throws MethodNotImplementedException { Vector<Constraint> constraints) throws MethodNotImplementedException {
for (Constraint c : constraints) { for (Constraint c : constraints) {
int newClusterId = ((LearningResultKmeans) learningResult).seeds.size();
if (c instanceof CannotLinkConstraint) { if (c instanceof CannotLinkConstraint) {
// in the case of CL we create a new seed with the farthest element // in the case of CL we create a new seed with the farthest element
CannotLinkConstraint cl = (CannotLinkConstraint) c; CannotLinkConstraint cl = (CannotLinkConstraint) c;
...@@ -424,22 +425,30 @@ public class ClassifierKmeans extends LearningMethod { ...@@ -424,22 +425,30 @@ public class ClassifierKmeans extends LearningMethod {
((LearningResultKmeans) learningResult).seeds.get(_result.getClusterMap()[first]) ((LearningResultKmeans) learningResult).seeds.get(_result.getClusterMap()[first])
.distance(data.getDataObject(second), learningResult.getWeights().getGlobalWeights())) { .distance(data.getDataObject(second), learningResult.getWeights().getGlobalWeights())) {
((LearningResultKmeans) learningResult).seeds.add( ((LearningResultKmeans) learningResult).seeds.add(
new LightHardSeed(data.getDataObject(first), data, ((LearningResultKmeans) learningResult).seeds.size())); new LightHardSeed(data.getDataObject(first), data, newClusterId));
} else { } else {
((LearningResultKmeans) learningResult).seeds.add( ((LearningResultKmeans) learningResult).seeds.add(
new LightHardSeed(data.getDataObject(second), data, ((LearningResultKmeans) learningResult).seeds.size())); new LightHardSeed(data.getDataObject(second), data, newClusterId));
} }
} }
} else if (c instanceof MustLinkConstraint) { } else if (c instanceof MustLinkConstraint) {
// in the case of ML we try to merge the involved clusters // OLD VERSION : in the case of ML we try to merge the involved clusters
// in the case of ML we create a new seed that is just composed of the two elements.
MustLinkConstraint ml = (MustLinkConstraint) c; MustLinkConstraint ml = (MustLinkConstraint) c;
int first = ml.getFirstIndex(); int first = ml.getFirstIndex();
int second = ml.getSecondIndex(); int second = ml.getSecondIndex();
if (_result.getClusterMap()[first] != _result.getClusterMap()[second]) { if (_result.getClusterMap()[first] != _result.getClusterMap()[second]) {
Vector<Integer> cr = new Vector<Integer>(); // Vector<Integer> cr = new Vector<Integer>();
cr.add(_result.getClusterMap()[first]); // cr.add(_result.getClusterMap()[first]);
cr.add(_result.getClusterMap()[second]); // cr.add(_result.getClusterMap()[second]);
((LearningResultKmeans) learningResult).mergeSeeds(cr, _result.getClusterMap()); // ((LearningResultKmeans) learningResult).mergeSeeds(cr, _result.getClusterMap());
// _result = learningResult.classify(data, (data.getNbObjects() == _result.getClusterMap().length));
int[] tmpClusterMap = _result.getClusterMap().clone();
tmpClusterMap[second] = newClusterId;
tmpClusterMap[first] = newClusterId;
LightHardSeed hs = new LightHardSeed(data.getDataObject(second),
data, tmpClusterMap, 2, newClusterId);
((LearningResultKmeans) learningResult).seeds.add(hs);
} }
} }
} }
......
...@@ -1644,7 +1644,7 @@ public class HybridClassification extends Classification implements Runnable, It ...@@ -1644,7 +1644,7 @@ public class HybridClassification extends Classification implements Runnable, It
//<< start test //<< start test
try { try {
PrintWriter writer = new PrintWriter("log_samarah"+this.getName()+".log"); PrintWriter writer = new PrintWriter("log/log_samarah"+this.getName()+".log");
writer.print(""); writer.print("");
writer.close(); writer.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
......
...@@ -105,7 +105,7 @@ public class SamarahConflict { ...@@ -105,7 +105,7 @@ public class SamarahConflict {
//<<< test - start //<<< test - start
FileWriter fw = null; FileWriter fw = null;
try { try {
fw = new FileWriter("log_samarah"+eval.hybridClassification.getName()+".log", true); fw = new FileWriter("log/log_samarah"+eval.hybridClassification.getName()+".log", true);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -121,7 +121,7 @@ public class HybridClassification extends jcl.learning.methods.multistrategy.sam ...@@ -121,7 +121,7 @@ public class HybridClassification extends jcl.learning.methods.multistrategy.sam
//<< start test //<< start test
try { try {
PrintWriter writer = new PrintWriter("log_samarah"+this.getName()+".log"); PrintWriter writer = new PrintWriter("log/log_samarah"+this.getName()+".log");
writer.print(""); writer.print("");
writer.close(); writer.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment