I'm trying to create a fitness function, where I can calculate the score of a value. The closer it gets to x, the higher the value. Yes it could be linear, but I'd prefer to have it exponential. I just don't know what term to even google for anymore...

Basically the idea is the same as a 100 meter dash runner going from 23 seconds to 22 seconds is not a big deal, but going from 9.9 to 9.8 is a dramatic difference. Except that in this case, instead of just above a min value, I want it to be as close as possible. So for example cutting a board in half, the closer to the middle on either side, the better the score.

So in the graph above, y is the max score I want to assign, it could be say 100, 1000, etc. pts. In terms of ideal value, I'd like to be able to say the ideal is maybe 12 and they managed to achieve 11. What score (y) do they get?

They should also be able to score anywhere from 0 to infinity, but as it gets closer to 0 and as it goes to infinity (or a max value), how do I determine the score?

In other words, what would the equation be?

  • $\begingroup$ There are several functions that loosely fit your qualitative criteria. Have you considered a normal distribution? $\endgroup$ – Omnomnomnom Aug 10 '13 at 2:37
  • $\begingroup$ You mention a maximum score, but then say they should be able to score anywhere from 0 to infinity. That seems like a contradiction to me. Can you clarify? $\endgroup$ – hasnohat Aug 10 '13 at 2:49
  • $\begingroup$ @Julien that is receive a score (y value) for any x value from 0 to infinity. The score is maxed, but the x value can be anything. $\endgroup$ – Stephane Grenier Aug 10 '13 at 3:35
  • $\begingroup$ @Omnomnomnom A normal distribution could work... Although I understand the theories behind it, now I have to learn how to play with the math. But yeah I think you got it. And if you have a good link on how to play with the variables in the equation, that would be awesome!! $\endgroup$ – Stephane Grenier Aug 10 '13 at 3:37

You could try

$$ f(x) = \frac{h}{(x-a)^2+1} $$

Where $a$ is the target value, and $h$ is the maximum score.

To get a narrower/wider peak, you can introduce a third parameter, $b$.

$$ f(x) = \frac{h}{b(x-a)^2+1} $$

This has the advantage of being algebraically simple.

If you want to have $f(0)=0$, you could use

$$ f(x) = \frac{hx}{b(x-a)^2+1} $$

Notice, though, that the maximum for this function is no longer at $x=a$. You can use some basic calculus to find that information.


If you want to let the score go to ∞ you can use a function like 1/(x-a) where I changed the name of your target value to "a" so as to eliminate confusion with the x axis. This has the advantage of being easy to compute for any x.

If you want to assign a maximum y, an easy way to write the function is still as f(x) = 1/(x-a) if 1/(x-a) ≤ y (that's your maximum y); and f(x) = y for 1/(x-a) > y. This means that you assign a maximum score if they get really close to a.

The term really close is kind of vague, but you can define it as any given distance you like from a (say 1/10 or 1/100) and then adjust coefficients in f(x) to force it to reach y within that interval.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.