Lord Klotski's Calculator

For discussion of non-RuneScape topics. PLEASE keep it friendly and avoid the usual controversial topics so things don't get out of hand.

Lord Klotski's Calculator

Postby Lord Klotski » Tue Oct 28, 2008 1:14 am

Today I had a few hours to kill, so I decided to write a calculator. The type that only I would write. Source code is concise ( 70 lines or so), and is available upon request, in either Java or C++. The version here is a .exe available (Ownage.exe) , compiled in C++. No idea whether it works on mac.

http://www.mediafire.com/?sharekey=0ec2 ... b9a8902bda

This calculator is , unsurprisingly from the introduction, one of a kind. It calculates exactly (to several decimal places) the expected number of hits required to kill a monster with a certain setup. The parameters are very general (Accuracy (%), Max Hit (#), and Monster HP(#)), so you will need to know your approximate accuracy against the monster that you plan on fighting, but other than that, it's very powerful.

Algorithm justification (Why I even needed to write a calculator)

1) The naive solution: The naive solution to this problem might be to assume that you "average" m*a/2 per hit, and therefore should take about (HP/(ma/2)) hits to kill the monster. For starters, this is wrong, and secondly, it's REALLY wrong.

Example 1) 70% accuracy, max hit of 40. Monster has 5 hp.
Naive solution: 5 / (40*.7/2) = 5/14 hits. This should take you less than 1 hit to kill the monster. Clearly wrong.
Lord Klotski's calculator: 1.57688. This is far more reasonable; you might miss once first, you might miss twice, you might hit a 1 or a 2, etc.

Well, perhaps the naive solution is a good approximation to larger values?

Example 2) 90% accuracy, max hit of 40. Monster has 125 hp.
Naive solution: 125/(40/2*.9) = 125/18 = 6.94444
Lord Klotski's Calculator: 7.47933

So our naive solution is off by .5, or about 9%. Nawt gud.

Why does the naive solution not work? Because, to be brief, it handles fast KOs very badly. I could post a full worked example, if needed.



2) Simulated statistical samples: The idea of doing several statistical samples came to mind when I designed this. The computer could generate a series of "hits" that would simulate killing the monster over and over, then I could take the mean of that and hope it was accurate.

The issue with this setup is that it is REALLY inaccurate for HP > 2-3 times the max hit (and gets worse if you want to do a GWD monster lol, or the CB), and requires a TON of iterations to get anywhere reasonably close to the "true mean". I happen to know that the variation of a single hit is random in both m^2 and a^2, so the variance is ugly high. I'd need to be running, for my higher HP values, hundreds of thousands of kills, each taking several hits...nawt a good idea. Furthermore, I'm not guaranteed that it's accurate.



3) Naive Recursion: This is the first of the two truly accurate ways to compute the expected value. Basically, we say that for a general hit, we will take off 0-max HP. Then , we define the expected number of hits to be 1+E(current HP - damage). In other words, the function refers to itself.

A quick example of recursion: The fibonacci series is defined recursively, where f(n) = f(n-1) + f(n-2). Try: f(3) = f(2) + f(1). We don't know f(2), so f(2) = f(1) + f(0). f(1) and f(0) are defined to be 1. Then we have f(3) = 2 + f(1). f(1) is defined to be 1, so f(3) = 2+1 = 3.

Now, if we try to compute f(5), we need f(4) and f(3). f(4) = f(3) + f(2). We need both of those: f(3) = f(2) + f(1). f(2) = f(1) + f(0), then we go back, calculating another branch of f(2), and then f(3) again. Finally we add these together to get f(5).

This method would have you, say for a max hit of 40, computing 40 different things each step/HP point (in addition to calling itself at it's own value if you miss). In other words, for a max hit of 40, and a HP of 120, you would require about 40^120 iterations. On a very good scientific calculator, you can find this equal to about 10^192 iterations. This program would not run in your lifetime. Sorry.



4) Recursion with Dynamic Programming (DP): - This is the algorithm that I used. Basically, I used some hax methods to not do the 40^120 iterations, and instead, should run so fast that you won't even notice that there's lag (unless you use the CB HP, which is 2000; it'll take 1-2 seconds or so). It has a couple creative ways of dealing with some of the fundamental issues with the recursive algorithm, such as the method does call itself with the same parameters (a well defined infinite loop, but you don't need to worry too much about that. I was also pretty tricky in a couple other places in the program, but see my previous statement.

The source code for the DP method is, as I said, available upon request. It took me four hours to write. Debugging recursion sucks.



How to use this calculator

Plug in a value for accuracy, expressed as a %. Plug in your max hit. Plug in the HP of the monster you want to fight. You will get an expected # of hits until death of the monster. Nice and easy. This has many uses - you can calculate Xp/hr, you can calculate "truncation loss in XP/HR" by fighting lower level monsters with low HP (remember that you lose XP from KO hits? Well, take the difference of the first naive solution and this method and you have it). You can (and this is REALLY why I wrote it) compare two different weapons AGAINST THE SAME MONSTER by inputting values for expected accuracy, max hit, an HP. Ferarri and I were doing the GS vs Whip debate; I claimed that the monster that we were killing mattered. Guess we'll see whether I'm right.


There ya go, ferarri. Now all we need are some numbers for accuracy (and you don't want mine, they are mostly at waterfiends :twisted: )
Last edited by Lord Klotski on Tue Nov 25, 2008 3:03 pm, edited 1 time in total.
Image
With max hit 'm', accuracy 'a', and monster HP 'x', this gives the expected number of hits to kill the monster :)

Submit combat data to Lord Klotski's Calculator V 1.337 ! viewtopic.php?f=7&t=5744&p=133065#p133065
User avatar
Lord Klotski
 
Posts: 1291
Joined: Wed Apr 16, 2008 7:12 pm

Lord Klotski's Calculator

Postby Lady Lisa » Tue Oct 28, 2008 1:43 am

Waaaaaaaaaaay too efficient for me! :P
Image
Image
Semi-official TS Home World 158 Log on hope to see you soon!
Need an LOL? Check out http://www.youtube.com/user/LadyLisaTabs
Lady Lisa
 
Posts: 1809
Joined: Fri Nov 30, 2007 6:47 pm
Location: Avondale Arizona RSN: Lady Lisa Email me: LadyLisaTabs@gmail.com

Lord Klotski's Calculator

Postby aximili e i » Tue Oct 28, 2008 1:45 am

Lady Lisa wrote:Waaaaaaaaaaay too efficient for me! :P

thats why your not a member of the efu lol
"Given [Pixar's] track record, they could announce their next film is about a lump of dirt, and I would assume that it would be a.) Oscar worthy and b.) a $200 million hit. They really can do no wrong."

Come and try to find me in the Where Am I HD? or Where Am I Classic? threads, I dare you.
HEY! Check out my RSC exploits in The Chronicles of a Classic Aximili E I.
Image
Image
User avatar
aximili e i
 
Posts: 2224
Joined: Sun Feb 10, 2008 1:20 am
Location: Usha, Glandar Cluster, Azoroth Territories, Wizard Unity

Lord Klotski's Calculator

Postby Lord Klotski » Tue Oct 28, 2008 12:06 pm

aximili e i wrote:
Lady Lisa wrote:Waaaaaaaaaaay too efficient for me! :P

thats why your not a member of the efu lol


I'll bet that Lady Lisa (along with the NTEG) prefer to do recursion by hand :twisted:

(Nobody will get my joke. Oh well)
Image
With max hit 'm', accuracy 'a', and monster HP 'x', this gives the expected number of hits to kill the monster :)

Submit combat data to Lord Klotski's Calculator V 1.337 ! viewtopic.php?f=7&t=5744&p=133065#p133065
User avatar
Lord Klotski
 
Posts: 1291
Joined: Wed Apr 16, 2008 7:12 pm

Lord Klotski's Calculator

Postby Rakshar » Tue Oct 28, 2008 12:26 pm

Lord Klotski wrote:
aximili e i wrote:
Lady Lisa wrote:Waaaaaaaaaaay too efficient for me! :P

thats why your not a member of the efu lol


I'll bet that Lady Lisa (along with the NTEG) prefer to do recursion by hand :twisted:

(Nobody will get my joke. Oh well)


I hire people to do my recursions. :twisted:


edit... isn't it illeagal to do a recursion in public? :P
"We make a living by what we get,
but we make a life by what we give."

Image
User avatar
Rakshar
 
Posts: 2001
Joined: Wed Jan 02, 2008 2:53 pm

Lord Klotski's Calculator

Postby aximili e i » Tue Oct 28, 2008 12:31 pm

Rakshar wrote:
Lord Klotski wrote:
I'll bet that Lady Lisa (along with the NTEG) prefer to do recursion by hand :twisted:

(Nobody will get my joke. Oh well)


I hire people to do my recursions. :twisted:

in that case i hire you rakshar to do it for me(then you'll hire someone else to do it for you / me then that person will hire someone else and so on..... YES the glorious NTEG revolution is at hand!(or has D.C. already done this before :twisted: )
"Given [Pixar's] track record, they could announce their next film is about a lump of dirt, and I would assume that it would be a.) Oscar worthy and b.) a $200 million hit. They really can do no wrong."

Come and try to find me in the Where Am I HD? or Where Am I Classic? threads, I dare you.
HEY! Check out my RSC exploits in The Chronicles of a Classic Aximili E I.
Image
Image
User avatar
aximili e i
 
Posts: 2224
Joined: Sun Feb 10, 2008 1:20 am
Location: Usha, Glandar Cluster, Azoroth Territories, Wizard Unity

Lord Klotski's Calculator

Postby niperwiper » Tue Oct 28, 2008 3:59 pm

has there ever been a calculator made to determine how long prayer lasts at different bonuses and levels? I'd be very interested in something like that to predetermine how many p pots to bring to different situations. Unfortunately I could only write script that runs on command prompt, don't know how to work with GUI's and stuff. Not to mention the algorithm behind prayer is unknown to me as well :P

I don't think I'd have much use for this calculator since I don't really want to determine my accuracy percentage :o
Image
Image
A.K.A. - Rilo Carlile

Outfits are important! | Witness 10 99's at once! August 7, 2010
(Standings last updated: 6/26/2010)
User avatar
niperwiper
 
Posts: 1649
Joined: Fri Mar 21, 2008 3:11 am
Location: Athens, GA

Lord Klotski's Calculator

Postby Blooblyblobl » Tue Oct 28, 2008 4:55 pm

I'd be interested in the source code (in java, or both :) ) - the basic idea makes perfect sense but I'll be interested to see how you made it efficient.

And yes, debugging recursion is a royal pain in the ass :P
Image
Image
Blooblyblobl
 
Posts: 557
Joined: Tue Jan 22, 2008 12:19 pm

Lord Klotski's Calculator

Postby Rakshar » Tue Oct 28, 2008 4:59 pm

My company's firewall blocks this link so I have to wait until I get home to look at it. :(
"We make a living by what we get,
but we make a life by what we give."

Image
User avatar
Rakshar
 
Posts: 2001
Joined: Wed Jan 02, 2008 2:53 pm

Lord Klotski's Calculator

Postby Lord Klotski » Tue Oct 28, 2008 6:41 pm

@Niper: The calculator exists, it's on runehq. "Prayer drain rate calculator", under the special calculators section. I would have made my own if it didn't already exist.

@Blobblyblobl: All right; I'll post java code in a sec. Gotta put in some comments, and make the code styling more legible (I program with variables such as "boolean hax, boolean hax2, int lolol...").
Image
With max hit 'm', accuracy 'a', and monster HP 'x', this gives the expected number of hits to kill the monster :)

Submit combat data to Lord Klotski's Calculator V 1.337 ! viewtopic.php?f=7&t=5744&p=133065#p133065
User avatar
Lord Klotski
 
Posts: 1291
Joined: Wed Apr 16, 2008 7:12 pm

Lord Klotski's Calculator

Postby Boa1891 » Tue Oct 28, 2008 7:40 pm

^ I lol'd.
Pantalaimone wrote:I think it is safe to say that Boa makes people quit. :lol: Boa is the anti joy of PKing :P

Image
User avatar
Boa1891
 
Posts: 4148
Joined: Sat Jul 26, 2008 3:40 pm
Location: I see a little sillhouette of a can... MOUNTAIN DEW! MOUNTAIN DEW!

Lord Klotski's Calculator

Postby Lord Klotski » Tue Oct 28, 2008 7:55 pm

Here goes all the formatting....

Code: Select all
package acmproject;
import java.io.* ;
import java.util.* ;
import java.lang.* ;
import java.math.* ;

/******************************************************
*                   Lord Klotski                             *
*                                                                  *
*                                                                  *
******************************************************/
public class DPFtw {
    static double accuracy ;
    static int maxHit ;
    static double[] Expected ;
    static int[] Count ;
    // Declaration of important variables. Yes, I know that they're global variables,
    // and technically that's bad. However, for DP, global arrays are needed,
    // so I figured that I might as well go the whole nine yards and make everything
    // global.
   
   
   
    static double LordKlotskiIsGreat(int q)
    {
        // Note that the single argument (parameter) that this function takes
        // is q - the current health of the monster. maxHit and accuracy are global :D
       
        double sum = 0 ;
        int loopbounds = maxHit ;
       
        if (Expected[q] != -1)
        {
            return Expected[q] ;
        }
        // This is really the part of the code that makes it all work.
       
        // Basically what makes this program use a DP solution instead of straight
        // recursion is that I store the previously computed values. This changes
        // the order of run time by an extremely significant margin, because
        // instead of each stage run in time O(4^stagelevel), each stage runs in
        // essentially constant time. By storing the expected values from each of
        // the previous stages, I am able to simply grab them and move on.
       
        if (Count[q] == 100)
        {
            return 1 ;
        }
        // WTF is this? Recall that the damage function can output a zero. If I
        // call my own function with the *same parameters*, then even DP won't
        // bail me out of a self inflicted infinite loop. So I start being tricky:
        // I assume that the player will not hit more than 100 zeroes in a row.
        // This counter counts how many times I've "missed" in a row.
       
       
        if (q <= maxHit)
        {
            sum += accuracy * (maxHit - q + 1) / (maxHit) ;
            loopbounds = q - 1 ;
        }
       
        // If the max hit is greater than the current health of the monster,
        // then it's possible to KO it. The odds of a KO are identically
        // the RHS of the first equation. If I don't KO it, then I must have
        // hit either a zero or a number lower than the current health of the
        // monster that I'm fighting...both of which are general recursion cases...
       
       
        for (int i = loopbounds ; i > 0 ; i --)
        {
            sum += accuracy / (maxHit) *(1+ LordKlotskiIsGreat(q - i)) ;
        }
       
        // In addition to telling myself how amazing that I am, this line of code
        // is extremely powerful. Basically, it loops from the maximum damage downwards
        // and recursively calls the function (for a current health equal to the
        // argument q - damage). 0 damage is not done in this loop;
        // KO's are not either. This is, in short, the extremely general case of damage.
       
       
        Count[q] ++ ;
        sum += ((1 - accuracy))*(LordKlotskiIsGreat(q)+1) ;
       
        // This is for the case of a "miss", with probability 1 - accuracy.
        // This is the reason why I have to be tricky with the "Count array",
        // because I'm calling a recursive function without changing the parameter...
       
       
        return sum;
       
        // The final expectation.
    }
   
   
   
   
   
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in) ;
       
        System.out.println("Accuracy (0 < a < 1) :") ;
        accuracy = input.nextDouble() ;
        System.out.println("Max Hit:(0 < m) :") ;
        maxHit = input.nextInt() ;
        System.out.println("HP: (0 < HP) :") ;
        int HP = input.nextInt() ;
       
        // Input of the variables. Straightforward.
       
       
        Expected = new double[HP+1] ;
        Count = new int[HP+1] ;
       
        for (int i = 0 ; i < HP+1 ; i ++)
        {
            Expected[i] = -1 ;
            Count[i] = 0 ;
        }
        Expected[0] = 0 ;
       
        // Expected[0] should never be called...but just in case...
       
       
        for (int i = 1 ; i < HP+1 ; i ++)
        {
            Expected[i] = LordKlotskiIsGreat(i) ;
        }
        // The other *really* tricky thing that I did. Normally when doing a DP
        // solution, one could just call the function and let it take care of
        // itself. Here, each iteration gives 40 recursive calls, so with a
        // stack size of around 7000, I'll run out of room using conventional DP.
        // However, what I do here is define the array as I go - specifically,
        // I first calculate Expected[1], then Expected[2]...etc. This means that,
        // for a general recursive call to a lower HP than the current, instead
        // of *that* iteration spawning, it should just take the already computed
        // value and return instantly; I'm therefore guaranteed to not have more
        // than 100 stack calls at once, which is perfectly valid.
       
       
        System.out.println(Expected[HP]) ;
        // Occasionally, printing out the answer helps.
       
    }
}
Last edited by Lord Klotski on Tue Nov 25, 2008 3:04 pm, edited 2 times in total.
Image
With max hit 'm', accuracy 'a', and monster HP 'x', this gives the expected number of hits to kill the monster :)

Submit combat data to Lord Klotski's Calculator V 1.337 ! viewtopic.php?f=7&t=5744&p=133065#p133065
User avatar
Lord Klotski
 
Posts: 1291
Joined: Wed Apr 16, 2008 7:12 pm

Lord Klotski's Calculator

Postby Lady Lisa » Tue Oct 28, 2008 8:29 pm

[pokes fun]
Sound the alarms!! Error within the code!!

// so I figured that I might as well go the whole ine yards and make everything
// global.


Yes I am a geek and read the entire thing line by line. And no I am not surprised that you code how great that you are...
I do believe that should be nine. :P

[/pokes fun]
Image
Image
Semi-official TS Home World 158 Log on hope to see you soon!
Need an LOL? Check out http://www.youtube.com/user/LadyLisaTabs
Lady Lisa
 
Posts: 1809
Joined: Fri Nov 30, 2007 6:47 pm
Location: Avondale Arizona RSN: Lady Lisa Email me: LadyLisaTabs@gmail.com

Lord Klotski's Calculator

Postby Kittguin » Tue Oct 28, 2008 8:38 pm

Boa1891 wrote:^ I lol'd.

+ over 9000
User avatar
Kittguin
 
Posts: 3067
Joined: Mon Mar 03, 2008 10:53 pm

Lord Klotski's Calculator

Postby Rue Night » Tue Oct 28, 2008 9:48 pm

You coded all those notes into your program? :shock:
What a waste of file space...
And youu call yourself an EFU member... :mrgreen:
(I am surprised how well I can still follow code, even though I haven't written a program in about a year)
ImageImageImageFormerly Metroidfan10.
Sir Valimont wrote:Thieving was always my favorite skill, and my favorite thing to hate was just how stupid the Thieving skillcape emote was ... And Jagex has managed to "fix" it into ... still being the dumbest thing imaginable.
User avatar
Rue Night
 
Posts: 2386
Joined: Thu Jan 31, 2008 2:38 pm
Location: Apparently "Tree City, USA"

Lord Klotski's Calculator

Postby Lord Klotski » Tue Oct 28, 2008 10:06 pm

FFS! Wrote the comments so that I could post on this site...I should post the C++ then you'd really see what my code is like LOL.
Image
With max hit 'm', accuracy 'a', and monster HP 'x', this gives the expected number of hits to kill the monster :)

Submit combat data to Lord Klotski's Calculator V 1.337 ! viewtopic.php?f=7&t=5744&p=133065#p133065
User avatar
Lord Klotski
 
Posts: 1291
Joined: Wed Apr 16, 2008 7:12 pm

Lord Klotski's Calculator

Postby Lady Lisa » Tue Oct 28, 2008 10:43 pm

Lord Klotski wrote:FFS! Wrote the comments so that I could post on this site...I should post the C++ then you'd really see what my code is like LOL.



Ah but you still love me lol! :twisted:
Image
Image
Semi-official TS Home World 158 Log on hope to see you soon!
Need an LOL? Check out http://www.youtube.com/user/LadyLisaTabs
Lady Lisa
 
Posts: 1809
Joined: Fri Nov 30, 2007 6:47 pm
Location: Avondale Arizona RSN: Lady Lisa Email me: LadyLisaTabs@gmail.com

Lord Klotski's Calculator

Postby Lord Klotski » Tue Oct 28, 2008 10:44 pm

All righty. Few things to talk about:

1) Lady Lisa , no idea what you are talking about.
Lady Lisa wrote:[pokes fun]
Sound the alarms!! Error within the code!!

// so I figured that I might as well go the whole ine yards and make everything
// global.


Yes I am a geek and read the entire thing line by line. And no I am not surprised that you code how great that you are...
I do believe that should be nine. :P

[/pokes fun]


Look carefully, I think you have made an error when quoting me.

2) C++ code. No comments. This should satisfy Metroid.

#include <cstdlib>
#include <iostream>


using namespace std;

double Expected[2001] ;
int Count[2001] ;
double a ;
int m ;
int h ;



double LordKlotskiIsGreat(int q )
{
double sum = 0 ;
int loopbounds = m ;

if (Expected[q] != -1)
{
return Expected[q] ;
}
if (Count[q] == 100)
{
return 1 ;
}

if (q <= m)
{
sum += a * (m - q + 1) / (m) ;
loopbounds = q - 1 ;
}

for (int i = loopbounds ; i > 0 ; i --)
{
sum += a / (m) *(1+ LordKlotskiIsGreat(q - i)) ;
}
Count[q] ++ ;
sum += ((1 - a))*(LordKlotskiIsGreat(q)+1) ;

return sum;
}


int main(int argc, char *argv[])
{


cout << "Accuracy:" << endl ;
cin >> a ;
cout << "Max Hit:" << endl ;
cin >> m ;
cout << "HP of Monster:" << endl ;
cin >> h ;




for (int i = 0 ; i < h + 1 ; i ++)
{
Expected[i] = -1 ;
Count[i] = 0 ;
}
Expected[0] = 0 ;

for (int i = 0 ; i < h + 1 ; i ++)
{
Expected[i] = LordKlotskiIsGreat(i) ;
}

cout << "The expected number of hits to kill the monster is:" << endl ;
cout << Expected[h] << endl ;






system("PAUSE");
return EXIT_SUCCESS;
}
Image
With max hit 'm', accuracy 'a', and monster HP 'x', this gives the expected number of hits to kill the monster :)

Submit combat data to Lord Klotski's Calculator V 1.337 ! viewtopic.php?f=7&t=5744&p=133065#p133065
User avatar
Lord Klotski
 
Posts: 1291
Joined: Wed Apr 16, 2008 7:12 pm

Lord Klotski's Calculator

Postby Rue Night » Tue Oct 28, 2008 11:13 pm

lol, much better :P
ImageImageImageFormerly Metroidfan10.
Sir Valimont wrote:Thieving was always my favorite skill, and my favorite thing to hate was just how stupid the Thieving skillcape emote was ... And Jagex has managed to "fix" it into ... still being the dumbest thing imaginable.
User avatar
Rue Night
 
Posts: 2386
Joined: Thu Jan 31, 2008 2:38 pm
Location: Apparently "Tree City, USA"

Lord Klotski's Calculator

Postby Lady Lisa » Wed Oct 29, 2008 1:35 am

Yawn......
Any coder can be well boring....
You are much better than this. :P
Image
Image
Semi-official TS Home World 158 Log on hope to see you soon!
Need an LOL? Check out http://www.youtube.com/user/LadyLisaTabs
Lady Lisa
 
Posts: 1809
Joined: Fri Nov 30, 2007 6:47 pm
Location: Avondale Arizona RSN: Lady Lisa Email me: LadyLisaTabs@gmail.com

Next

Return to Off-Topic

Who is online

Users browsing this forum: No registered users and 2 guests