Reputation: 1
I am creating a program in Scala that needs to generate a fitness function based on the line that I provide it with.
To generate the fitness function I wrote a method that takes a list of points and returns a fitness function based on those points.
This fitness function should measure the sum of the distances of each point from the line in terms of their y values. And the lower the sum the higher the fitness.
But now I am stuck, because I can't figure out how I can transform the sum of the distances from 0 to Infinite to a Double which is from 1 to 0, 1 the best fit and 0 being the worst fit line.
Any ideas or maths equation ? Thank you in advance
I have already tried to clamp this value using the tanh function only to realize that it works horribly for larger values. I have also tried doing it using:
fitness = 1 - Math.atan(x)/(Math.PI/2);
So that I could maybe get the reverse answer, but it didn't work :'^)
This is the code that pertains to how my program runs:
//Point Class that is just a coordinate point (x, y)
class Point(val x: Double, val y: Double) {
}
//Line Class this is just a line with y = slope * x + yIntercept
class Line(val slope: Double, val yIntercept: Double) {
def evaluate(x: Double): Double = {
slope * x + yIntercept
}
}
def lineFitFuncGen(points: List[Point]): Line => Double = {
//Sum of the distances using the line given
line: Line => {
var lineSum: Double = 0.0
for (point <- points) {
lineSum += Math.abs(line.evaluate(point.x) - point.y)
}
lineSum
}
}
I run the program and I get the sum but now I don't know how to take this sum and convert it into a range of 1 to 0. And I want to make it so that my lowest sum possible, which is 0 gives me fitness of 1, while my highest sum possible, which is Infinity gives me fitness of 0
Upvotes: 0
Views: 578
Reputation: 976
How about
fitness = 1/(1+x);
a function that goes towards 0 as x increases
Upvotes: 2
Reputation: 198314
Maths, not programming. But...
fitness(x) = 2 / (exp(x) + 1)
is sigmoid function adapted for your requirements (fitness(0) = 1
, fitness(inf) = 0
).
Upvotes: 2