0

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

cs95
  • 379,657
  • 97
  • 704
  • 746

2 Answers2

2

Maths, not programming. But...

fitness(x) = 2 / (exp(x) + 1)

is sigmoid function adapted for your requirements (fitness(0) = 1, fitness(inf) = 0).

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Thank you very much for this function, but I found out that the values that it gives back are way too small for the program to not count as just 0.0. For some reason, I did not think of something as simple as fitness = 1/(1+x); Maybe it's my lack of sleep who knows, but thank you very much for your answer – Edgar Ustian Mar 28 '19 at 10:28
2

How about

fitness = 1/(1+x);

a function that goes towards 0 as x increases

Alan
  • 949
  • 8
  • 26
  • Why did I not think of something this simple? I think my brain is just slowly giving up on me for all the lack of sleep that I made it go through. But thank you very much for this simple solution – Edgar Ustian Mar 28 '19 at 10:30
  • 2
    i feel you bro. happens to me and my mates all the time. – Alan Mar 28 '19 at 11:08