Algorithm to show how far away a value is from another - java

Disclaimer: This is a very very difficult question about mathematics and algorithms (in my opinion) - so respect to anyone who makes this. I admire you.
I would like to evaluate the performance of my employees. I would like to do this by measuring the following parameters as percentages of the amount of time they spend working:
1. % of Time spent working
2. % of Time spent in meetings
3. % of Time spent travelling
I have a "preferred" set of percentages for each of these. This represents the ideal time that I would LIKE my employees to spend their time.
% Time spent in Meetings -> 30%
% Time spent Travelling -> 10%
% Time Spent Working -> 60%
total time spent on activities: 100%
So in words, I would like my employees to spend 30% of their time in meetings, 10% time travelling and 60% on a desk working.
I would ALSO like to add weights to these different percentages. The weight would represent how "lenient" I am with each percentage being different to what I desire. In other words, how important I find it for each variable to be closest to the desired percentages (30, 10, 60). I would like to apply the weights on a scale of.1 to 10, 10 being most important, 1 being least important.
Meetings -> 3
Travelling -> 9
Working -> 5
So given the percentage of time spent by an employee, and the weight of the importance of the time spent being close to the desired time spent, I would like to generate an index between 0 and 100 where 100 is the perfect time percentages and 0 is the worst. So a score of 100 would give the "preferred" percentages:
% Time spent in Meetings -> 30%
% Time spent Travelling -> 10%
% Time Spent Working -> 60%
How I would try to approach this:
Find out what the minimum and maximum ratios are
Calculate a value to tell how far away each value is from the desired value using the minimum and maximum ratios. Make sure this value is in the ratio 0-1 (corresponding to 0-100)
Calculate a weighted average.

I think you're trying something which is kinda like the H-score.
H-score is a scored we use in digital pathology to measure tumor positivity for a maker and it is meant to weight the number of positive cells by their intensity.
It's:
1* (% of positive cells with score 1) + 2*(% of positive cells with score 2) + 3*(% of positive cells with score 3)
You could calculate the same as:
3*(1-% of difference between actual worked hourse and planned ones)
9*(1-% of difference between actual worked hourse and planned ones)
5*(1-% of difference between actual worked hourse and planned ones)
Don't forget to use absolute value when you compute the difference.
I used 1-% so that a difference of 0.8 (as planned 0.ì and worked 0.9) will result in a score of 0.2. This will work for the opposite situation too (planned 0.9 and worked 0.1).
In this way, who perfectly matches the planned hourse will have a score of 1700%.
Then just:
Score/Total weights = score between 0-100%.

Related

Find all timings with given angle of clock minute and hour hands

Given absolute value of angle between hour and minute hands.
We need to find all valid values of time (in hours and minutes, both non-negative integers) from midnight (inclusive) to noon (not inclusive) which satisfy the given angle.
It is to be noticed that time value is considered valid if the angle between the clock's hands for that value and the angle given has described differ by less than 1/120 degrees.
Also, the movement of the minute hand influences the hour hand. That is, every minute, it moves by 1/60th of the angular distance between two consecutive hour marks.
So how to solve this problem in good way. I wrote down a code with hell lot of if and else. So just wondering if there is clean way of finding all of them.
Example : If A=30 then it can be :
01:00
11:00
I'll try an idea in pseudocode
for minuteHand in [0,59]:
minuteAngle=angle(minuteHand) // gives the angle respect to the 0
hourAngle1 = minuteAngle+A //Should be modified to account for the precision
hourAngle2 = minuteAngle-A
if isValidTime(minuteAngle,hourAngle1): // check if the two hands express a valid time at this angle
found a valid time
if isValidTime(minuteAngle,hourAngle2):
found a valid time
Problem of currently going on competition in Codechef.
https://www.codechef.com/OCT15/problems/TIMEASR
I do not really understand what is your problem. "How to solve this?" is a bit to vague to give a short answer. However, the first thing I would do is to convert all values to the same units. 30 degrees corresponds to 1/12th of the full circle, ie in one minute one hand moves by 6 degrees (1/60 of a full turn) and the other one by 1/2 degree (30 degrees in a full hour). So you are basically trying to solve
abs( (t * 6) - (t*1/2) ) == 30 + n*360
where n = 0,1,2,.... That modulo part makes it a bit tedious to get a direct solution, but you can simply run a loop for any values of t to find the solutions. Hope this helps. If not, maybe you should consider showing some of your code....
This question shouldn't be answered right now as it's from a LOng contest being hosted by codechef. So , this is spoiling the problem for enthusiasts like us who wants to solve this on our own.

Obtain primes with threads. How to divide intervals?

I want to obtain all the prime numbers in the interval [min, max]. I want to do the calculus in n servers. Which is the best way to divide my initial interval in another n intervals, to manage approximately the same load in all the n servers?
[Optional]
I had an idea, but it didnt work as I expected. I assumed that all numbers are primes, so that a number i costs i instructions to verify is a prime.
If we keep in mind this method:
Then, the number of instructions to get primes in interval [1,100] is 1+2+..+99+100 = 100(1+100)/2 = 5050.
Now, if I want to do this calculus in 2 servers (n=2), I have to divide this load to each one (2525 instructions each one). The interval I want is defined by 2525 = x(1+x)/2 -> x=71.
In general terms, the general formula would be Load = (Interval(x) - Interval(x-1) + 1) * (Interval(x-1) + Interval(x)) / 2, being Load = (max - min + 1) * (min + max) / (2 * n).
Doing this, with x and y = [1:9999999] and n = 16, I have got this results:
(source: subirimagenes.com)
I dont get the same time and instructions in all servers, what means this is not the way to divide the intervals.
Any idea?
I think you looking for a parallel approach.
This is what the work stealing algorithm was designed for, aka Fork Join Pool. In fact, prime number calculation is a classic use case for work stealing because telling whether n is prime requires iterating till sqrt(n) so the bigger is n the longer it takes. So distributing them among your workers evenly and waiting for every worker to finish its job is unfair, the first core will quickly determine whether n is prime or not and sit idle and the other core will stay busy calculating bigger numbers. With work stealing the idle processor will steal work from its neighbours queues.
This implementation might be useful.
I solved this problem. I didn't do a first complete division of my interval and assign each part to a different server. I made the decision to divide the interval into very small parts ([min, max]/length^2 for example), and each calculus server got one of these parts. When they finish, they get another one until there is no more small intervals to calculate.
Why did I do this? The reason it's because I can't ensure that the servers I'm working with have the same calculus speed.

Making the range of perlin noise smaller

I have a perlin noise function, and I want to use it to pick biomes for a map for my game. The problem is that biomes are determined by two factors - average precipitation and average temperature. So, I thought, I'd just make two perlin noise functions and overlap them.
The issue now is that biomes do not encompass all possible precipitation temperature combinations. For example, there is no biome with high precipitation and low temperature, as shown in this picture.
(source: wikimedia.org)
How can I still use perlin noise but never reach the areas that aren't covered by biomes?
You can clamp the values into the allowed range (eg. maximum allowed precipitation in an area with temperature 0 °C is 100 cm).
You can do this during the noise algorithm itself, not only after the entire value field is finished. I would imagine it would work like this:
First generate the temperature map.
When you are generating each random value for the rainfall noise, generate a value in range scaled down appropriately to the range of values allowed by the temperature map.
Example:
If you would generate random value in range 0 - 250 mm (about 50% of maximum possible rainfall, for one of the low frequency noise layers), look at the temperature in that pixel, see it's 10 °C, so the range of the random value will be scaled down to 0-100mm (50% of the 0-200 mm allowed by that temperature).
Therefore, even if you roll maximum random value for each of the layers, the composite value will be restricted by the maximum dictated by the temperature.
I don't know how realistic this is and how important realism is for you. What precisely prevents low temperature areas from having great rainfall? The solution I proposed might actually simulate factors like reduced evaporation in low temperature areas quite well.
EDIT:
One more idea, which might end up being equivalent to my first solution:
Generate both the temperature map and rainfall map independently.
Multiply the rainfall map by the temperature map (scaled to range <0, 1>). This will reduce rainfall in areas with low temperature.

Finding mean with least number of iterations

I have a list of measurements with the following properties:
The measurements are expensive. Fewer measurements -> better
They are all positive. In fact, there is a positive lower limit and I can't get any values below that. This lower limit is what I need to know with some confidence.
They will distribute around one or more median values
I know that there is another "better" median when I find an outlier which is smaller than median - 2*variance because the distance between the "best" median and the lower limit is always smaller than two times the width of the normal distribution
Goal: Find the best median with the least amount of iterations with a confidence of, say, 90%.
I'd prefer the smallest value but the smallest median is good enough.
What I'm looking for is a piece of code where I feed the measurements and which tells me the median and how confident it is that this median is the one I seek.
Background: I want to time Java methods. I could run the test for a couple of minutes to average outliers out but when looking at the data, it's pretty obvious for a human that the values quickly accumulate around the median value.
Unless the JIT kicks in and the median suddenly jumps. Eventually, you will end up with a curve that is very steep left of the smallest median (i.e. the variance on the left side of the median is low) and a long, soft slope on the right side with a bump where the pre-JIT median was.
Sample test data (13KB)
testConnect-count.csv is a histogram of the values, testConnect-history.csv is the sequence of measurements. The goal is find an algorithm which returns the smaller median around 115000 by reading the smallest number of values from testConnect-history.csv
I believe it common practice to look at percentiles for latency as they don't follow a normal distribution and its the longer latencies which will hurt you.
In your case you could use the 50th percentile and 90th percentile.
These are simple to calculate if you have a sorted collection
List<Long> times = ....
Collections.sort(times);
long median = times.get(times.size()/2);
long ninetyth = times.get(times.size()*9/10);
I use trove as this can be more efficient for timing sensitive tests. It uses primitive values instead of creating objects.
TLongArrayList times =
times.sort();
long median = times.get(times.size()/2);
long ninetyth = times.get(times.size()*9/10);
long ninetynineth = times.get(times.size()*99/100);
In your case the median is 116000 and the 90%tile is 170000. The 99%tile is 255000
I believe the actual question is: give me the average execution of a subprogram after it has been JIT-optimized.
The process is not stationary. The time for the JIT to kick in depends on the actual virtual machine implementation and the program under test. I believe You won't find a general-purpose-magic-bullet method.
You must experiment. I`d try throwing away a fixed number of measurements, make a fixed number of measurements, throw away outliers, take average of the rest.

calculate average based on 1 number only

How to calculate percentage ( or average) when You have dividend but not deviser?
You have a lot of values, and some of them figure into your average - or percentage - and some of them probably don't. You are not expressing the problem clearly enough for anyone to be able to give you a meaningful answer.
A percentage represents a fraction, one value divided by another (multiplied by 100 to express it in percentage, but that's trivial and not part of the problem). What is the value that represents 100%? And what value are you trying to assign? In what way do you think that the quantity of bonuses should affect the percentage?
Some possible answers:
The total bonus earned by an individual, as compared to her nominal salary. If she earns $50k and her bonus is $20K, that is 20/50 *100 = 40%.
The total bonus earned by an individual, as compared to all the bonuses given out that year. If she received the same $20K, but the company gave out $100K in bonuses, then the percentage is 20/100 * 100 = 20%.
The most recent bonus earned by an individual, as compared to all bonuses awarded to her this year. If she got $5K for her last bonus, and the total was $20, that's 5/20 * 100 = 25%.
We really don't have enough information to go on; it could be any of these, or something entirely different. It is entirely possible to have a percentage value greater than 100%.
The average of one value is that value (Total number=1).
But this probably means I don't understand your question.
Without knowing the number of years, you need to know something else about the range of bonuses possible. i.e. does it have to be a whole number between 15 - 25%. However, this is largely guessing.
To get an average, you need a total and a count. BTW: In your case you want the geometric average, but you need to know the same things.
If your input is a list of numbers, showing percentage values means you need to compute the total and then see how much of the total each of them is:
For instance, if you have 110, 110, 110, you'll have a total of 330 and each of the values will be shown as 110/330 = 0.33 = 33% of the total.
In addition, if I have three decimal
values 120, 4420, and 230. How can I
get a number less than 1 that
represent the average of these 3
values?
You cannot. The average of those 3 numbers will be (120 + 4420 + 230) / 3. That will never be less than one. Maybe you are confused about what average means?
You need to be more specific or give an example. But I will give an answer based off of what I THINK you mean.
You cannot find the average of one lone number. If you were saying a temperature of 125 degrees every hour you could do it, The answer would obviously be 125. It is the closest thing that I can think of to what you are asking. You need to be more specific or the problem cannot be done. Otherwise use the simple formula: Sum/Number of integers. Also known as the mean. So that would be 125/1, which is still 125.

Categories

Resources