I'm working on a tile based city builder. Currently the way the data is stored is an array of objects. Within each object, it has information like its location and other tile properties,
State.land = [
{ location: [0, 0] },
{ location: [0, 1] },
{ location: [1, 1] },
{ location: [1, 2] },
{ location: [-1, 0] },
]
The player can click on spots next to existing locations which would push a new object with the location into the land array.
Currently working on a delete method. You can only delete a land if all adjacent lands can reach [0, 0] ([0, 0] is not delete-able)
Im thinking this would be a good way to implement a nice path finding algorithm and DFS was the first that came into mind.
I've only ever implemented DFS while leetcoding but it's been awhile since I last done it. Would DFS be a good option for this problem?
Related
I'm working on Mat in OpenCV. However, I need to manually calculate the Mat by myself. Is there is a way of accessing Mat likes 2D array?
const myMat = cv.matFromArray(cv, 3, 3, cv.CV_64F, [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
const newMat = someProcessThatReturnMat(myMat)
/* OpenCV in JS cannot access Mat like this */
const result = someProcess(newMat[1][2], newMat[2][0])
Thank you in advance
Updated: The problem is cv.matFromArray cannot convert 2D array to Mat. You have to use it as 1D array. That's why it never return the correct values. For example:
const myMat = cv.matFromArray(3, 3, cv.CV_64F, [1,2,3,4,5,6,7,8,9])
And then, you can access the value
const value = myMat.doubleAt(1, 2) // row 1, col 2
You need to use the doubleAt(y,x) method.
It's double because the mat's content is CV_64F, not because you want doubles.
You can also use .data64F to access a flat Float64Array of the Mat's data.
OpenCV.js is... rough. It originated from someone's Google Summer of Code and hasn't received significant care since. Documentation amounts to some tutorials; API docs seem to be missing entirely. The "Mat" interface emulated the at() method from C++, badly, instead of looking at numpy (python) or making this access feel "native" to javascript. Overloading the [] operator is possible using a Proxy but that was not implemented.
Here's an example: https://docs.opencv.org/4.x/de/d06/tutorial_js_basic_ops.html
Feel free to browse OpenCV's issues and maybe suggest some improvements.
I'm trying to create a map based on certain arrays of three. For example,
const rule30Map = new Map();
rule30Map.set([1, 0, 0], 1);
rule30Map.set([0, 1, 1], 1);
rule30Map.set([0, 1, 0], 1);
rule30Map.set([0, 0, 1], 1);
When I try getting a value based on values in an array, the console returns undefined,
console.log(rule30Map.get([1, 0, 0])); // prints undefined
but the expected output would be 1. Can somebody explain why my logic was misunderstood?
The keys are compared with === comparison. Two separate arrays that look like [1, 0, 0] are still two separate arrays that are not equal to each other.
Working out some automated way of keeping track of key objects by their characteristics somehow would probably be more complicated than using a plain object for storage. Because JavaScript does not provide a generic way for a class of objects to supply hashing and comparison overrides means that Map and Set in JavaScript are somewhat limited in usefulness.
You could do:
const a = [1,0,0];
const map = new Map();
map.set(a.toString(), "value");
map.get([1,0,0].toString());
I assume you are computing the [1,0,0] part.
I am having hard time figuring out how to train the brain.js neural network with dynamic dataset. The GitHub documentation says the following: Each training pattern should have an input and an output, both of which can be either an array of numbers from 0 to 1 or a hash of numbers from 0 to 1
net.train([{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [0]}]);
const output = net.run([1, 0]); // [0.987]
The problem is that I don't know beforehand how many elements are in the input array of my training data so I don't know how many {input: [0, 0], output: [0]} elements I need to pass to net.train().
For example:
How do I train the neural network if I have the following arrays without hardcoding the number of {input: [0, 0], output: [0]} elements.
var input1_array = [.1, .2, .3, .4, .5]
var input2_array = [.6, .7, .8, .9, .95]
var output1_array = [.2, .6, .8, .85, .95]
// the following doesn't work
net.train([input:[input1_array, input2_array], output:[output1_array]]);
I hope question is still valid.
First, I would start with blank array (i.e/ input1_array=[];), but you can't do it as in train you need to have some value. For that reason you can put some initial data like
var input1_array = [0]; if your network will have two inputs then var input1_array = [0,0];
If you will use two or more previous values as inputs you will need
var trainingdata= [{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
] //(sorry for typos, but idea to have two entries)
Now you have some initial zeros to not have NULL going to you NN.
I was using prompt during testing and input did the following
trainingdata.push (input:[prompt("Enter next in value")],output:[prompt("Enter next out value")])
Depending on what you do with the network, you may need retrain it each time a new value comes (like time series prediction with LSTM) or just collect data (NN type).
In your example replace prompt with whatever source of data like something from page (getElementById) or some server data JSON parsed value.
I found one trick. Make function to push to array is length
You can do something like:
if (trainingData.length < maxtrainingdata) {
trainingData.push({input: [var1], output: [var2]});
}
else {
trainingData.shift();
trainingData.push({input: [var1], output: [
}
Im pretty new to AI but my idea here is, to make your dataset dynamic.
So let's say you have an AI that needs to learn how to answer to some questions correctly.
In the beginning the AI will just answer them randomly with no idea of what it is doing.
You need to make some testing environment for it to learn.
With this I mean create a piece of code in your AI that knows which answers are correct, every time it gets it right push that result to the dynamic dataset.
Let me give you another example.
If you were training some AI to escape a room.
You would need to make a piece of code when it escapes that pushes those results to the dataset so it can learn the best possible ways to do it.
Did you understand?
Obviously this operation is very expensive.
If you want to train it constantly, you would need some real powerful processing power.
The best way you can work around this issues is by saving iterations.
Imagine that every 100 results you reboot and re-train, that would help. Or if you don't need to actively train it, you can just save push the data into a file that can be re-trained every time the AI reboots.
Your AI would become smarter every time you reboot 🤩
For d3.js to render charts, it takes input as an array of objects. Here is an example of a typical input:
var data = [
{x: 0, y: 0},
{x: 1, y: 1},
{x: 2, y: 4},
{x: 3, y: 9}
];
Assume my data is in the following form:
var data = [
[x, y],
[0, 0],
[1, 1],
[2, 4],
[3, 9]
];
Then I need to convert the data from an array of arrays into an array of objects then feed it to d3.js. Now I know how to do this. This is a simple problem to solve, and I am not asking about this.
My question: Is there a built in function in d3.js that converts an array of arrays into an array of objects? Something similar to built in functions d3.csv() or d3.tsv()? Why re-invent the wheel if it is already created :)
Thanks.
Looking at the implementation in the csv/tsv parser, it looks like it reads the first row and generates a converter function to map the rest of the rows through.
This seems pretty private to the dsv implementation (rather than re-using any utility functions), and doesn't expose anything that can be used on an array rather than text. So, I'd guess the answer to your question is no.
If you're looking for a super hacky/fragile/bad way of reusing the csv functions, you could always do this*:
const result = d3.csvParse(data.map(a => a.join(',')).join('\n'))
*don't do this
Given an unknown array of integers of an unknown length, whose values are also unknown, how can I organize them into three columns, so that the sum of the left most group is the largest, the middle the second largest and the third is the smallest with the groups being as close as possible in size.
The actual goal here is to organize <ul> elements by their size (# of <li> elements they contain) into three columns. I'm looking for an answer in javascript, but if someone can explain the logic simply enough that would good enough :)
So in other words given an array such as...
var set = [1, 1, 4, 6, 7, 10, 3, 6]
Would be organized as...
var left = [10, 4]
var middle = [6, 7]
var right = [3, 6, 1, 1]
There are other possibilities. The first column sums to 14, but this could be the outcome of various combinations such as [6, 4, 3, 1]. Being organized in such a way would make it difficult to get the right values for the next column, so preferably use the largest numbers earlier on, as in my example above. *
I'm sure this has been asked and answered before but I didn't know how to look this up. I did some research and found out that this is pretty much the Partitioning Problem, although I'm still at a loss on how to do it or if there is simple one feasible answer here. Anything that works for the simple example I gave should suffice.
* EDIT: On second thought, this may be an incorrect assumption.