Easy applications for learning Java Data Structures - java

I have been struggling to think of some decent uses for things like vectors and stacks. Since I find myself best able to remember things once i've done something useful with it. I was after some short but useful applications you've found for several of the java data structures.
I'm not after any code samples, but more things that stick in your mind as 'that was a really great use of a hashmap/linked list etc' - things that i could then go on to try myself.

"Usefulness" is a subjective term, but in any case, an intuitive way to learn data structures is to use them to simulate real-life activities.
Stack
Simulate a secretary that is shredding a bunch of documents. She has -- guess what? -- a stack of documents on her desk, and she shreds them one by one by picking the top document and feeding it into the shredder, repeating this until all documents are shredded.
Her boss would intermittently come over to her desk and put a new document to shred on top of her stack.
Circular doubly linked list
Simulate kids playing in the playground. The kids stand in a circle, then each kid would -- guess what? -- link up by holding hands with the kid to the left (with the left hand) and to the right (with the right hand).
Do "Eeny, meeny, miny, moe" around the circle, say starting from the youngest kid. The "it" kid would then have to leave the circle, and the gap is closed in the most natural way, i.e. by having the two kids around the gap link up.
Restart the "Eeny, meeny, miny, moe" from the gap. Go the opposite direction on a whim. Do this until one kid remains.
Map
Dog says woof. Cow says moo. Yeah, simulate that.

A good use for a Stack would be bracket matching. Write a small program that will parse some input and report back if the bracket syntax was correct (i.e. every open bracket has a corresponding closing bracket).

How about an RPN Calculator for Stacks? Vectors can be applied to almost any problem.

Related

How do I make a reinforcement learning agent in Java?

I have a challenge that my teacher gave to beat an army of his soldiers on a 18x24 grid, with random obstacles placed on the board. The game is turn based and I have an army of 50 soldiers, each of which needs to either move or attack on their turn.
My problem is I only have access to creating a class of soldiers to fight in this environment. Currently I have a method that evaluates the board position by looking at how many soldiers there are left from each team and does yourTeam - enemyTeam to get the current score, and I have a method that will produce the legal moves for the soldier.
I want to know how I would create a reinforcement learning agent in Java with what I have access to. If you know any ways to do this or any resources that may help that would be great. Thank you for the help!
Java is not a good language for doing math heavy computation (which is what you will need to do for RL). You could attempt to implement the Q-Learning, value-iteration or policy-iteration algorithms but I would avoid doing anything with neural networks/modern deep RL approaches here as your work load will increase dramatically.
With regard to your problem, if you are to implement one of the old-school algorithms. Think about your state and action space. I have serious concerns about the size of your action space, even with a small number of moves for each solider (say 3 - attack, move up, move down) with 50 soldiers the action space will be very large - 50^3, even this many will be difficult to deal with, any more (even 4 or 5) will send you deep into some complex topics in RL.
Other problems are - defining a good reward signal, efficiently running (potentially millions) of simulated games.
The short answer is, this is not something to be taken lightly, it would be challenging and time consuming even for someone who has experience in the field and using Java is a no-no (Python is better). Given you probably don't have long to find a good solution, I would recommend trying a different approach - planning based maybe, or hard coding a reasonable strategy.
If you still want to go ahead and read up on the topic here are some good resources:
Reinforcement Learning an Introduction (Sutton & Barto) - any edition is fine
Selected chapters in Artificial Intelligence: A Modern Approach (Russel & Norvig)
Hope this helps and sorry it may not have been the answer you we hoping for!

I cant decide the best way to store strings for what I'm trying to achieve

I have been given the task to make a text based game and I am opting to go with a dating sim. I have all the dialogue written out but I am stuck on where I would store the dialogue. At first I thought of just making a lot of if statement ex( if you pick this option it takes you to this) but this seems to be really inefficient. So I dont know whether it would be better to have them in an array or array list and just go to that specific place in that array. Sorry if that was confusing that was some background I guess im asking what is the best way to store strings in a game revolving around conversations.
Your question is too vague but of course, other people before has been working on it.
A good starting point (with lot of references) could be, "Gameplay Design Patterns for Game Dialogues" Jenny Brusk and Staffan Björk
On the other hand, Dialog Trees (and here and here) is a way to store dialogs but can work or not for your specific needs.

Concurrently search a game tree using minimax and AB pruning. Is that possible?

I'm going be competing in a board game AI competition at my school and am trying to come up with some ideas for concurrency to gain an edge. I will most likely be at a disadvantage because I will be implementing it in java and I understand c or c++ would be much faster.
It doesn't seem like you could just split the game tree in half because of the move ordering which should leave the best moves first and it seems that it would be difficult or maybe even impossible to communicate the current alpha/beta at a given depth. I'm going to be using transposition tables as well which would need to be synchronized.
Besides searching, is there something that a second thread could be doing which could aid in the search or provide some type of speed increase. Each AI will have 5 seconds to make a move and your program can be working while the opponent is thinking.
Any input, no matter how obscure, would be appreciated.
An overview can be found in the Chess Programming Wiki's parallel search article. Even if your actual game is not chess, many concepts will also apply. The site also covers sophisticated solutions for shared transposition tables.
However, when you don't have much time, I would not start with a parallel search. You are correct that parallelism can increase the strength of the search algorithm. It is very difficult to get it right, though, and the benefits are way lower than one would expect.
If you want to experiment with parallelism, go ahead. It is an interesting topic. However, if you just want to get the best results in a limited amount of time, I would recommend to stick with a sequential search, and instead focus on move ordering and correctness.
It is possible. You have to make communication between threads to have AB prunning help. Also, move ordering must be tweaked, it doesn't help if one thread has the best-rated moves to analyze while the others not.

Feature/blob correlation and histogram analysis

I'm working on a sketch search engine that correlates whatever someone's sketching with a picture in the database (the db is just about 40 pictures now). I'm doing this mostly for fun so I'm not that well-versed in computer imaging techniques.
First of all, are there any rules of thumb on how one should create histograms (bin sizes, ranges, etc)? I'm using some histogram code found at http://www.scribd.com/doc/6194304/Histograms (but ported to JavaCV). Sometimes I get good results, sometimes I get bad results, most of the time I get "meh" results. I've been experimenting a TON with bin sizes and ranges and I'm wondering if comparing higher dimensional histograms may be the answer here.
Second of all, it seems that black makes a very strong presence in my current histogram setup (even a black dot shifts the entire result set). Should this be expected? Or did I screw something up? Example:
And after the dot:
Note how I'm already getting pictures of "earthrise" as "close" matches.
I'm also wondering what methods I should use for blob or feature analysis. I think that stuff like SURF may be overkill because I only want to broadly compare blobs, not accurately map templates. Is there any way I can compare the edges after being passed through a Canny filter? (Low complexity if possible):
For example, here, I want the two smiley faces to be at the top because the needle smiley "blob" is more closely related to the smily face shape than to a bunch of passion fruit or a galaxy.
Phew long question. If you want to try out the engine for yourself, go to http://skrch.dvt.name/ (shameless plug, I know, I know -- only works in FF/Chrome/Safari). Maybe more experienced computer vision people can make suggestions based on results. Oh, I'm using the CV_COMP_BHATTACHARYYA distance when comparing histograms (it seemed that it gave the best results although chi-square isn't bad either).
Is there a background ?
IS it significant ?
Maybe you need to look at whether there is a user-supplied background or not.
then you "just" need to have 2 histogram per db entry, one with bg, one without.
That'll stop earthrise looking like an apple with a dot.
for basic bg separation, try a canny, then taking "outside" and removing it from a copy of the original.

Solving The 8 Puzzle With A* Algorithm

I would like to solve/implement the 8 puzzle problem using the A* algorithm in Java. Am asking if someone can help me by explaining to me the steps i must follow to solve it. I have read on the net how the A* works but i don't know how to begin the implementation in Java.
I will be very grateful if you guys can help me and give me the guidelines so that i can implement it myself in Java. I really want to do it to be able to understand it, so i just need the guidelines to start.
I will use priority queues and will read the initial configuration from a text file which looks like for example this:
4 3 6
1 2 5
7 8
Pointers to other sites for more explanation/tutorials are welcome.
I'd begin with deciding how you want to represent the game board states,
then implement the operators (eg. move (blank) tile up, move (blank) tile down, ...).
Typically you will have a data structure to represent the open list (ie. those states
discovered but as yet unexplored (ie. compared with goal state) and another for the
closed list (ie. those states discovered and explored and found not to be the goal state).
You seed the open list with the starting state, and repeatedly take the "next" state to
be explored from the open list, apply the operators to it to generate new possible states
and so on ...
There is a tutorial I prepared many years ago at:
http://www.cs.rmit.edu.au/AI-Search/
It is far from the definitive word on state space searching though, it is simply an educational tool for those brand new to the concept.
Check http://olympiad.cs.uct.ac.za/presentations/camp1_2004/heuristics.pdf it describes ways of tackling this very problem.
A* is a lot like Djikstra's algorithm except it includes a heuristic. You might want to read that wiki or read about single-source shortest path algorithms in general.
A lot of the basic stuff is important but obvious. You'll need to represent the board and create a method for generating the possible next states.
The base score for any position will obviously be the minimum number of actual moves to arrive at it. For A* to work, you need a heuristic that can help you pick the best option of possible next states. One heuristic might be the number of pieces in the correct position.

Categories