How do you find the smallest difference in mapping elements of a Primary List to two Secondary Lists - algorithm

I have 10 players on a team.
My team of players need to purchase a total of 10 bats and 10 balls.
They can purchase:
A bat and a ball
Two bats
Two balls
They cannot buy 3 bats, or 3 balls, or any other combination. Two items only.
The Seller has 10 different balls, and 10 different bats, all with different prices.
Once 1 bat is sold, then that would be removed from the list.
The Buyer can go into debt.
The Seller does not have any change (even after a purchase).
If the Buyer spends 100 on a ball worth 10, he does NOT get 90 back.
I do have access to how much money each player has, as well as the value of how much the Seller will sell each bat and ball for.
Buyer - Name, Ball Purchase Price, Bat Purchase Price
Alpha 10 15
Bravo 20 20
Charlie 30 30
Delta 40 40
Echo 50 50
Foxtrot 60 60
Golf 70 70
Hotel 80 80
India 90 95
Juliett 99 99
Seller - Ball Name, Ball Price
A 10
B 20
C 30
D 40
E 50
F 60
G 70
H 80
I 90
J 99
Seller - Bat Name, Bat Price
A 99
B 95
C 80
D 70
E 60
F 50
G 40
H 30
I 20
J 15
In this example Alpha should purchase Ball-A and Bat-J, and Juliett should purchase Ball-J and Bat-A.
I am trying find an optimized way of figuring out which player should be buying which items to save the most money as a team, or for the team to be the least in debt.
How do you find the smallest difference in mapping elements of a Primary List to two Secondary Lists?
In a more complex scenario, how do I find out which Buyer should purchase which items?
In other examples, the seller might have a very expensive store where the players might have to go into debt.
Searching this type of question was difficult, as I am trying to find the smallest differences between my primary list, and two secondary lists, where the primary list can compare the same element twice.

Related

Add multiple custom row measures in PowerBI

I have a PowerBi matrix and I'm trying to 3 some custom rows at the end of each group but can't figure out how to do so. Below is what the matrix looks like.
Salesperson
Total Units Sold
John
Apples
10
Oranges
5
Spoilage
2
Katie
Mangoes
12
Apples
9
Pears
15
Spoilage
1
And I'm trying to get a Total, Net and Percentage into the matrix as shown below. Total Fruits is a summation of all the rows above except the spoilage row. Net is the summation of all above including the Spoilage and Percentage (Pct) is Spoilage divided by Total Fruits.
Salesperson
Total Units Sold
John
Apples
10
Oranges
5
Total Fruits
15
Spoilage
2
Net
13
Pct
13.3%
Katie
Mangoes
12
Apples
9
Pears
15
Total Fruits
36
Spoilage
1
Net
35
Pct
2.9%
I have a fact table that records each fruit sold by the product code and the salesperson id and dimension tables for the salesperson and the products.
I'm new to PowerBI and so I would appreciate all the details to make this work.

How to devide some fixed amount of reward points to players of a racing game in a fair way depending on their finishing time

I'm in need of some kind of algorithm I can't figure out on my own sadly.
My biggest problem is that I have no good way to describe the problem... :/
I will try like this:
Imagine you have a racing game where everyone can try to be the fastest on a track or map. Every Map is worth 100 Points in total. If someone finished a map in some amount of time he gets a record in a database. If the player is the first and only player to finish this map he earns all the 100 points of this map.
Now, that's easy ;) but...
Now another player finishes the map. Let's imagine the first player finishes in 50 Seconds and the 2nd player finishes in 55 seconds, so a bit slower. I now need a calculation depending on both records in the database. Each of both players now earn a part of the 100 points. The faster player a bit more then the slower player. Let's say they finished the exact same time they both would get 50 points from 100, but as the first one is slightly faster, he now earns something around 53 of the points and the slower player just 47.
I started to calculate this like this:
Sum of both records is 105 seconds, the faster player took 50/105 in percent of this, so he earns 100-(50/105*100) points and the slower player 100-(55/105*100) points. The key to this is, that all points distributed among the players always equals to 100 in total. This works for 2 players, but it breaks at 3 and more.
For example:
Player 1 : 20 seconds
Player 2 : 20 seconds
Player 3 : 25 seconds
Calculation would be:
Player 1: 100-(20/65*100) = 69 points
Player 2: 100-(20/65*100) = 69 points
Player 3: 100-(25/65*100) = 61 points
This would no longer add up to 100 points in total.
Fair would be something around values of:
Player 1 & 2 (same time) = 35 points
Player 3 = 30 points
My problem is i can't figure out a algorithm which solves this.
And I need the same algorithm for any amount of players. Can someone help with an idea? I don't need a complete finished algorithm, maybe just an idea at which step i used the wrong idea, maybe the sum of all times is already a bad start.
Thx in advance :)
We can give each player points proportional to the reciprocal of their time.
One player with t seconds gets 100 × (1/t) / (1/t) = 100 points.
Of the two players, the one with 50 seconds gets 100 × (1/50) / (1/50 + 1/55) ≈ 52.4, and the one with 55 gets 100 × (1/55) / (1/50 + 1/55) ≈ 47.6.
Of the three players, the ones with 20 seconds get 100 × (1/20) / (1/20 + 1/20 + 1/25) ≈ 35.7, and the one with 25 seconds gets 100 × (1/25) / (1/20 + 1/20 + 1/25) ≈ 28.6.
Simple observation: Let the sum of times for all players be S. A person with lower time t would have a higher value of S-t. So you can reward points proportional to S-t for each player.
Formula:
Let the scores for N players be a,b,c...,m,n. Total sum S = a+b+c...+m+n. Then score for a given player would be
score = [S-(player's score)]/[(N-1)*S] * 100
You can easily see that using this formula, the sum of scores of all players will be always be 100.
Example 1:
S = 50 + 55 = 105, N-1 = 2-1 = 1
Player 1 : 50 seconds => score = ((105-50)/[1*105])*100 = 52.38
Player 2 : 55 seconds => score = ((105-55)/[1*105])*100 = 47.62
Similarly, for your second example,
S = 20 + 20 + 25 = 65
N - 1 = 3 - 1 = 2
For Player 1, (S-t) = 65-20 = 45
Player 1's score => (45/(2*65))*100 = 34.6
Player 2 => same as Player 1
For Player 3, (S-t) = 65-25 = 40
Player 3's score => (40/(2*65))*100 = 30.8
This method avoids any division in the intermediate states, so there will be no floating point issues for the calculations.

How to find the closest set of numbers from a given one?

I am trying to find a proper algorithm for the following task:
I have an amount of resources (actual available quantity). This amount increases constantly at a given rate (increase/min). The goal is to buy all available products from all given options a,...,n (here: option_A, option_B, Option_C).
Now, depending on the rising resources, which product can be bought earlier (here: option_A4, option_B3, option_C3)?
Actual available quantity
Resource A 142
Resource B 56
Resource C 383
Resource D 335
Increase/min
Resource A 2
Resource B 263
Resource C 482
Resource D 301
Option_A ResA ResB ResC ResD bought
Product 1 00032 00066 00058 00008 *
Product 2 00292 00395 00407 00024 *
Product 3 01752 03555 02033 00073 *
Product 4 03505 31999 12200 00294
Product 5 07009 63998 85401 02938
Option_B ResA ResB ResC ResD bought
Product 1 00008 00048 00006 00034 *
Product 2 00049 00240 00012 00134 *
Product 3 00098 01438 00083 00806
Product 4 00491 04314 00499 06451
Product 5 03929 08628 04985 12901
Option_C ResA ResB ResC ResD bought
Product 1 00022 00011 00024 00078 *
Product 2 00111 00106 00122 00699 *
Product 3 00334 00211 00610 04892
Product 4 00669 01477 01831 39137
Product 5 06020 04432 16482 78275
I don't know if there is an algorithm for solving this kind of tasks already out there, but my approaches would be:
Approach A
1. Sum of digits of the actual available quantity
2. Sum of digits of every product
3. Compare the sum of the actual available quantity with each sum of products
4. Identify the product with the less distance
This would be easy, but it pictures only the actual situation without involvement of the constantly rising resources.
Approach B
1. Calculate how long it would take, to reach every single resource depending of the actual amount of resources plus the increasing rate.
E.g. for Option_A, Product 1, ResA:
needed: 3505
available: 142
increase: 2/min
required: 3363 (3505-142)
time after reaching requirements: 1681,5min (3363/2)
2. Repeat for ResB,ResC,ResD and sum the amount of time
3. Repeat 1+2 for every product
4. Choose the product with the shortest amount of time
What do you think?
Looks like you're building a script for resource management game like C&C Tiberium Alliances, haha
My answer would be: your second approach is the way, with a few alterations.
On your second step, you don't sum the amount of time, instead you pick the maximum of them. This is because all the resources are increasing concurrently, right?
See this example:
Res A Res B Res C Res D
Current 142 56 383 335
Increment 2 263 482 301
Product 4 3505 31999 12200 294
Required 3353 31943 11817 0
Time 1676.5 121.5 24.5 0 mins
So you need 1676.5 mins until Res A is enough to buy Product 4 (of Option A), 121.5 mins for Res B, 24.5 min for Res C, and none for Res D as it's already enough.
The time you need to actually be able to buy Product 4 will be 1676.5 mins (i.e., the maximum)
Then you repeat that for every product not bought yet, then sort with increasing time left.
Hope this helps!

Find the optimum number of non uniform bins

R - Problem: to find the optimum number of non-uniform bins to show a range of data points.
I have a bunch of data points (let us assume different prices of different mobiles). I need to categorize these mobile phones into some categories (based on the price). The bin size (in this example refers to the price range) need not be uniform (there might be lots of mobiles in the low price category and few in the long tail category).
Is there any efficient algorithm to find the optimum number of bins required and the number of data points (in this case mobile phones) which shall go into each category.
This is not a standard formula, but wanted to post as it seem to work well with data set i tested.
Find the average price of all the mobiles.
Ex: 5 mobiles with prices 10, 20, 40, 80, 200
Avg is 350/5 = 70
Subtract minimum price from average price: 70 - 10 = 60 -> name it N1
Subtract avg price from Max price: 200 - 70 = 130 -> name it N2
Find the ratio N2/N1 : 130/60: Roughly 2
This indicates that it is better to have 2 bins at the lower price range for every 1 bin at higher range.
So, for example take 2 bins below 70. Range 0 - 35(2 mobiles), 36 - 70(1 mobile)
1 bin above 70: Range 71 - 200(2 mobiles)
As you can see, number of bins and bin sizes are reasonably optimal.

DAX AverageX where table dimension is reduced by one

I'm trying to find the right way to structure a DAX formula to compute a specific average. I think I might be able to construct the average more or less explicitly by using a sum/count construction, but I'm wondering if averagex with an appropriate set of table filters might get the job done.
Specifically, my problem can be explained like this: I'm trying to compute the average cost of a car in DAX, but my data includes the cost of all the components individually (call it body, wheels and engine for now).
Name Year Part Cost
Alice 2000 Engine $10
Alice 2000 Wheels $5
Alice 2000 Body $25
Alice 2001 Engine $8
Alice 2001 Wheels $6
Alice 2001 Body $2
Bob 2000 Engine $10
Bob 2000 Wheels $5
Bob 2000 Body $25
Bob 2001 Engine $8
Bob 2001 Wheels $6
Bob 2001 Body $2
Is there any way to tell DAX that I want to first sum across all the components of the car first, and then compute averages on the data set where the dimensionality of the data has been reduced by one (only the "part" dimension removed)?
For example, the average cost for Alice then would yield
((10+5+25)+(8+6+2))/2 = 28
While if I had a pivot table constructed per name and per year, it would show
Alice 2000 40
Alice 2001 16
etc...
Thanks.
Try this... it works in the case where Name,Year provides a unique combination.
[nCombinations]:=COUNTROWS(SUMMARIZE(Table1,Table1[Name],Table1[Year]))
[TotalCost]:=SUM(Table1[Cost])
[AverageCost]:=CALCULATE([TotalCost]/[nCombinations])
Create a PivotTable with [Name] and [Year] on rows,
Then add [nCombinations] [TotalCost] and [AverageCost] in the body.
Row nCombinations TotalCost AverageCost
Alice 2 56 28
2000 1 40 40
2001 1 16 16
Bob 2 56 28
2000 1 40 40
2001 1 16 16
Grand Total 4 112 28

Resources