Problems in coding small straight for Yahtzee game - c++

So this is for a school assignment obviously, and for some reason I can not wrap my head around coding to calculate a small straight for the game of yahtzee. I have done all the coding for all the other possible rolls except this one. I know this is a question asked a lot but i cant find a simple answer anywhere. I need to be able to do it using if, for, and while loops. If anyone can help it would be massively appreciated!

Sort the rolls and count the number of consecutive dice. If you have four then you have a small straight. If all five rolls are consecutive then you have a large straight.

Related

How to divide vector into quintiles

My question is not so much about code, it's more of an algorithm problem. I am trying to think of a way to divide a vector of doubles into "quintiles" i.e. 5 parts. I then need to find the average of each individual part. The vector can have any number of data points. Does anyone have an idea of how to go about determining what the beginning and end index would be for each quintile? If anyone would like to suggest code, the language I am working with is C++. Thank you.

speed up CPLEX C++ code

This is the result of implementing my very first model in CPLEX C++ and I am very much surprised how slow and poor the quality is. I belief that much of it can be avoided by a better formulation. Can anyone help me improve the code, please? Hints, ideas, thoughts ... everything is appreciated!
It is about scheduling exams within 5 days that have 2 timeslots each available. My input is the number of exams (first row first number) and conflicting exam pairs (first row second number) where I also know the number of students taking both exams(in the following rows -> exam1 exam2 #students taking both exams).
The code you can find here and the instance here.
The constraints I am including are:
each exams is scheduled exactly once
conflicting exams cannot be scheduled at the very same period
penalize if conflicting exams are scheduled on same day
penalize if conflicting exams are scheduled on consecutive days
penalize if conflicting exams are scheduled on adjacent periods over night
I have the feeling that something is even wrong in the formulation because I cannot imagine that the value of the objective value is that high. Does anyone see the flaw? I'm stuck.
The problem might be in the loop where I try to figure out whether a soft constraint is violated or not. There I am looping over days but probably I accidentally overwrite my variables all the time. Does anyone have an idea how to determine the binary variable indicating if the soft constraint is violated on any day (and of course it can happen only once but most probably it is not at the end).
Rather than debugging C++ code I just re-implemented the model using a modeling language (I believe that is quicker and a more pleasant way to spend a sunday evening).
(Note: updated after fixing bug). Here is my solution with a total penalty of 751:
You should be able to plug this into your code and verify the results.
Note: it took about 900 seconds on an old laptop to prove optimality with Cplex. If you want just a good solution, you can stop a little bit earlier:
(Blue line is the objective and the red line is the best possible bound). To get good performance I helped Cplex a little bit with setting branching priorities and using some options suggested by a tuning run.

Ideas Related to Subset Sum with 2,3 and more integers

I've been struggling with this problem just like everyone else and I'm quite sure there has been more than enough posts to explain this problem. However in terms of understanding it fully, I wanted to share my thoughts and get more efficient solutions from all the great people in here related to Subset Sum problem.
I've searched it over the Internet and there is actually a lot sources but I'm really willing to re-implement an algorithm or finding my own in order to understand fully.
The key thing I'm struggling with is the efficiency considering the set size will be large. (I do not have a limit, just conceptually large). The two phases I'm trying to implement ideas on is finding two numbers that are equal to given integer T, finding three numbers and eventually K numbers. Some ideas I've though;
For the two integer part I'm thing basically sorting the array O(nlogn) and for each element in the array searching for its negative value. (i.e if the array element is 3 searching for -3). Maybe a hash table inclusion could be better, providing a O(1) indexing the element?
For the three or more integers I've found an amazing blog post;http://www.skorks.com/2011/02/algorithms-a-dropbox-challenge-and-dynamic-programming/. However even the author itself states that it is not applicable for large numbers.
So I was for 2 and 3 and more integers what ideas could be applied for the subset problem. I'm struggling with setting up a dynamic programming method that will be efficient for the large inputs as well.
That blog post you linked to looked pretty great, actually. This is, after all, an NP-complete problem...
But I bet you could speed it up even further. I haven't done any benchmarks, but I'm gonna guess that his use of a matrix is his single biggest time sink. First, it'll take a huge amount of memory for some really trivial inputs (For example: [-1000, 1000] will need 2001 columns! Good grief!), and then you're wasting a ton of cycles scanning through each row looking for "T"s, which are often gonna be pretty sparse.
So instead: Use a "set" data structure. That'll keep space and iteration time to a minimum,* but store values just as well: If it's in the set, it's a "T"; otherwise, it's an "F".
Hope that helps!
*: Of course, "minimum" doesn't necessarily = "small."

If(), else if() alternative in c++(Is this AI?)

First off, I am a noob. I am also a Janitor that has never made a dime writing code. This is just something that I love doing. It is for fun:) That being said, I wrote this console based tic-tak-toe game that has enough ai to not lose every game. (I guess ai is what it should be called.) It has something like 70 if/else if statements for the computers turn. I used 3 int arrays like so:
int L[2], M[2], R[2];
0 = blank;
1 = X;
2 = O;
The board then 'Looks' likeL[0] | M[0] | R[0]L[1] | M[1] | R[1]L[2] | M[2] | R[2]
So I basically wrote out every possible scenario I could think something like:
if(M[0]==1 & M[1]==1 & M[2]==0){M[2] = 2;}//here the computer prevents a win
else if(L[0] ==2&M[1]==2&R[2]==0){R[2]=2;}//here the computer wins
//and so on....68 more times!
I guess my question(s) is(are): Is there a better way? Is there a way to achieve the same result with less lines of code?Is this considered Artificial Intelligence?
The standard algorithm for this is called Minimax. It basically builds a tree, where the beginning of the game is the root, and then the children represent every possible move X can make on the first turn, then the children of each of those nodes are all the moves O can make in response, etc. Once the entire tree is filled (which is possible for Tic-Tac-Toe, but for games like Chess computers still don't have enough memory), you work your way back up, assuming both players are smart enough to make the best move, and arrive at the optimal move. Here is another explanation of Minimax specifically using Tic Tac Toe as an example.
The Wikipedia page on Tic-Tac-Toe has a very good algorithm outline for winning (or tying) every game:
http://en.wikipedia.org/wiki/Tic-tac-toe which is what I used to make a Tic-Tac-Toe game several years ago.
After you understand the algorithm, one of the cleverest ways to implement a Tic-Tac-Toe computer player is with a magic square. The method is discussed here. As far as size goes, I've seen this implemented in about 50 lines of code, I'll post the code if I find it :)
This isn't technically artificial intelligence, as AI usually refers to artificial neurons, neuron layers, gradient descent, support vector machines, solving complex polynomials, and the like. Solving Tic-Tac-Toe
Yes there are better ways.
The most obvious would be to consider how different mirror views of the board would simplify the number of cases.
Also, consider pre-storing "interesting" patterns in arrays and then comparing the game state against the data. For example, one series of pattern would be all the ways a player can win in the next move.
Also, note that with the declaration int L[2], there are only two entries in array L, namely L[0] and L[1]. The references you have to L[2], M[2], etc. are errors that should have been caught by the compiler. Consider turning up the warning level. How this is done depends on the compiler. For gcc it is -Wall.
This counts as a form of artificial intelligence. The series of if statements are accumulated knowledge: how to recognize a situation and the appropriate best reaction to it.
The closest thing to real AI to solve such a game would be to code an artificial network and train it with all combinations of the tictactoe game.
In that case the code would not do so much if then else to solve the problem but would solve the problem by taking the most reasonable choice that solves the problem from a pattern trained in it.
But coding a neural network is not a trivial thing :)
When in need to code a rule-based system (like the AI you are building), you can use a rule engine, like for example CLIPS (which is a tool developed at NASA for creating Expert Systems written in C).
http://en.wikipedia.org/wiki/CLIPS
Perhaps it's an overkill for playing Tic Tac Toe, but if you are in the mood for learning cool AI stuff, Expert Systems is a very interesting area, but different (and perhaps less trickier) than Neural Networks.
Have fun!

Matrix Libraries vs For Loops in C++

Could you tell me if using a matrix library results in a faster run-time than regular for-loops? Currently, I have some methods that use for-loops that iterate through multidimensional vectors to calculate matrix products and element-wise products, where the matrix size is roughly 1000 columns by 400 rows. This method is the most called method in my program and I would like to know if using a matrix library would increase the program's speed. Also, which library would you recommend (from http://eigen.tuxfamily.org/index.php?title=Benchmark, Eigen seems best to me)?
Thank You
Yes -- a fair number of C++ matrix libraries (E.g., MTL, uBLAS, Blitz++) use template metaprogramming to optimize their behavior. Absent a reason to do otherwise, I'd start with Boost uBlas. You might also want to look at the OO numerics libraries list for other possibilities.
I am trying to answer the question "should I" instead of "which one" because it isn't clear that you actually need such a library.
Would a matrix library improve execution time? Probably. The methods they teach you in high school are certainly not the fastest. However there are other issues to consider.
First, are you optimizing prematurely? Trying to make your program as fast as possible as soon as possible is tempting, but not always the right thing to do. You have to make the determination if doing so is really a valid way to spend your time.
Second, will speed have any significant effect on usability? Making a program work in 2 seconds instead of 4 seconds isn't really worth the effort.... but 30 hours instead of 60 hours? Maybe so. I like to put emphasis on getting everything working before doing the polishing.
Finally, I have met several examples of code somebody else wrote several years before which was utterly useless. Old libraries that couldn't be found or compiled with a new OS or compiler or something different meant that I had to completely rewrite something wasting weeks of my time. It may have seemed like a good idea originally to get that extra few percent performance, but it meant that their code had a limited life span, especially because of poor documentation.
Keep It Simple Stupid is an excellent mantra for so many things. I am a strong advocate for only using libraries when absolutely necessary, and then only using those which seem to be long lived and stable.