Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I cannot figure out what data structure will be the best for this type of the problem:
I have a circle. It can have cuts at different positions(angle). Segments between cuts have color(say red or black). If there are no cuts all circle has one color.
What operations do I need on it?
[1] Change color of the segment.
[2] Add cut at some angle.
[3] For a given angle tell which segment it belongs.
[4] Join consecutive segments of the same color.
Right now I have class for a Segment storing angles of its ends and color.
And ArrayList to work with it.
Problems that I have:
[1]I want something faster than ArrayList. (TreeSet? something else?)
[2]I am treating circle without cuts as a special case. (two fake cuts at 0 and 0)
[3]I am treating Segments containing 0 angle as special case. Say Segments (7pi/8, pi/8) and (pi/8, 7pi/8) require different approach and enormous amount of if conditions.
The idiomatic way to do this in Java is not to use a data structure, but to create your own class. EG: a SegmentedCircle class. Give it the API you wish it had and then implement that as behavior. It'll probably delegate to other classes named Segment or Cut and may have a list of these.
It's usually a safe bet to get your API right/convenient first, then worry about performance second (and only if it's actually needed). In other words, don't pre-optimize.
Since you've told me that you need faster Adds and Removes, the right tool for the job may be a HashSet. From its documentation:
This class offers constant time performance for the basic operations (add, remove, contains and size)
Note: You need to make sure you implement equals and hashcode correctly on your classes for this to work propertly.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to look for a particular object in a large list(java collection) without iterating it. Assume we have a large collection and just need to check whether a object exists or not without iterating.
Ok, Let's step out of the binary world.
Think of a chest full of Lego parts. You want a 2x2 flat black piece.
How would you find it without looking in the chest?
There is no magical to find it, you need to jump into the chest and find the piece grabbing one by one and checking if it's the one you are looking for.
There are ways to speed up the process.
You can Organise (sort your collection) by colour for example and just look in the black pile.
Or you can map (Index your pieces) so you know the position of the piece and can go and retrieve from you know where the piece is.
That is, in a very simplistic way, the same idea for databases and collection.
So, summarizing, no, you can't not just find without looking. Sorry :(
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a project in which I need to create a "robot" that goes around and picks up batteries in a space (100x100 or whatever). I am stuck on the best way to hold his AI instructions. I want to create a list of some kind that has all instructions the robot would ever need. Example: if the robot is in a spot it would check the surrounding spots for a battery, empty space, or a wall. I want to create a lot of robots and give each scenario a random action (go up, down, left, right, etc) Each turn the robot does something. The robots with the best outcome (highest batteries) get to the next level.
Maybe this was too many details for my question but, would an array be the best way to hold the instructions?
Looking for a push in the correct direction. I am new to Java having gone through just 1 beginner class of it so far. Not looking for the code itself (examples are nice but I want to do my own work), of course, just ideas as to where to begin with the AI instructions.
To start with you should forget about what sort of data structures you are going to need, and start from the beginning. What are the robots legal moves - write that down, and get it crystal clear on how you want the robot to run.
You should write step by step use cases for each of the options which include what the positions are, sensors, batteries etc.
Once you have written all this down, the data structure will be much simpler to identify.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to implement an Auto-encoder by my own in Java. From the theory, I understood that auto-encoder is basically a symmetric network.
So, if I chose to have 5 layers in total, do I have to use 9 layers in training (back propagation) phase or 5 layers enough?
I've been reading theory but they are too abstract and full of math formulas, I could not get any implementation details via google.
What's the usual way of doing this?
An Auto-encoder, in training phase, using back propagation, tries to get the output similar to the input with a goal to minimize the error. It is shown above. The number of layers in the above image are 7 while the actual layers are 4 after the training. So, while training can I implement the back-propagation with just 4? If so, how can I do this?
Simple backpropagation won't work with so many layers. Due to so called vanishing gradient pehenomen, networks having more than two hidden layers won't learn anything reasonable. In fact, best results are obtained with one hidden layer. So in case of autoencoder you should have INPUT layer, HIDDEN layer and OUTPUT layer. No need for more, the Universal Approximation Theorem clearly shows, that this is enough for any problem.
From the OOP point of view it depends whether you plan to reuse this code with different types of neurons, and by type of neuron I mean something deeper than just different activation function - different behaviour (stochastic neurons?); different topologies (not fully connected networks). If not - modeling each neuron as a separate object is completely redundant.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am currently working on a text based zombie RPG simulator where there will be a randomly generated city to explore and do things in. I was originally planning to save the city in a 2D array, but then I realized that if I would do that, my array would take up gigabytes of data for just number storage. After doing some research I discovered something called perlin noise, which is an algorithm that generates virtually infinite and random looking terrain based on a single seed, and I'm curious if I could do something similar with my city. Any ideas guys?
I realize I didn't leave you guys with much information about what I need.
The city I want generated should be represented by a grid of coordinates, where each coordinate plots a separate
building on the map. Buildings however should be arranged in a specific way. If it is a financial district, then office buildings and should spawn more frequently. If it is a residential district then buildings like apartment buildings and laundromats should be generated more frequently. If it is a park, then no buildings should be generated.
Also, some buildings should take up more coordinates than others, such as malls and such.
Highways across the city should also be generated, but this is not important if it complicates the code too much, as this is meant to be a simple learning project.
Perlin noise mainly works by combining several "images" (see for instance here: http://devmag.org.za/2009/04/25/perlin-noise/) where some have lower frequencies and other higher. So what you could do is use the value at a certain coordinate of the lower frequencies to determine the type of area in your city. Say for instance the pixel of the image at the lowest frequency is less than 0.5 and that of the second lowest as well, you could state that you are in a financial part of your city. Then the higher frequencies (who vary more) determine the type of building or object (tree,...).
However you will have to experiment with parameters to make it quite realistic.
I know this is kind of a basic answer, but feel free to comment on this procedure.
Roads can probably not be generated using Perlin noise itself, but for instance by an algorithm that does some postprocessing (say for instance you are are in a financial district, you could program a rule that if there is a building on the left of a certain tile and on the right and there is no building at the tile itself, you should introduce a road.
You can use random points and a relaxation like in this tutorial: http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/. It also uses a delaunay triangulation and the dual voronoi-diagram and noisy edges.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have to determine if a package can be delivered based on rules as follows...
If packageWeight > 70 and below 100
AND
If destinationState is in the special list of allowed state {MA, PA, NY, NJ, LA,....}
AND
contentType is any of the following {ACD, FDY, PRZ, QUO, ..... }
AND
CarrierName is any of the following {FXD, USPS....}
if the above conditions are true then we can deliver the package
and in addition there are 3 flags with 15x16x8 possibilities?
what is the best way to avoid a if else cluster ?
hash map?
any other suggestions ?
You can break down different group of conditions in different functions. For. e.g. Define a function isCarrierSupported that returns true if carrier is one of FXD, USPS etc. Similarly you can define multiple functions like isWeightCorrect etc. Then later in main program, you can just call these functions and get final result.
This does not avoid ifs and elses but neatly organizes the program to make it more readable.
What are looking for a is a rules engine, then just use one that already exists.
This isn't a trivial domain to be trying to create something from scratch. Just like you should not be writing your own caching system or your own ORM or your own threading framework.
Don't make the mistake of rolling your own it will end in tears.
Since you are using Java, go look at Drools. Save yourself and your company a bunch of money and use something that is already built for you.
HashMap would work or You could convert whole flag-state into 11-bit mask (15-4 bits, 16 - 4 bits, 8 - 3 bits, or leave more bits for further growth) and use true/false array by 11-bit index.
The downside is - You'll anyway have to code same if/else logic to fill the map/array or to define a constant describing all deliver (or don't-deliver) states.
P.S. switch/case looks more appropriate then if/else here...