Commit 7320b50c authored by lafabregue's avatar lafabregue

modification of DBA

parent 32bb3000
......@@ -18,6 +18,7 @@ import jcl.data.distance.average.Average;
import jcl.data.distance.average.AverageParameter;
import jcl.data.distance.average.EmptyAverageParameter;
import jcl.data.distance.average.sequential.AverageMDDBAMean;
import jcl.data.distance.average.sequential.AverageMDDBAMeanOptimized;
import jcl.data.distance.average.sequential.AverageMDeuclideanMean;
import jcl.data.distance.average.sequential.ParameterDBAMean;
import jcl.data.distance.average.unitary.AverageEuclidean;
......@@ -100,7 +101,7 @@ public class DistanceModel {
// }
if (dataObject.getAttribute(i) instanceof AttributeMultiDimSequence) {
distances[i] = DistanceDTWMD.getInstance();// uses DTWMD distances for sequential attributes ...
averages[i] = AverageMDDBAMean.getInstance();// uses MultiDim DBA mean for sequential attributes ...
averages[i] = AverageMDDBAMeanOptimized.getInstance();// uses MultiDim DBA mean for sequential attributes ...
}
else {
distances[i]=NumericalEuclideanDistance.getInstance();//... and euclidean distances for numerical attributes
......
package jcl.data.distance.average.sequential;
import static jcl.data.distance.Tools.distanceTo;
import jcl.data.attribute.Attribute;
import jcl.data.attribute.AttributeMultiDimSequence;
import jcl.data.distance.average.Average;
public class AverageMDDBAMeanOptimized implements Average<AttributeMultiDimSequence,ParameterDBAMean> {
/**
* Singleton
*/
private static AverageMDDBAMeanOptimized instance = new AverageMDDBAMeanOptimized();
/**
* Empty constructor
*/
private AverageMDDBAMeanOptimized(){}
/**
* To get the singleton
* @return the unique instance
*/
public static AverageMDDBAMeanOptimized getInstance(){
return instance;
}
private final static int[] moveI = { -1, 0, -1 };
private final static int[] moveJ = { -1, -1, 0 };
@Override
public AttributeMultiDimSequence mean(Attribute[] attributesTab, AttributeMultiDimSequence oldCenter,
ParameterDBAMean p) {
AttributeMultiDimSequence res = null;
if (oldCenter != null) {
res = DBAMean(attributesTab, oldCenter, p);
} else {
int alea = (int) (Math.round(Math.random() * (attributesTab.length - 1)));
res = DBAMean(attributesTab, (AttributeMultiDimSequence)attributesTab[alea], p);
}
for (int i = 0; i < p.getNbIterations(); i++) {
res = DBAMean(attributesTab, res, p);
}
return res;
}
private AttributeMultiDimSequence DBAMean(Attribute[] sequences, AttributeMultiDimSequence C, ParameterDBAMean p) {
AttributeMultiDimSequence updatedMean = (AttributeMultiDimSequence) C.clone();
int[] nElementsForMean = new int[C.getNbTuples()];
int i, j,move;
double res = 0.0;
int centerLength = C.getNbTuples();
int seqLength;
for (Attribute aT : sequences) {
AttributeMultiDimSequence T = (AttributeMultiDimSequence) aT;
seqLength = T.getNbTuples();
p.getMatriceW()[0][0] = distanceTo(C.sequence[0], T.sequence[0]);
p.getMatriceChoix()[0][0] = ParameterDBAMean.RIEN;
for (i = 1; i < centerLength; i++) {
p.getMatriceW()[i][0] = p.getMatriceW()[i - 1][0] + distanceTo(C.sequence[i], T.sequence[0]);
p.getMatriceChoix()[i][0] = ParameterDBAMean.HAUT;
}
for (j = 1; j < seqLength; j++) {
p.getMatriceW()[0][j] = p.getMatriceW()[0][j - 1] + distanceTo(T.sequence[j], C.sequence[0]);
p.getMatriceChoix()[0][j] = ParameterDBAMean.GAUCHE;
}
for (i = 1; i < centerLength; i++) {
for (j = 1; j < seqLength; j++) {
double diag = p.getMatriceW()[i - 1][j - 1], left = p.getMatriceW()[i][j - 1], top = p.getMatriceW()[i - 1][j];
if (diag <= left) {
if (diag <= top) {
res = diag;
move = ParameterDBAMean.DIAGONALE;
} else {
res = top;
move = ParameterDBAMean.HAUT;
}
} else {
if (left <= top) {
res = left;
move = ParameterDBAMean.GAUCHE;
} else {
res = top;
move = ParameterDBAMean.HAUT;
}
}
p.getMatriceChoix()[i][j] = move;
res = p.getMatriceW()[i+moveI[move]][j+moveJ[move]];
p.getMatriceW()[i][j] = res + distanceTo(C.sequence[i], T.sequence[j]);
}
}
i = centerLength - 1;
j = seqLength - 1;
while (p.getMatriceChoix()[i][j] != ParameterDBAMean.RIEN) {
for(int k = 0 ; k < T.getTupleDimension() ; k++) {
updatedMean.sequence[i][k] += T.sequence[j][k];
}
nElementsForMean[i]++;
move = p.getMatriceChoix()[i][j];
i += moveI[move];
j += moveJ[move];
}
assert (i != 0 || j != 0);
for(int k = 0 ; k < T.getTupleDimension() ; k++) {
updatedMean.sequence[i][k] += T.sequence[j][k];
}
nElementsForMean[i]++;
}
for (int t = 0; t < centerLength; t++) {
for(int k = 0 ; k < updatedMean.getTupleDimension() ; k++) {
updatedMean.sequence[t][k] /= nElementsForMean[t];
}
}
return updatedMean;
}
}
......@@ -1388,6 +1388,163 @@ public class HybridClassification extends Classification implements Runnable, It
return tmp;
}
/**
* <p>
* Method to unify the result from classification agents
* </p>
*
* @param data
* the data to unify over
* @param onSample
* run the unification on the sample if true, on the whole data otherwise
* @return the unification result
*/
public ClusteringResult unification_relative(Data data, boolean onSample) {
LearningAgent a;
int[] stat;
int[] ff;
int[] ag;
int[] cl;
int[][] vote;
int[][] clustermaps;
// nombre d'agents au total
final int nbre_agents = this.agents.size();
// nombre d'objets classifies : on prend le plus gros
int nb_obj = 0;
if (onSample) {
nb_obj = data.getNbObjects();
} else {
nb_obj = data.getWholeDataNbObjects();
}
// Initialisation des parametres et de la matrice
stat = new int[nb_obj];
ff = new int[nb_obj];
ag = new int[nb_obj];
cl = new int[nb_obj];
for (int p = 0; p < nb_obj; p++) {
stat[p] = 0;
ff[p] = 0;
ag[p] = -1;
cl[p] = -1;
}
vote = new int[nbre_agents][];
for (int i = 0; i < nbre_agents; i++) {
a = this.getAgent(i);
vote[i] = new int[a.getNbClusters()];
}
// compute the cluster map for each agent
clustermaps = new int[nbre_agents][];
for (int i = 0; i < nbre_agents; i++) {
a = this.getAgent(i);
if (a.getData() == this.data) {
clustermaps[i] = a.getClassification().getClusteringResult()
.getClusterMap(onSample);
} else {
clustermaps[i] = a.getClassification().getLearningResult()
.classify(data, onSample).getClusterMap(onSample);
}
}
// Calcul de la matrice de vote sur les objets
for (int p = 0; p < nb_obj; p++) {
for (int i = 0; i < nbre_agents; i++) {
a = this.getAgent(i);
for (int j = 0; j < a.getNbClusters(); j++) {
vote[i][j] = 0;
}
}
for (int i = 0; i < nbre_agents; i++) {
a = this.getAgent(i);
final int k = clustermaps[i][p];
if (k >= 0) {
vote[i][k] += 1;
}
for (int j = 0; j < nbre_agents; j++) {
if ((j != i) && (k >= 0)) {
final int km = this.evalMax.alpha[i][j].maxColumn(k);
a = this.getAgent(j);
final int kj = clustermaps[j][p];
vote[j][km] += 1;
if (kj == km) {
stat[p] += 1;
}
}
}
}
int max = vote[0][0];
for (int i = 0; i < nbre_agents; i++) {
a = this.getAgent(i);
for (int j = 0; j < a.getNbClusters(); j++) {
if (vote[i][j] > max) {
max = vote[i][j];
ag[p] = i;
cl[p] = j;
}
}
}
}
final int[] agree = new int[12];
for (int i = 0; i < stat.length; i++) {
agree[(int) ((float) stat[i] / (float) nbre_agents + 0.5)]++;
}
final Vector<Integer> values = new Vector<Integer>();
for (int p = 0; p < nb_obj; p++) {
int classe = -1;
stat[p] = (int) ((float) stat[p] / (float) nbre_agents + 0.5);
if ((this.config.unification == SamarahConfig.WITH_UNCLASSIFIED_UNIFICATION && stat[p] >= ((nbre_agents - 1) / 2))
|| this.config.unification == SamarahConfig.WITHOUT_UNCLASSIFIED_UNIFICATION) {
classe = ag[p] + (cl[p] + 1) * nbre_agents;
}
final Integer val = new Integer(classe);
if (!values.contains(val)) {
values.add(val);
}
ff[p] = values.indexOf(val);
}
final int nbClasses = values.size();
double resoMin = 0;
int agentMin = 0;
try {
resoMin = (this.agents.get(0)).getData().getResolution();
for (int x = 0; x < this.agents.size(); x++) {
if ((this.agents.get(0)).getData().getResolution() < resoMin) {
resoMin = (this.agents.get(0)).getData().getResolution();
agentMin = x;
}
}
} catch (final ClassCastException ex) {
agentMin = 0;
}
this.overall_stat = ff;
if (this.first_stat == null) {
this.first_stat = stat;
}
//final Data biggestData = (this.agents.get(agentMin)).getData();
final ClusteringResult tmp = ClusteringResult.gerenerateDefaultClusteringResult(null, ff,
new GlobalWeights(data.getOneDataObject()), nbClasses, data,
HybridClassification.QUALITY_INDICES, importedColors);
importedColors = null;
return tmp;
}
/**
* <p>
* Methode gardant uniquement les pires conflits.
......
......@@ -425,7 +425,7 @@ public class LearningAgent implements Runnable, Serializable {
*/
public void split(final int c, final int n)
throws MethodNotImplementedException {
if (this.classification.getClusteringResult().getCard(c) >= 2) {
if (this.classification.getClusteringResult().getCard(c) >= n) {
this.classification.split(c, n);
}
}
......
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