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 {
private static final long serialVersionUID = 1L;
/** Le resultat de la classification qui contient ce cluster */
protected final ClusteringResult result;
protected ClusteringResult result;
/** The cluster's gravity center */
protected DataObject clusterCenter = null;
......@@ -254,6 +254,17 @@ abstract public class Cluster implements Serializable {
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>
* Test si la classe est lockee.
......
......@@ -63,6 +63,7 @@ public class SimpleClusteringResult extends ClusteringResult {
this.clusters = new Cluster[result.clusters.length];
for (int i = 0; i < result.clusters.length; i++) {
this.clusters[i] = (Cluster) result.clusters[i].clone();
this.clusters[i].setResult(this);
}
this.weights = (ClassificationWeights) result.weights.clone();
......
......@@ -413,6 +413,7 @@ public class ClassifierKmeans extends LearningMethod {
public ClusteringResult injectConstraints(LearningResult learningResult, ClusteringResult _result, Data data,
Vector<Constraint> constraints) throws MethodNotImplementedException {
for (Constraint c : constraints) {
int newClusterId = ((LearningResultKmeans) learningResult).seeds.size();
if (c instanceof CannotLinkConstraint) {
// in the case of CL we create a new seed with the farthest element
CannotLinkConstraint cl = (CannotLinkConstraint) c;
......@@ -424,22 +425,30 @@ public class ClassifierKmeans extends LearningMethod {
((LearningResultKmeans) learningResult).seeds.get(_result.getClusterMap()[first])
.distance(data.getDataObject(second), learningResult.getWeights().getGlobalWeights())) {
((LearningResultKmeans) learningResult).seeds.add(
new LightHardSeed(data.getDataObject(first), data, ((LearningResultKmeans) learningResult).seeds.size()));
new LightHardSeed(data.getDataObject(first), data, newClusterId));
} else {
((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) {
// 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;
int first = ml.getFirstIndex();
int second = ml.getSecondIndex();
if (_result.getClusterMap()[first] != _result.getClusterMap()[second]) {
Vector<Integer> cr = new Vector<Integer>();
cr.add(_result.getClusterMap()[first]);
cr.add(_result.getClusterMap()[second]);
((LearningResultKmeans) learningResult).mergeSeeds(cr, _result.getClusterMap());
// Vector<Integer> cr = new Vector<Integer>();
// cr.add(_result.getClusterMap()[first]);
// cr.add(_result.getClusterMap()[second]);
// ((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
//<< start test
try {
PrintWriter writer = new PrintWriter("log_samarah"+this.getName()+".log");
PrintWriter writer = new PrintWriter("log/log_samarah"+this.getName()+".log");
writer.print("");
writer.close();
} catch (FileNotFoundException e) {
......
......@@ -105,7 +105,7 @@ public class SamarahConflict {
//<<< test - start
FileWriter fw = null;
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) {
e.printStackTrace();
}
......
......@@ -121,7 +121,7 @@ public class HybridClassification extends jcl.learning.methods.multistrategy.sam
//<< start test
try {
PrintWriter writer = new PrintWriter("log_samarah"+this.getName()+".log");
PrintWriter writer = new PrintWriter("log/log_samarah"+this.getName()+".log");
writer.print("");
writer.close();
} 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