[REEF-1663] Fix TestKMeansOnDirectRunViaFileSystem failure in AppVeyor
This change replaces exact comparison of floating-point numbers
with comparison with some absolute error.
JIRA:
[REEF-1663](https://issues.apache.org/jira/browse/REEF-1663)
Pull request:
This closes #1187
diff --git a/lang/cs/Org.Apache.REEF.Examples/MachineLearning/KMeans/KMeansMasterTask.cs b/lang/cs/Org.Apache.REEF.Examples/MachineLearning/KMeans/KMeansMasterTask.cs
index e85ecd6..d0f92db 100644
--- a/lang/cs/Org.Apache.REEF.Examples/MachineLearning/KMeans/KMeansMasterTask.cs
+++ b/lang/cs/Org.Apache.REEF.Examples/MachineLearning/KMeans/KMeansMasterTask.cs
@@ -32,6 +32,7 @@
public class KMeansMasterTask : ITask
{
private static readonly Logger Logger = Logger.GetLogger(typeof(KMeansMasterTask));
+ private const double Eps = 1E-6;
private int _iteration;
@@ -73,7 +74,6 @@
_centroids = new Centroids(DataPartitionCache.ReadDataFile(centroidFile));
float loss = float.MaxValue;
- float newLoss;
while (true)
{
@@ -88,23 +88,20 @@
ProcessedResults results = _meansReducerReceiver.Reduce();
_centroids = new Centroids(results.Means.Select(m => m.Mean).ToList());
Logger.Log(Level.Info, "Broadcasting new centroids to all slave nodes: " + _centroids);
- newLoss = results.Loss;
+ float newLoss = results.Loss;
Logger.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "The new loss value {0} at iteration {1} ", newLoss, _iteration));
- if (newLoss > loss)
+ if (newLoss > loss + Eps)
{
_controlBroadcastSender.Send(ControlMessage.STOP);
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture, "The new loss {0} is larger than previous loss {1}, while loss function must be monotonically decreasing across iterations", newLoss, loss));
}
- else if (newLoss.Equals(loss))
+ if (newLoss > loss - Eps)
{
Logger.Log(Level.Info, string.Format(CultureInfo.InvariantCulture, "KMeans clustering has converged with a loss value of {0} at iteration {1} ", newLoss, _iteration));
break;
}
- else
- {
- loss = newLoss;
- }
+ loss = newLoss;
}
_controlBroadcastSender.Send(ControlMessage.RECEIVE);
_dataBroadcastSender.Send(_centroids);
diff --git a/lang/cs/Org.Apache.REEF.Tests/Functional/ML/KMeans/TestKMeans.cs b/lang/cs/Org.Apache.REEF.Tests/Functional/ML/KMeans/TestKMeans.cs
index 0afda40..4db2b3b 100644
--- a/lang/cs/Org.Apache.REEF.Tests/Functional/ML/KMeans/TestKMeans.cs
+++ b/lang/cs/Org.Apache.REEF.Tests/Functional/ML/KMeans/TestKMeans.cs
@@ -19,13 +19,8 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
-using Org.Apache.REEF.Common.Io;
-using Org.Apache.REEF.Common.Tasks;
-using Org.Apache.REEF.Driver.Bridge;
using Org.Apache.REEF.Examples.MachineLearning.KMeans;
using Org.Apache.REEF.Network.Group.Config;
-using Org.Apache.REEF.Network.Naming;
-using Org.Apache.REEF.Network.NetworkService;
using Org.Apache.REEF.Tang.Implementations.Configuration;
using Org.Apache.REEF.Tang.Implementations.Tang;
using Org.Apache.REEF.Tang.Interface;
@@ -41,6 +36,7 @@
private const int K = 3;
private const int Partitions = 2;
private const string DataFileNamePrefix = "KMeansInput-";
+ private const double Eps = 1E-6;
public TestKMeans()
{
@@ -82,20 +78,17 @@
DataVector.WriteToCentroidFile(newCentroids, executionDirectory);
centroids = newCentroids;
float newLoss = LegacyKMeansTask.ComputeLossFunction(centroids, labeledData);
- if (newLoss > loss)
+ if (newLoss > loss + Eps)
{
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture, "The new loss {0} is larger than previous loss {1}, while loss function must be monotonically decreasing across iterations", newLoss, loss));
}
- else if (newLoss.Equals(loss))
+ if (newLoss > loss - Eps)
{
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "KMeans clustering has converged with a loss value of {0} at iteration {1} ", newLoss, iteration));
break;
}
- else
- {
- loss = newLoss;
- }
+ loss = newLoss;
iteration++;
}