The code below is a minimal working example of a circle estimator i'm using. It usses Accord.Net's cobyla optimizer. The input for the localize method is a set of points. The center location is found using the optimizer and returned. (My real code is a bit more complicated but I left out most less relevant stuff)
The code is working fine but the issue I have is with the static
fields (I have more of them in my real code). When I optimize multiple problems concurrently the static
field will give problems because the optimizer might read values intended for another thread.
The question is how do I make this code work for concurrent code? Can I ditch the statics and still get the points inside the Func<double[], double>
? Are there other ways to circumvent the concurrency problems with the statics?
internal class CircleLocalizer
{
private static (double, double)[] points; // I dislike these statics!
private double[] prior = { 0, 0 };
Func<double[], double> objective = x =>
{
var distancesToCenter = CircleLocalizer.points.Select(p => Distance.Euclidean(new[] { p.Item1, p.Item2 }, new[] { x[0], x[1] }));
var averageDistance = distancesToCenter.Average();
return distancesToCenter.Select(d => Math.Pow(d - averageDistance, 2)).Sum();
};
public double[] Localize((double, double) points)
{
CircleLocalizer.points = points;
Cobyla cobyla = new Cobyla(2, objective);
cobyla.Minimize(Prior);
return cobyla.Solution;
}
}
question from:
https://stackoverflow.com/questions/65883055/can-i-do-concurrent-cobyla-optimizations-using-accord-net 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…