private void btn_gDPDP_Click(object sender, EventArgs e) { TimeSeriesPair res = new TimeSeriesPair(test1, test2); res.DTW(); TimeSeries center = res.gDPDP(); showMessage(res, center); }
private void showMessage(TimeSeriesPair res, TimeSeries center) { TimeSeriesPair m13 = new TimeSeriesPair(test1, center); m13.DTW(); TimeSeriesPair m23 = new TimeSeriesPair(test2, center); m23.DTW(); string line = "error = " + string.Format("{0:f}", m13.error + m23.error); lbxData.Items.Add(line); }
public double mDPDP( ArrayList samples, ListBox lbx, int classIndex, int classCount, TimeSeries seed) { if (samples.Count == 0) { return(0.0); } double error = 0.0; int groupCount = 4; TimeSeries[] seedList = new TimeSeries[groupCount]; int[] numberList = new int[samples.Count]; ArrayList[] dataList = new ArrayList[groupCount]; for (int i = 0; i < samples.Count; i++) { numberList[i] = i; } Random random = new Random(); for (int i = 0; i < groupCount; i++) { dataList[i] = new ArrayList(); int lastIndex = samples.Count - i - 1; int randIndex = random.Next(lastIndex + 1); int randValue = numberList[randIndex]; numberList[randIndex] = numberList[lastIndex]; seedList[i] = (TimeSeries)samples[randValue]; } int tempGroup = groupCount; while (tempGroup > 0) { for (int i = 0; i < tempGroup; i++) { dataList[i].Clear(); seedList[i].weight = 1.0; } double[][] dtwMatrix = new double[samples.Count][]; for (int i = 0; i < samples.Count; i++) { TimeSeries ts = (TimeSeries)samples[i]; dtwMatrix[i] = new double[tempGroup]; for (int j = 0; j < tempGroup; j++) { TimeSeriesPair pair = new TimeSeriesPair(seedList[j], ts); pair.DTW(); dtwMatrix[i][j] = pair.error; } } for (int i = 0; i < samples.Count; i++) { TimeSeries ts = (TimeSeries)samples[i]; int k = 0; double minError = 1.0e20; for (int j = 0; j < tempGroup; j++) { if (dataList[j].Count > (samples.Count / tempGroup + 1)) { continue; } if (dtwMatrix[i][j] < minError) { minError = dtwMatrix[i][j]; k = j; } } dataList[k].Add(ts); } for (int i = 0; i < tempGroup; i++) { int loopMax = (tempGroup == 1) ? 30 : 15; for (int j = 0; j < loopMax; j++) { error = dbaStep(seedList[i], dataList[i]); string line = string.Format( "class = {0:d}/{1:d}; size = {2:d}; group = {3:d}/{4:d}; loop = {5:d}; error = {6:f}, avgError = {7:f}", classIndex, classCount, dataList[i].Count, i + 1, tempGroup, j, error, error / dataList[i].Count ); lbx.Items.Add(line); lbx.SetSelected(lbx.Items.Count - 1, true); Application.DoEvents(); } } if (tempGroup > 1) { ArrayList pairList = new ArrayList(); //calculate the pairwise DTW distances among the group seeds for (int i = 0; i < tempGroup; i++) { for (int j = 0; j < tempGroup; j++) { if (i == j) { continue; } TimeSeriesPair pair = new TimeSeriesPair(seedList[i], seedList[j]); pair.DTW(); pairList.Add(new DTWPair(pair.error, i, dataList[i].Count, j, dataList[j].Count )); } } DTWPair[] pairVect = getPairsFromList(pairList, tempGroup / 2); //Each element of pairVect is a pair of indice which corresponding seeds will be merged. TimeSeries[] newSeeds = new TimeSeries[tempGroup / 2]; for (int i = 0; i < tempGroup / 2; i++) { int i0 = pairVect[i].i0; int i1 = pairVect[i].i1; double w0 = pairVect[i].w0; double w1 = pairVect[i].w1; TimeSeries ts0 = (TimeSeries)seedList[i0]; TimeSeries ts1 = (TimeSeries)seedList[i1]; if (Math.Abs(w0 + w1) < 0.000001) { newSeeds[i] = ts0.clone(); } else { ts0.weight = w0 / (w0 + w1); ts1.weight = w1 / (w0 + w1); TimeSeriesPair pair = new TimeSeriesPair(ts0, ts1); newSeeds[i] = pair.gDPDP(); //Merge the two old seeds into a new seed. } } for (int i = 0; i < tempGroup / 2; i++) { seedList[i] = newSeeds[i]; //update new seeds } } tempGroup /= 2; //the group count becomes the half } seed.copy(seedList[0]); return(error); }