Skip to content

Commit

Permalink
Making the thread-capable version the default ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Petitjean committed Aug 1, 2018
1 parent 6efd7f5 commit ae1597d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 52 deletions.
43 changes: 18 additions & 25 deletions DBA.java
Expand Up @@ -26,44 +26,36 @@
* This toy class show the use of DBA.
* @author Francois Petitjean
*/
public class DBA {
public class DBA{
static final long serialVersionUID = 1L;

private final static int NIL = -1;
private final static int DIAGONAL = 0;
private final static int LEFT = 1;
private final static int UP = 2;

/**
* This attribute is used in order to initialize only once the matrixes
*/
private final static int MAX_SEQ_LENGTH = 2000;

/**
* store the cost of the alignment
*/
private static double[][] costMatrix = new double[MAX_SEQ_LENGTH][MAX_SEQ_LENGTH];

/**
* store the warping path
*/
private static int[][] pathMatrix = new int[MAX_SEQ_LENGTH][MAX_SEQ_LENGTH];

/**
* Performs the DBA averaging by first finding the median over a sample, then doing n iterations of the update
* @param sequences set of sequences to average
*/
public static double[] performDBA(double[][] sequences) {
int medoidIndex = approximateMedoidIndex(sequences);

int maxLength=0;
for (int i = 0; i < sequences.length; i++) {
maxLength = Math.max(maxLength,sequences[i].length);
}
double[][]costMatrix = new double[maxLength][maxLength];
int[][]pathMatrix = new int[maxLength][maxLength];
int medoidIndex = approximateMedoidIndex(sequences,costMatrix);
double[]center = Arrays.copyOf(sequences[medoidIndex], sequences[medoidIndex].length);

for (int i = 0; i < 15; i++) {
center = DBAUpdate(center, sequences);
center = DBAUpdate(center, sequences,costMatrix,pathMatrix);
}
return center;
}

private static int approximateMedoidIndex(double[][] sequences) {
private static int approximateMedoidIndex(double[][] sequences,double[][]mat) {
/*
* we are finding the medoid, as this can take a bit of time,
* if there is more than 50 time series, we sample 50 as possible
Expand All @@ -84,7 +76,7 @@ private static int approximateMedoidIndex(double[][] sequences) {

for (int medianCandidateIndex:medianIndices) {
double[] possibleMedoid = sequences[medianCandidateIndex];
double tmpSoS = sumOfSquares(possibleMedoid, sequences);
double tmpSoS = sumOfSquares(possibleMedoid, sequences,mat);
if (tmpSoS < lowestSoS) {
indexMedoid = medianCandidateIndex;
lowestSoS = tmpSoS;
Expand All @@ -93,16 +85,16 @@ private static int approximateMedoidIndex(double[][] sequences) {
return indexMedoid;
}

private static double sumOfSquares(double[]sequence,double[][]sequences) {
private static double sumOfSquares(double[]sequence,double[][]sequences,double[][]mat) {
double sos = 0.0;
for (int i = 0; i < sequences.length; i++) {
double dist = DTW(sequence,sequences[i]);
double dist = DTW(sequence,sequences[i],mat);
sos += dist*dist;
}
return sos;
}

public static synchronized double DTW(double[]S,double []T) {
public static double DTW(double[]S,double []T,double[][]costMatrix) {
int i, j;
costMatrix[0][0] = squaredDistance(S[0],T[0]);
for (i = 1; i < S.length; i++) {
Expand All @@ -122,7 +114,7 @@ public static synchronized double DTW(double[]S,double []T) {
return sqrt(costMatrix[S.length - 1][T.length - 1]);
}

private static synchronized double[] DBAUpdate(double[] C, double[][] sequences) {
private static double[] DBAUpdate(double[] C, double[][] sequences,double[][]costMatrix,int[][]pathMatrix) {
double[]updatedMean = new double[C.length];
int[]nElementsForMean= new int[C.length];

Expand Down Expand Up @@ -232,7 +224,8 @@ private static int ArgMin3(final double a, final double b, final double c) {
}

private static double squaredDistance(double a, double b) {
return (a - b) * (a - b);
double diff = a-b;
return diff*diff;
}

public static void main(String [] args){
Expand Down
46 changes: 19 additions & 27 deletions DBAWarpingWindow.java
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2016 Chang Wei Tan
* Copyright (C) 2018 Chang Wei Tan, François Petitjean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -25,45 +25,37 @@
* This toy class show the use of DBA with warping window.
* @author Chang Wei Tan
*/
public class DBAWarpingWindow {
public class DBAWarpingWindow{
static final long serialVersionUID = 1L;

private final static int NIL = -1;
private final static int DIAGONAL = 0;
private final static int LEFT = 1;
private final static int UP = 2;

/**
* This attribute is used in order to initialize only once the matrixes
*/
private final static int MAX_SEQ_LENGTH = 2000;

/**
* store the cost of the alignment
*/
private static double[][] costMatrix = new double[MAX_SEQ_LENGTH][MAX_SEQ_LENGTH];

/**
* store the warping path
*/
private static int[][] pathMatrix = new int[MAX_SEQ_LENGTH][MAX_SEQ_LENGTH];

/**
* Performs the DBA averaging by first finding the median over a sample, then doing n iterations of the update
* @param sequences set of sequences to average
* @param w warping window
*/
public static double[] performDBA(double[][] sequences,int w) {
int medoidIndex = approximateMedoidIndex(sequences,w);
int maxLength=0;
for (int i = 0; i < sequences.length; i++) {
maxLength = Math.max(maxLength,sequences[i].length);
}
double[][]costMatrix = new double[maxLength][maxLength];
int[][]pathMatrix = new int[maxLength][maxLength];

int medoidIndex = approximateMedoidIndex(sequences,w,costMatrix);
double[]center = Arrays.copyOf(sequences[medoidIndex], sequences[medoidIndex].length);

for (int i = 0; i < 15; i++) {
center = DBAUpdate(center, sequences,w);
center = DBAUpdate(center, sequences,w,costMatrix,pathMatrix);
}
return center;
}

private static int approximateMedoidIndex(double[][] sequences,int w) {
private static int approximateMedoidIndex(double[][] sequences,int w,double[][]costMatrix) {
/*
* we are finding the medoid, as this can take a bit of time,
* if there is more than 50 time series, we sample 50 as possible
Expand All @@ -84,7 +76,7 @@ private static int approximateMedoidIndex(double[][] sequences,int w) {

for (int medianCandidateIndex:medianIndices) {
double[] possibleMedoid = sequences[medianCandidateIndex];
double tmpSoS = sumOfSquares(possibleMedoid, sequences,w);
double tmpSoS = sumOfSquares(possibleMedoid, sequences,w,costMatrix);
if (tmpSoS < lowestSoS) {
indexMedoid = medianCandidateIndex;
lowestSoS = tmpSoS;
Expand All @@ -93,16 +85,16 @@ private static int approximateMedoidIndex(double[][] sequences,int w) {
return indexMedoid;
}

private static double sumOfSquares(double[]sequence,double[][]sequences, int w) {
private static double sumOfSquares(double[]sequence,double[][]sequences, int w,double[][]costMatrix) {
double sos = 0.0;
for (int i = 0; i < sequences.length; i++) {
double dist = DTW(sequence,sequences[i],w);
double dist = DTW(sequence,sequences[i],w,costMatrix);
sos += dist*dist;
}
return sos;
}

public static synchronized double DTW(double[]S,double []T, int w) {
public static double DTW(double[]S,double []T, int w,double[][]costMatrix) {
int i, j;
costMatrix[0][0] = squaredDistance(S[0],T[0]);
for (i = 1; i < Math.min(S.length,w+1); i++) {
Expand Down Expand Up @@ -138,7 +130,7 @@ public static synchronized double DTW(double[]S,double []T, int w) {
* @param sequences set of sequences to average
* @param w warping window size for DTW
*/
private static synchronized double[] DBAUpdate(double[] C, double[][] sequences, int w) {
private static double[] DBAUpdate(double[] C, double[][] sequences, int w,double[][]costMatrix,int[][]pathMatrix) {
double[]updatedMean = new double[C.length];
int[]nElementsForMean= new int[C.length];

Expand Down Expand Up @@ -260,11 +252,11 @@ private static double squaredDistance(double a, double b) {


public static void main(String [] args){
int w = 5;
int w = 1000;
Random r = new Random(3071980);
double [][]sequences = new double[50][];
for(int i=0;i<sequences.length;i++){
sequences[i] = new double[20];
sequences[i] = new double[12000];
for(int j=0;j<sequences[i].length;j++){
sequences[i][j] = Math.cos(r.nextDouble()*j/20.0*Math.PI) ;
}
Expand Down

0 comments on commit ae1597d

Please sign in to comment.