Using JavaScript/jQuery to generate fixtures - logic

I'm putting together a tool for a colleague which helps to create a nice fixture list. I got about 2/3 through the tool, collecting various data ... and then I hit a brick wall. It's less of a JavaScript problem and more of a maths/processing brainblock.
Lets say I have 4 teams, and they all need to play each other at home and away. Using this tool - http://www.fixturelist.com/ - I can see that a home and away fixture with 4 teams would take 6 weeks/rounds/whatever. For the life of me, though, I can't work out how that was programmatically worked out.
Can someone explain the logic to process this?
For info, I would use this existing tool, but there are other factors/features that I need to work in, hence doing a custom job. If only I could understand how to represent that logic!

In your example of 4 teams, call them a, b, c and d:
a has to play b, c, d
b has to play c, d (game against a already included in a's games)
c has to play d (game against a already included in a's games, against b already included in b's games)
If they need to play at home and away, that's 12 games. You can play at most 4/2 = 2 games a week, so that's 6 weeks.
With n teams you need x games, where:
x = ((n-1 + n-2 + n-3 ...) * 2)
This takes y weeks, where:
y = x/(n/2) = 2x/n
This can be simplified with an arithmetic series fairly easily, or calculated with a for loop if you want.

Related

Recursion algorithm for multilevel report

I'm currently developing a multi-level SSRS report, and I'm struggling with the algorithm. I've developed a recursion class which looks like below, but the level numbers are incorrect. I want the parent record (represented by a, b, and c) to show the child records so that the child records' level = (parentRecLevel+1). Right now, the level values just increment by 1. Anyone have any advice?
protected BOMLevel getBomLevelItem(str itemId, int numLevel, boolean firstRec)
while select tmpBOM
{
bomLevel = this.getBomLevelItem(bomLevel.ItemId, bomLevel.Level, false);
}
Current Outcome (where b1, c1, and c2 are children of b and c respectively):
1 a
2 b
2 b1
3 c
4 c1
5 c2
Wanted Outcome:
1 a
2 b
3 b1
2 c
3 c1
3 c2
TLDR: Do not reinvent the wheel, use existing algorithms and frameworks.
I'm assuming your question is not for a training exercise, but a real world problem. If it is an exercise, try to get a good grasp of recursion in an easy to use language of your choice with a big community before coming back to x++.
Your recursion method looks incomplete, because in each recursion, you iterate through all records of tmpBom, which (unless you modify the records in that table somewhere else) does not make sense and will never terminate. I also don't see how this method could produce the outcome you describe. I suggest you take a look at some recursion algorithm training material to learn about the fundamental parts of a recursion.
You tagged the question x++ and the syntax also looks very much like that. Unfortunately you did not add the information which version of microsoft-dynamics you are using, but I will assume dynamics-ax-2012 as it is currently the most common version in use.
In this version, there is already an out-of-the-box SSRS report that will show you the structure of a bill of material. You can call the report at Inventory management > Reports > Bills of materials > Lines. It should be fairly easy to modify this report so that it also shows the level if the report does not already fulfill your requirements.
If you still need to implement your own solution, take a look at class BOMSearch and its children. It is used in several places (check the cross references) and can also used to expand/explode a bill of material.
Also note that there are a lot of articles out there that try to explain how to expand or explode a bill of material in x++ code, but as with all things on the internet, be careful: Most of them are incomplete or plain wrong.

Queue-like datastructure that is readable from anywhere

I'm working on a game in Yoyo Game Maker where I have items moving along conveyor belts. As the items only move in one direction, I thought it would make most sense to use a queue or queue-like data structure to store the items. However, to be able to render the items, I need to be able to read all of them at any point in the queue, not just the head or tail.
[[a] [b] [c] [d]]
|
V
a <- [[ ] [b] [c] [d]] <- e
|
V
[[b] [c] [d] [e]]
| | | |
V V V V
b c d e
I could simply use an array that manually moves all its values forward by one slot every turn (using a for loop), but somehow that seems inefficient, laggy, or at the very least, bad form. My programming instincts recoil at the thought of using such a system, anyway.
Is this a correct assumption to make? Is an array really the best way of implementing a structure like this? Should I even be worried about efficiency, or are the differences in this case negligible?
Some advice or examples (in any programming language) would be greatly appreciated.
It might "seem" inefficient to use an array, but it most likely won't be. Take into consideration how many items will actually be on the conveyor belt at any single time. If you want quick random access to any index in a data structure, you must use an array, a DS List or a DS Grid(wouldn't make sense here, tough).
Using a DS List, you can use ds_list_delete(your_list, 0) to 'dequeue', much like a DS Queue, and ds_list_insert(your_list, 0, value) to 'enqueue' an item.
Iterating through the list is then very simple:
for ( var i = 0; i < ds_list_size( your_list ); i++ ) {
var item = your_list[|i];
}
It might also be relevant to add, that, in a game I'm working on, objects are build using components, which basically means that all of the enemies, the player, and such have a for-loop in their Step events to iterate through and update all components if need be. At most I have about 80 such objects in the game at any time and performance hasn't been a problem yet.
You should always first try to get a working solution, then testing it in conditions specific to your game: eg. if you are going to need a 100 items in the structure, try that, and if the performance is not satisfactory, only then optimize.
I had made a game in game maker which had a similar requirement. If you know about the frog game it had some woods floating on a river.
Movement: For that, I had created some number of the wood object which moves in the particular direction. when frog collides with any one of them he/she starts to float in that direction. Also, if it goes out if window in one direction I would place it back at the other side of window.
Picking: I think to pick up one of the item, You will need to define the collision inside each object. After collision you can define you next steps.

On Two Plus Two poker hand evaluator, how do you get the best 5 cards combination out of the 7 that you passed to it?

Is it possible to extract that info from the equivalence value?
I understand that the higher the equivalence value the better. Category and rank can also be extracted from the equivalence value. But is there a way to find out what the best 5 cards combination are from the 7 that you passed to it?
Twoplustwo is the fastest poker hand evaluator around (14-15 million hands evaluated per second). You give your 7 cards to it and it spits out a hand equivalence value. The higher the value, the better is card is.
Here's a great summary on twoplustwo: http://www.codingthewheel.com/archives/poker-hand-evaluator-roundup#2p2
Cached version of the link above:
http://web.archive.org/web/20130116102452/http://www.codingthewheel.com/archives/poker-hand-evaluator-roundup
(disclaimer: I'm working on a poker software that does, amongst other, hand evaluations)
You give your 7 cards to it and it spits out a hand equivalence value.
there are several evaluators doing that and if I'm not mistaken some of them compute more than a hundred million hands per second (!). These evaluators basically come down to 7 array lookups in a gigantic array and it only takes a few cycles (despite the cache misses) to evaluate a hand. I don't think 14-15 millions / second is anywhere near the fastest. CactusKev's evaluator is 10x faster if I'm not mistaken.
Now to answer your question:
how do you get the best 5 cards combination out of the 7 that you
passed to it?
Well it doesn't tell you but once you have the strength of the hand it can become very easy: you don't need to re-invent the wheel.
You can use the strength to simplify your "best five out of seven" computation.
You could also use other libraries, giving your directly the five best card (instead of just their strength) or you can use the strength to find the five best cards yourself.
I'll just give a few examples...
You know you have a full house (a.k.a. a "boat"), then you know that you're looking for three cards that have the same rank and then the best pair (if there are two pairs, but you're sure to find at least one, because the evaluator told you you have a boat).
You know you have a straight: find five cards that follow each other, starting from the best one (beware of the special wheel case case).
You could also get a bit fancier for the straight: you could take the strength of every possible straight and compare the strength that the evaluator gives you with these. If it matches, say, a ten-high straight, then simply look for any T, 9, 8, 7 and 6 card (no matter the suit).
You know you have "no pair": simply take the five highest card you find
etc.
There are only a few different ranks... They could be, for example:
NO_PAIR
ONE_PAIR
TWO_PAIRS
SET
STRAIGHT
FLUSH
FULL_HOUSE
FOUR_OF_A_KIND
STRAIGHT_FLUSH
(you could of course create intermediate "wheel straight" and "wheel straight flush" and "royal flush" cases if you want, etc.)
Once you know the which type of hand your hand is (thanks to the fast evaluator you're using), simply switch to a piece of code that finds the five best out of seven for that particular hand.
I think it's a good way to proceed because you leverage the ultra-fast evaluator and it then greatly simplifies your logic.
At startup, you'd need to compute the strenth once, for example by computing:
HIGHEST_NO_PAIR_HAND = ultraFastEvaluator( "As Kd Qh Jc 9d 5s 2c" );
HIGHEST_FULL_HOUSE = ultraFastEvaluator( "As Ac Ad Kh Ks 8s 2h" );
I'm of course not advocating to use strings here. It's just an example...
You could then, for each hand you want to find the actual five best:
compute the strength using the fast evaluator
is it <= HIGHEST_NO_PAIR_HAND ?
yes: take five highest cards
no: is it <= HIGHEST_ONE_PAIR_HAND ?
yes: take highest pair + highest three cards
no: is it <= HIGHEST_TWO_PAIRS_HAND?
etc.
So in my opinion you could reuse an API that directly finds the five best out of seven or entirely rewrite your own, but it's going to faster if you use the fast evaluator's result to then simplify your logic.
EDIT note that there's not necessarily one way to do "five best out of seven". For example with As Ac on a Kc Kd Qh Qs 2c board, both "As Ac Kc Kd Qh" and "As Ac Kc Kd Qs" are "five best" (the last queen's suit doesn't matter).
No, it's not possible to extract that information. The lookup table contains only the equivalence values, which are broken into hand type and rank; no other information is preserved.
If you need to evaluate millions of hands per second and get the winning hand for each, instead of just the rank, you'll need to use a different evaluator. If you only need to extract the winning hand rarely, you could use this evaluator, and resort to a slower method to find the best 5 cards when necessary.
Old post but I'll give it a shot. If you're using a table lookup (e.g., the 7-card arrays mentioned above, aka the Ray Wotton method), construct a second table with your target information in the same slot positions. Example: I ended up in slot 167,452 to find my eval, now I'll look at my other array in slot 167,452 to find my 5-card hand.
A single card can be represented by 6 bits - 2 for the suit and 4 for the rank. 30 bits would give you the entire 5-card hand. Maybe not quite that simple, but that's the general idea. I've used this exact technique for some stuff I did awhile back.
Alternatively, you could pass in all the 7-choose-5-card combinations (21 of 'em I believe) and find out which matches the original eval.
The twoplustwo hand evaluator can evaluate five cards hands. Here's the code for that in C#:
int LookupFiveCardHand(int[] cards) {
//assert cards size is 5
int p = HR[53 + cards[i++]];
p = HR[p + cards[i++]];
p = HR[p + cards[i++]];
p = HR[p + cards[i++]];
p = HR[p + cards[i++]];
return HR[p];
}
Notice there's 6 array look-ups despite there being 5 cards.
Anyways, since the evaluator is so fast, you can just compare every possible 5 card combination. A hand containing 7 card hands will have twenty-one 5 card combinations. Code in C#:
List<int> GetBestFiveCards(List<int> sevenCardHand) {
List<List<int>> fiveCardHandCombos = new List<List<int>>();
// adds all combinations of five cards to fiveCardHandCombos
for (int i = 0; i < sevenCardHand.Count; i++) {
for (int j = i+1; j < sevenCardHand.Count; j++) {
List<int> fiveCardCombo = new List<int>(sevenCardHand);
fiveCardHandCombos.RemoveAt(j); // j > i, so remove j first
fiveCardHandCombos.RemoveAt(i);
fiveCardHandCombos.Add(fiveCardCombo);
}
}
Dictionary<List<int>, int> comboToValue = new Dictionary<List<int>, int>();
for (int i = 0; i < fiveCardHandCombos.Count; i++) {
comboToValue.Add(fiveCardHandCombos[i], LookupFiveCardHand(fiveCardHandCombos[i]));
}
int maxValue = comboToValue.Values.Max();
return comboToValue.Where(x => x.Value == maxValue).Select(x => x.Key).First(); //grab only the first combo in the event there is a tie
}

Are Haskell List Comprehensions Inefficient?

I started doing Project Euler and got to problem number 9. Since I was using Project Euler to learn Haskell, I decided to use list comprehensions (as shown in Learn You A Haskell). I do that and GHCI takes awhile to figure out the triplet, which I figured is normal because of the calculations involved. Now, at work yesterday (I don't work as a programmer professionally, yet) I was talking to a friend who knows VBA and he wanted to try to find the answers in VBA. I thought it would be a fun challenge as well, and I churn out some basic for loops and if statements, but what got me was that it was much faster than Haskell was.
My question is: are Haskell's list comprehension incredibly inefficient? At first I thought it was just because I was in GHC's interactive mode, but then I realized VBA is interpreted too.
Please note, I didn't post my code because of it being an answer to project euler. If it will answer my question (as in I'm doing something wrong) then I will gladly post the code.
[edit]
Here is my Haskell list comprehension:
[(a,b,c) | c <- [1..1000], b <- [1..c], a <- [1..b], a+b+c=1000, a^2+b^2=c^2]
I guess I could've lowered the range on c but is that what is really slowing it down?
There are two things you could be doing with this problem that could make your code slow. One is how you are trying values for a, b and c. If you loop through all possible values for a, b, c from 1 to 1000, you'll be spending a long time. To give a hint, you can make use of a+b+c=1000 if you rearrange it for c. The other is that if you only use a list comprehension, it will process every possible value for a, b and c. The problem tells you that there is only one unique set of numbers that satisfies the problem, so if you change your answer from this:
[ a * b * c | .... ]
to:
head [ a * b * c | ... ]
then Haskell's lazy evaluation means that it will stop after finding the first answer. This is the Haskell equivalent of breaking out of your VBA loop when you find the first answer. When I used both these tips, I had an answer that completed very quickly (under a second) in ghci.
Addendum: I missed at first the condition a < b < c. You can also make use of this in your list comprehensions; it is valid to say things along the lines of:
[(a, b) | b <- [1..100], a <- [1..b-1]]
Consider this simplified version of your list comprehension:
[(a,b,c) | a <- [1..1000], b <- [1..1000], c <- [1..1000]]
This will give all possible combinations of a, b, and c. It's kind of like saying, "how many ways can three one-thousand-sided dice land?" The answer is 1000*1000*1000 = 1,000,000,000 different combinations. If it took 0.001 seconds to generate each combination, it would therefore take 1,000,000 seconds (~11.5 days) to finish all combinations. (OK, 0.001 seconds is actually pretty slow for a computer, but you get the idea)
When you add predicates to your list comprehension, it still takes the same amount of time to compute; in fact, it takes longer since it needs to check the predicate for each of the 1 billion combinations it computes.
Now consider your comprehension. It looks like it should be much faster, right?
[(a,b,c) | c <- [1..1000], b <- [1..c], a <- [1..b], a+b+c=1000, a^2+b^2=c^2]
There are 1000 choices for c. How many are there for b and a? Well, the average choice for c is 500. For all choices of c, then, there are an average of 500 choices for b (since b can range from 1 to c). Likewise, for all choices of c and b, there are an average of 250 choices for a. That's very hand-wavy, but I'm fairly sure it's accurate. So 1000 choices for c * 1000/2 choices for b * 1000/4 choices for a = 1 billion / 8 ~= 100 million. It's 8x faster, but if you paid attention, you'll notice it's actually the same big-Oh complexity as the simplified version above. If we compared "simplified" vs "improved" versions of the same problem, but from [1..100000] instead of [1..1000], the "improved" would still only be 8x faster than the "simplified".
Don't get me wrong, 8x is a wonderful constant-factor speedup. But unless you want to wait a couple hours to get the solution, you'll need to get a better big-Oh.
As Neil noted, the way to reduce the complexity of this problem is, for a given b and c, choose the a that satisfies a+b+c=1000. That way, you're not trying a bunch of as that will fail. This will drop the big-Oh complexity; you'll only be considering approximately 1000 * 500 * 1 = 500,000 combinations, instead of ~100,000,000.
Once you get the solution to the problem you can check out other peoples versions of Haskell solutions on the Project Euler site to get an idea of how other people have solved the problem. Incidentally, here is a link to the referenced problem: http://projecteuler.net/index.php?section=problems&id=9
In addition to what everyone else has said about generating fewer elements in the generators, you can also switch to using Int instead of Integer as the type of the numbers. The default is Integer, but your numbers are small enough to fit in an Int.
(Also, to nitpick, Haskell list comprehensions have no speed. Haskell is a language definition with very little operational semantics. A particular Haskell implementation might have slow list comprehensions, though.)

Aggregating automatically-generated feature vectors

I've got a classification system, which I will unfortunately need to be vague about for work reasons. Say we have 5 features to consider, it is basically a set of rules:
A B C D E Result
1 2 b 5 3 X
1 2 c 5 4 X
1 2 e 5 2 X
We take a subject and get its values for A-E, then try matching the rules in sequence. If one matches we return the first result.
C is a discrete value, which could be any of a-e. The rest are just integers.
The ruleset has been automatically generated from our old system and has an extremely large number of rules (~25 million). The old rules were if statements, e.g.
result("X") if $A >= 1 && $A <= 10 && $C eq 'A';
As you can see, the old rules often do not even use some features, or accept ranges. Some are more annoying:
result("Y") if ($A == 1 && $B == 2) || ($A == 2 && $B == 4);
The ruleset needs to be much smaller as it has to be human maintained, so I'd like to shrink rule sets so that the first example would become:
A B C D E Result
1 2 bce 5 2-4 X
The upshot is that we can split the ruleset by the Result column and shrink each independently. However, I cannot think of an easy way to identify and shrink down the ruleset. I've tried clustering algorithms but they choke because some of the data is discrete, and treating it as continuous is imperfect. Another example:
A B C Result
1 2 a X
1 2 b X
(repeat a few hundred times)
2 4 a X
2 4 b X
(ditto)
In an ideal world, this would be two rules:
A B C Result
1 2 * X
2 4 * X
That is: not only would the algorithm identify the relationship between A and B, but would also deduce that C is noise (not important for the rule)
Does anyone have an idea of how to go about this problem? Any language or library is fair game, as I expect this to be a mostly one-off process. Thanks in advance.
Check out the Weka machine learning lib for Java. The API is a little bit crufty but it's very useful. Overall, what you seem to want is an off-the-shelf machine learning algorithm, which is exactly what Weka contains. You're apparently looking for something relatively easy to interpret (you mention that you want it to deduce the relationship between A and B and to tell you that C is just noise.) You could try a decision tree, such as J48, as these are usually easy to visualize/interpret.
Twenty-five million rules? How many features? How many values per feature? Is it possible to iterate through all combinations in practical time? If you can, you could begin by separating the rules into groups by result.
Then, for each result, do the following. Considering each feature as a dimension, and the allowed values for a feature as the metric along that dimension, construct a huge Karnaugh map representing the entire rule set.
The map has two uses. One: research automated methods for the Quine-McCluskey algorithm. A lot of work has been done in this area. There are even a few programs available, although probably none of them will deal with a Karnaugh map of the size you're going to make.
Two: when you have created your final reduced rule set, iterate over all combinations of all values for all features again, and construct another Karnaugh map using the reduced rule set. If the maps match, your rule sets are equivalent.
-Al.
You could try a neural network approach, trained via backpropagation, assuming you have or can randomly generate (based on the old ruleset) a large set of data that hit all your classes. Using a hidden layer of appropriate size will allow you to approximate arbitrary discriminant functions in your feature space. This is more or less the same idea as clustering, but due to the training paradigm should have no issue with your discrete inputs.
This may, however, be a little too "black box" for your case, particularly if you have zero tolerance for false positives and negatives (although, it being a one-off process, you get an arbitrary degree of confidence by checking a gargantuan validation set).

Resources