I am trying to create a custom map format for my own little 2D RPG, so my question is rather how do I manage reading and creating a custom map format properly and flexible. First off, I am writing my code in Java. The idea was to have a class called 'TileMap'. This class defines a 2-dimensional integer - array where all my entities are stored ( I'm using an entity-system to realize my game ). I also want to save and parse some information about the size of the map before the actual reading process happens. The map file should look much like this:
#This is a test map
width=4
height=3
layercount=1
tilesize=32
[1;0;0;0]
[23;1;0;0]
[5;0;1;0]
where layercount is the number of layers the z-dimension offers. and tilesize is the size of every tile in pixels. Entities are defined in between the brackets. The pattern goes: [entity_id;x_pos;y_pos;z_pos]. I already wrote the code to parse a file like this but its not very flexible because you just have to put one tiny whitespace in front of the square brackets and the map can't load up. I just need some few helpful tips to do this in a flexible way. Can anybody help me out?
I think that may have 3 different ways to solve that:
First, you can use a Map with Maps: Map<Serializable,Map<String,Object>> where Serializable is your entity_id, and the map are the attributes that you need, like ("width",4), ("height",3):
public static final String WIDTH = "WIDTH";
public static final String HEIGHT = "HEIGHT";
...
Map<String,Object> mapProperties = new HashMap<String,Object>();
mapProperties.put(WIDTH, 4);
mapProperties.put(HEIGHT, 3);
....
Map<Serializable,Map<String,Object>> map = new HashMap<Serializable,Map<String,Object>>();
map.put(myEntity.getId(), mapProperties);
Second way could be like this: http://java.dzone.com/articles/hashmap-%E2%80%93-single-key-and
Third way could be like this: Java Tuple Without Creating Multiple Type Parameters
Related
The base problem is trying to use a custom data model to create a DataSetIterator to be used in a deeplearning4j network.
The data model I am trying to work with is a java class that holds a bunch of doubles, created from quotes on a specific stock, such as timestamp, open, close, high, low, volume, technical indicator 1, technical indicator 2, etc.
I query an internet source, example, (also several other indicators from the same site) which provide json strings that I convert into my data model for easier access and to store in an sqlite database.
Now I have a List of these data models that I would like to use to train an LSTM network, each double being a feature. Per the Deeplearning4j documentation and several examples, the way to use training data is to use the ETL processes described here to create a DataSetIterator which is then used by the network.
I don't see a clean way to convert my data model using any of the provided RecordReaders without first converting them to some other format, such as a CSV or other file. I would like to avoid this because it would use up a lot of resources. It seems like there would be a better way to do this simple case. Is there a better approach that I am just missing?
Ethan!
First of all, Deeplearning4j uses ND4j as backend, so your data will have to eventually be converted into INDArray objects in order to be used in your model. If your trianing data is two array of doubles, inputsArray and desiredOutputsArray, you can do the following:
INDArray inputs = Nd4j.create(inputsArray, new int[]{numSamples, inputDim});
INDArray desiredOutputs = Nd4j.create(desiredOutputsArray, new int[]{numSamples, outputDim});
And then you can train your model using those vectors directly:
for (int epoch = 0; epoch < nEpochs; epoch++)
model.fit(inputs, desiredOutputs);
Alternatively you can create a DataSet object and used it for training:
DataSet ds = new DataSet(inputs, desiredOutputs);
for (int epoch = 0; epoch < nEpochs; epoch++)
model.fit(ds);
But creating a custom iterator is the safest approach, specially in larger sets since it gives you more control over your data and keep things organized.
In your DataSetIterator implementation you must pass your data and in the implementation of the next() method you should return a DataSet object comprising the next batch of your training data. It would look like this:
public class MyCustomIterator implements DataSetIterator {
private INDArray inputs, desiredOutputs;
private int itPosition = 0; // the iterator position in the set.
public MyCustomIterator(float[] inputsArray,
float[] desiredOutputsArray,
int numSamples,
int inputDim,
int outputDim) {
inputs = Nd4j.create(inputsArray, new int[]{numSamples, inputDim});
desiredOutputs = Nd4j.create(desiredOutputsArray, new int[]{numSamples, outputDim});
}
public DataSet next(int num) {
// get a view containing the next num samples and desired outs.
INDArray dsInput = inputs.get(
NDArrayIndex.interval(itPosition, itPosition + num),
NDArrayIndex.all());
INDArray dsDesired = desiredOutputs.get(
NDArrayIndex.interval(itPosition, itPosition + num),
NDArrayIndex.all());
itPosition += num;
return new DataSet(dsInput, dsDesired);
}
// implement the remaining virtual methods...
}
The NDArrayIndex methods you see above are used to access parts of a INDArray. Then now you can use it for training:
MyCustomIterator it = new MyCustomIterator(
inputs,
desiredOutputs,
numSamples,
inputDim,
outputDim);
for (int epoch = 0; epoch < nEpochs; epoch++)
model.fit(it);
This example will be particularly useful to you, since it implements a LSTM network and it has a custom iterator implementation (which can be a guide for implementing the remaining methods). Also, for more information on NDArray, this is helpful. It gives detailed information on creating, modifying and accessing parts of an NDArray.
deeplearning4j creator here.
You should not in any but all very special setting create a data set iterator. You should be using datavec. We cover this in numerous places ranging from our data vec page to our examples:
https://deeplearning4j.konduit.ai/datavec/overview
https://github.com/eclipse/deeplearning4j-examples
Datavec is our dedicated library for doing data transformations. You create custom record readers for your use case. Deeplearning4j for legacy reasons has a few "special" iterators for certain datasets. Many of those came before datavec existed. We built datavec as a way of pre processing data.
Now you use the RecordReaderDataSetIterator, SequenceRecordReaderDataSetIterator (see our javadoc for more information) and their multi dataset equivalents.
If you do this, you don't have to worry about masking, thread safety, or anything else that involves fast loading of data.
As an aside, I would love to know where you are getting the idea to create your own iterator, we now have it right in our readme not to do that. If there's another place you were looking that is not obvious, we would love to fix that.
Edit:
I've updated the links to the new pages. This post is very old now.
Please see the new links here:
https://deeplearning4j.konduit.ai/datavec/overview
https://github.com/eclipse/deeplearning4j-examples
I need to modify a file. We've already written a reasonably complex component to build sets of indexes describing where interesting things are in this file, but now I need to edit this file using that set of indexes and that's proving difficult.
Specifically, my dream API is something like this
//if you'll let me use kotlin for a second, assume we have a simple tuple class
data class IdentifiedCharacterSubsequence { val indexOfFirstChar : int, val existingContent : String }
//given these two structures
List<IdentifiedCharacterSubsequences> interestingSpotsInFile = scanFileAsPerExistingBusinessLogic(file, businessObjects);
Map<IdentifiedCharacterSubsequences, String> newContentByPreviousContentsLocation = generateNewValues(inbterestingSpotsInFile, moreBusinessObjects);
//I want something like this:
try(MutableFile mutableFile = new com.maybeGoogle.orApache.MutableFile(file)){
for(IdentifiedCharacterSubsequences seqToReplace : interestingSpotsInFile){
String newContent = newContentByPreviousContentsLocation.get(seqToReplace);
mutableFile.replace(seqToReplace.indexOfFirstChar, seqtoReplace.existingContent.length, newContent);
//very similar to StringBuilder interface
//'enqueues' data changes in memory, doesnt actually modify file until flush call...
}
mutableFile.flush();
// ...at which point a single write-pass is made.
// assumption: changes will change many small regions of text (instead of large portions of text)
// -> buffering makes sense
}
Some notes:
I cant use RandomAccessFile because my changes are not in-place (the length of newContent may be longer or shorter than that of seq.existingContent)
The files are often many megabytes big, thus simply reading the whole thing into memory and modifying it as an array is not appropriate.
Does something like this exist or am I reduced to writing my own implementation using BufferedWriters and the like? It seems like such an obvious evolution from io.Streams for a language which typically emphasizes indexed based behaviour heavily, but I cant find an existing implementation.
Lastly: I have very little domain experience with files and encoding schemes, so I have taken no effort to address the 'two-index' character described in questions like these: Java charAt used with characters that have two code units. Any help on this front is much appreciated. Is this perhaps why I'm having trouble finding an implementation like this? Because indexes in UTF-8 encoded files are so pesky and bug-prone?
I usually use HashMap<Integer, Object> to keep an array of objects where you can get items by Integer.
I'm showing you an example so you can understand.
HashMap<Integer,String>:
[0] - Hello
1 - How are you doing
[2] - Bye
So with a HashMap, I can remove items avoiding the rest moving from their indexes.
hashmap.remove(0)
[0] - null
1 - How are you doing
[2] - Bye
But HashMap shouldn't be used for indexes with an Integer. So... Which kind of array should I use to perform actions like the ones I am explaining above?
Edit: About the part of "shouldn't be used", this is what Android Eclipse is telling me:
You can use a simple Array. They can be pointed by integer. And other than calling remove you can set null to the specific place. If you really want to call remove write your own wrapper method which does that for you.
I your indices are dense (no big holes in a range [0..n], the most efficient approach would be to use a plain array of String:
final String[] lup = new String[3];
lup[0] = "Hello";
lup[1] = "How are you doing";
lup[2] = "Bye";
// to remove elements just set the index to `null`:
lup[0] = null;
You can use SparseArray which is similar to HashMap<Integer,String>
SparseArray<String> arr=new SparseArray<String>();
arr.put(0, "Hello");
If you already know the total size, then go with Arrays. But if you don't then go with ArrayList.
Now, I don't see a purpose of mapping here.
Hashmap is a Map data structure. Like a list each item that is stored in a hashmap is stored at a particular index. This index is called a hash and it is generated using a hash function. Hash functions accept the object to be stored as an argument and generate a number that is unique to it. Different hashing functions have different trade-offs. A function that is too sparse will use up more space than required(your case). while one that is not sparse enough will suffer from collisions where objects use the same hash.
Further Reading if interested: Look at Android's SparseArray implementation for inspiration.
View the source by downloading AOSP's source code here http://source.android.com/source/downloading.html.
It is highly optimized for integers!
Actually, you can use a simple array of strings.
String arr[] = new String[size];
After reading a little bit, I guess the answer is the following:
1.- I can't use plain arrays. Reason: I might not know the final size of it.
2.- Lists doesnt fit, as when you remove an item, the following items fits to the new index. I don't want index move.
So as a global answer, using HashMap is OK, however using SparseArray is recommended as it is more efficient.
I'm trying to write a Java program to decode and encode Ogg streams. I've got a decoder working but I didn't like the fact that I had duplicate code so I started writing something like that:
Decoder oggDecoder = new Decoder(
new StringDecoder( "Ogg" ),
new IntDecoder( "something" )//, ...
);
I wrote encoders and decoders for some "types" and then use them to build the whole thing.
But then I don't know how to store the result. I have 3 options I know:
- keep the data in an array of bytes and provide a get( String name ) and set( String name, Object value ) methods that will work directly on the bytes.
- use a dictionary.
- use a class and use reflection to set the properties.
I'm not that much into performance and if it's slow I don't really care as long as it's fast enough to read music. Meaning that I know writing the functions myself would make it faster but I want to write just one function working for all the properties.
So what do you think would be the fastest way of doing this?
Another way to ask this question would be:
Given a set of field names as an array of String, what is the most appropriate data structure to store the corresponding values that got decoded from a byte stream:
- keep them as byte
- store them in a dictionary
- store them in a class using reflexion
Thanks in advance for your answer.
KISS - just use a HashMap<String, byte[]>. No reflection needed.
Update
I don't think I understood at first what you want, but now I think what you are looking for is a hetergeneous map structure.
Here's a question that might be of more use to you.
I am looking for a pattern that applies to configuring placement of graphic objects.
Here's my problem:
I have some data objects with properties such as name, description, image etc.
I want to draw representations of these objects onto a canvas.
There are different "layouts" that can be applied. All the layout definitions are there at compile time.
In the layout description I want to be able to define the following for any of the object properties:
size: for text of course this doesn't mean the length/height of the whole text but of a character
position:
either absolute to the starting point of the representation
or relative to other displayed properties (e.g. thisPropertyDisplay.x = anotherPropertyDisplay.x + anotherPropertyDisplay.length)
Of course I could explicitly code the placement in n blocks/methods, one for each layout. Is there a better practise or an idiom how to implement this problem in Java?
What I actually want to do is I want to describe something like that:
property1.x = 0; property1.y = 0;
property2.x = property1.Endpoint.x; property2.y = property1.y;
property3.x = property1.Endpoint.x; property3.y = property2.Endpoint.y;
That definition is not explicitly coded in n Java methods, but there is one method able to parse all layout definition and apply them to the objects.
If the computation of the text size is beyond a useful pattern, just assume the length and height is always given.
Does that make sense? Is there a pattern / idiom name for it?
Decorator pattern seems to be the right one:
http://en.wikipedia.org/wiki/Decorator_pattern
The idea is you will have another class that will extend the behavior to handling the position and size information.
Hope it helps