How to clamp a value from 0 to infinite to a value from 1 to 0?What is JavaScript's highest integer value that a number can go to without losing precision?How to check if a number is a power of 2How can I check for NaN values?How to sum array of numbers in Ruby?How can a time function exist in functional programming?Statistical best fit for gesture detectionCovariance of a passed function argumentHow do I determine whether my calculation of pi is accurate?How to clamp a value from 0 to infinite to a value from 0 to 1?Genetic Algorithm Fitness Stop Increasing at Some Point

Can I disable a battery powered device by reversing half of its batteries?

I asked for a graduate student position from a professor. He replied "welcome". What does that mean?

Why did they ever make smaller than full-frame sensors?

Square roots and cube roots equation

Can I conceal an antihero's insanity - and should I?

Why does Coq include let-expressions in its core language

How can I discourage sharing internal API keys within a company?

Is there an inconsistency about Natasha Romanoff's middle name in the MCU?

What is my breathable atmosphere composed of?

How to say "quirky" in German without sounding derogatory?

Can the card disintegrate destroy creatures with indestructible?

Has SHA256 been broken by Treadwell Stanton DuPont?

How unbalanced coaxial cables are used for broadcasting TV signals without any problems?

Do ibuprofen or paracetamol cause hearing loss?

Modify width of first column in file with a variable number of fields, using awk

Selecting 2 column in an Inner join

Are scroll bars dead in 2019?

A shy person in a queue

What's the biggest organic molecule that could have a smell?

My research paper filed as a patent in China by my Chinese supervisor without me as inventor

How are aircraft depainted?

Do all humans have an identical nucleotide sequence for certain proteins, e.g haemoglobin?

What jurisdiction do Scottish courts have over the Westminster parliament?

Uncovering the Accelerated Dragon opening



How to clamp a value from 0 to infinite to a value from 1 to 0?


What is JavaScript's highest integer value that a number can go to without losing precision?How to check if a number is a power of 2How can I check for NaN values?How to sum array of numbers in Ruby?How can a time function exist in functional programming?Statistical best fit for gesture detectionCovariance of a passed function argumentHow do I determine whether my calculation of pi is accurate?How to clamp a value from 0 to infinite to a value from 0 to 1?Genetic Algorithm Fitness Stop Increasing at Some Point






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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










share|improve this question


























  • One language tag at a time please, thanks

    – cs95
    Mar 28 at 10:07












  • @MarkRotteveel Accidental.. thanks, fixed

    – cs95
    Mar 28 at 10:08











  • can't you just do 1 / (1 + v)?

    – Russ Hyde
    Mar 28 at 10:09











  • sorry, I just didn't know which language would be the right one

    – Edgar Ustian
    Mar 28 at 10:21

















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










share|improve this question


























  • One language tag at a time please, thanks

    – cs95
    Mar 28 at 10:07












  • @MarkRotteveel Accidental.. thanks, fixed

    – cs95
    Mar 28 at 10:08











  • can't you just do 1 / (1 + v)?

    – Russ Hyde
    Mar 28 at 10:09











  • sorry, I just didn't know which language would be the right one

    – Edgar Ustian
    Mar 28 at 10:21













0












0








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










share|improve this question
















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







scala math fitness






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 10:08









cs95

167k32 gold badges241 silver badges304 bronze badges




167k32 gold badges241 silver badges304 bronze badges










asked Mar 28 at 10:06









Edgar UstianEdgar Ustian

12 bronze badges




12 bronze badges















  • One language tag at a time please, thanks

    – cs95
    Mar 28 at 10:07












  • @MarkRotteveel Accidental.. thanks, fixed

    – cs95
    Mar 28 at 10:08











  • can't you just do 1 / (1 + v)?

    – Russ Hyde
    Mar 28 at 10:09











  • sorry, I just didn't know which language would be the right one

    – Edgar Ustian
    Mar 28 at 10:21

















  • One language tag at a time please, thanks

    – cs95
    Mar 28 at 10:07












  • @MarkRotteveel Accidental.. thanks, fixed

    – cs95
    Mar 28 at 10:08











  • can't you just do 1 / (1 + v)?

    – Russ Hyde
    Mar 28 at 10:09











  • sorry, I just didn't know which language would be the right one

    – Edgar Ustian
    Mar 28 at 10:21
















One language tag at a time please, thanks

– cs95
Mar 28 at 10:07






One language tag at a time please, thanks

– cs95
Mar 28 at 10:07














@MarkRotteveel Accidental.. thanks, fixed

– cs95
Mar 28 at 10:08





@MarkRotteveel Accidental.. thanks, fixed

– cs95
Mar 28 at 10:08













can't you just do 1 / (1 + v)?

– Russ Hyde
Mar 28 at 10:09





can't you just do 1 / (1 + v)?

– Russ Hyde
Mar 28 at 10:09













sorry, I just didn't know which language would be the right one

– Edgar Ustian
Mar 28 at 10:21





sorry, I just didn't know which language would be the right one

– Edgar Ustian
Mar 28 at 10:21












2 Answers
2






active

oldest

votes


















2
















Maths, not programming. But...



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


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






share|improve this answer

























  • 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 at 10:28



















2
















How about



fitness = 1/(1+x);


a function that goes towards 0 as x increases






share|improve this answer

























  • 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 at 10:30






  • 2





    i feel you bro. happens to me and my mates all the time.

    – Alan
    Mar 28 at 11:08













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55394877%2fhow-to-clamp-a-value-from-0-to-infinite-to-a-value-from-1-to-0%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















Maths, not programming. But...



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


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






share|improve this answer

























  • 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 at 10:28
















2
















Maths, not programming. But...



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


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






share|improve this answer

























  • 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 at 10:28














2














2










2









Maths, not programming. But...



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


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






share|improve this answer













Maths, not programming. But...



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


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







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 10:14









AmadanAmadan

144k14 gold badges156 silver badges212 bronze badges




144k14 gold badges156 silver badges212 bronze badges















  • 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 at 10:28


















  • 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 at 10:28

















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 at 10:28






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 at 10:28














2
















How about



fitness = 1/(1+x);


a function that goes towards 0 as x increases






share|improve this answer

























  • 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 at 10:30






  • 2





    i feel you bro. happens to me and my mates all the time.

    – Alan
    Mar 28 at 11:08















2
















How about



fitness = 1/(1+x);


a function that goes towards 0 as x increases






share|improve this answer

























  • 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 at 10:30






  • 2





    i feel you bro. happens to me and my mates all the time.

    – Alan
    Mar 28 at 11:08













2














2










2









How about



fitness = 1/(1+x);


a function that goes towards 0 as x increases






share|improve this answer













How about



fitness = 1/(1+x);


a function that goes towards 0 as x increases







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 10:14









AlanAlan

5321 silver badge19 bronze badges




5321 silver badge19 bronze badges















  • 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 at 10:30






  • 2





    i feel you bro. happens to me and my mates all the time.

    – Alan
    Mar 28 at 11:08

















  • 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 at 10:30






  • 2





    i feel you bro. happens to me and my mates all the time.

    – Alan
    Mar 28 at 11:08
















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 at 10:30





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 at 10:30




2




2





i feel you bro. happens to me and my mates all the time.

– Alan
Mar 28 at 11:08





i feel you bro. happens to me and my mates all the time.

– Alan
Mar 28 at 11:08


















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55394877%2fhow-to-clamp-a-value-from-0-to-infinite-to-a-value-from-1-to-0%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript