Related
Note: the code below now reflects a working solution to the problem, I figured out the error.
I am trying to solve the simple problem of seeing if two nodes are connected. There are many solutions available that use a stack, and I can find much DFS code that is recursive, but non that use recursion and actually search for something and return true/ false. Any help would be appreciated. Thanks!
public static boolean routeBetween(int[][] graph, int startNode, int targetNode){
//where we can keep track of the visited vertices
int numberOfVertices = graph[0].length;
boolean[] visited = new boolean[numberOfVerticies];
//set all verticies to not visited
for(int i=0; i<visited.length; i++){
visited[i] = false;
}
return dfs(graph, visited, startNode, targetNode);
}
//where the actual dfs / recursion will happen, need this to keep track of
//visited
public static boolean dfs(int[][] graph, boolean[] visited, int startNode, int targetNode){
if(startNode == targetNode){
return true;
}
boolean foundNode = false;
if(!visited[startNode]){
visited[startNode] = true;
for(int i=0; i<graph[startNode].length;i++){
if(graph[startNode][i] ==1){
boolean currentChild = dfs(graph, visited, i, targetNode);
foundNode = currentChild || foundNode;
}
}
}
return foundNode;
}
Here is some code that I was using to test the above code:
int [][] matrix = {
{0, 1, 0, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 0},
{1, 1, 0, 0, 1, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 0, 1, 0}
};
System.out.println(GraphTools.routeBetween(matrix,0,1));
System.out.println(GraphTools.routeBetween(matrix,0,2));
I know that you have already figured out your issue, but sometimes it's worthwhile to see things worked out differently.
Since you are already keeping track of all the nodes that you visit in a boolean array, much of the work you do in your dfs method turns out to be redundant.
Another way to do it is as follows:
public static boolean dfs2(int[][] graph, boolean[] visited, int startNode, int targetNode) {
// if you have not visited the current node or the target node
// then visit this node and recursively explore its unvisited
//neighbors
if (!visited[startNode] && !visited[targetNode]) {
// visit the start node
visited[startNode] = true;
for (int i = 0; i < graph[startNode].length; i++) {
if (graph[startNode][i] == 1) {
return dfs(graph, visited, i, targetNode);
}
}
}
// otherwise we just return whether or not we have visited the
// target node and continue... If we have visited the target node
//we never go into the if-statement and we always return true
return visited[targetNode];
}
Your way is perfectly fine, I just wanted to offer an alternative solution. Hope this is helpful.
Currently I have a dependency to commons-math 2.1 but I want to upgrade it to commons-math 3.6. Unfortunately there are some testcases that are not working any longer. I know what is causing my problem, but I don't know how to change the testcase accordingly to test the correct behavior as before.
I have following test code:
#Test
public void testIdentityMatrix() {
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 } };
double[] y = { 1, 2, 3, 4 };
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.setNoIntercept(true);
regression.newSampleData(y, x);
double[] b = regression.estimateRegressionParameters();
for (int i = 0; i < y.length; i++)
{
assertEquals(b[i], y[i], 0.001);
}
}
After the upgrade to commons-math 3.6 the OLSMultipleLinearRegression checks the given matrix x and vector y for valid contents. And this validation fails with the message:
not enough data (4 rows) for this many predictors (4 predictors)
What do I need to change to correct that test case?
This is a bug in Commons Math 3.x. When there is no intercept in the model, as long as the design matrix is not singular, the number of observations equal to the number of regressors should be OK. In your example, I think you mean for the third x row to be {0,0,1,0} (otherwise the design matrix is singular). With this change to your data and the code patch applied in the Hipparchus fix your test succeeds. This bug is being tracked as MATH-1392 in Commons Math.
The number of samples has to be bigger than the number of variables. Apparently your test case it not correct. You would have to add at least one more sample.
If you change
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 } };
to
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 }, {1,0,0,0} };
it should work. (although I didn't test it).
I guess the 3rd row of x should be 0010 instead of 0001?
However, if you change x to
double[][] x = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 ), { 0,
0, 0, 1 }, {1,1,1,1} };
and change y to
double[] y = { 1, 2, 3, 4, 10 };
that the last element is the sum of other elements, then it works.
I am trying to implement a machine learning algorithm (k-nn for example).As it stands my Main class, which essentially builds 8×8-pixel matrices into an array to be manipulated later. (See the data description and sample dataset.) As it stands my arrays are printing as a like so:
, Class Code: 7 DataSet:[0, 0, 3, 9, 15, 8, 0, 0, 0, 1, 15, 16, 16, 7, 0, 0, 0, 0, 5, 16, 16, 10, 4, 0, 0, 0, 3, 16, 16, 16, 9, 0, 0, 0, 0, 15, 14, 4, 0, 0, 0, 0, 0, 13, 5, 0, 0, 0, 0, 0, 1, 15, 3, 0, 0, 0, 0, 0, 4, 13, 0, 0, 0, 0, 7]
Now for my starting point I'm looking to try to implement a very basic kNN algorithm as something to build from but I am having trouble manipulating the datasets that are being outputted. I have been reading up on Foundations of Machine Learning by M. Mohri but it hasn't been of any help. My Main class for building my data:
import java.util.*;
import java.io.*;
public class Main {
static class Data {
int[] dataSet;
int classCode;
public Data(int[] dataSet, int label) {
this.dataSet = dataSet;
this.classCode = label;
}
#Override
public String toString() {
return "Class Code: " + classCode + " DataSet:" + Arrays.toString(dataSet) + "\n";
}
}
ArrayList<Data> dataSetList;
int[][] dataArray = new int[2810][65];
private void readFile(String csvFile) {
int instances = 0;
dataSetList = new ArrayList<>();
try {
Scanner scan = new Scanner(new BufferedReader(new FileReader(csvFile)));
while (scan.hasNext()) {
String line = scan.next();
String[] extractedDataFromFile = line.split(",");
for (int i = 0; i < extractedDataFromFile.length; i++) {
dataArray[instances][i] = Integer.parseInt(extractedDataFromFile[i]);
}
dataSetList.add(new Data(dataArray[instances], dataArray[instances][extractedDataFromFile.length - 1]));
instances++;
}
System.out.println(dataSetList.toString());
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
Main main = new Main();
main.readFile("dataset1.csv");
}
}
This is my first time experimenting with machine learning so any feedback or approach to this would be hugely appreciated.
EDIT//
I'm looking at a basic kNN implementation as a starting point whether someone can redirect me to material on implementing on a similar data set or an example using my current provided code. I apologize if my initial post was a little vague
I've been trying to implement an A* algorithm in a tower defense style game and after hours of trying I thought I'd ask for some insight in my logic flaws.
I've been trying to create it via http://web.mit.edu/eranki/www/tutorials/search/ & https://stackoverflow.com/a/5602061/1550619
But and infinite problem occurs when trying to generate the successors/neighbors to an AStarNode(I think) - First time implementing A* and still learning.
private ArrayList<AStarNode> aStaring(Object o, AStarNode start, AStarNode goal) {
ArrayList<AStarNode> closed = new ArrayList();
ArrayList<AStarNode> open = new ArrayList();
start.g = 0;
start.h = estimateDistance(start, goal);
start.f = 0;
open.add(start);
while(!open.isEmpty()){
AStarNode q = null;
for(AStarNode asn : open){
if(q == null || asn.f < q.f){
q = asn;
}
}
open.remove(q);
closed.add(q);
for(AStarNode succesor : q.generateSuccesors()){
if(closed.contains(succesor)){
System.out.println("Closed contained succesor");
//TODO Check if walkable
}else{
if(!open.contains(succesor)){
succesor.g = q.g+1;
succesor.h = estimateDistance(succesor, goal);
succesor.f = succesor.g + succesor.h;
succesor.parent = q;
open.add(succesor);
}else{
float nextG = q.g + succesor.cost;
if(nextG < succesor.g){
open.remove(succesor);
closed.add(succesor);
}
}
if(succesor.x == goal.x && succesor.y == goal.y){ //path found
System.out.println("hurray");
return reconstructPath(succesor);
}
}
}
}
return null;
}
public class AStarNode {
private MapDimension md = new MapDimension();
public AStarNode parent;
public int x,y;
public int f,g,h;
public int cost = 1;
public AStarNode(int x, int y){
this.x = x;
this.y = y;
}
//Looking up 4 neighbors and adding to node;
public ArrayList<AStarNode> generateSuccesors(){
ArrayList<AStarNode> neighbors = new ArrayList<>();
if(x+1 < md.getWidth()){
AStarNode temp = new AStarNode(x+1,y);
temp.parent = this;
neighbors.add(temp);
}
if(x-1 > 0){
AStarNode temp = new AStarNode(x-1,y);
temp.parent = this;
neighbors.add(temp);
}
if(y+1 < md.getHeight()){
AStarNode temp = new AStarNode(x,y+1);
temp.parent = this;
neighbors.add(temp);
}
if(y-1 > 0){
AStarNode temp = new AStarNode(x,y-1);
temp.parent = this;
neighbors.add(temp);
}
return neighbors;
}
}
Map:
public static final int[][] MAP = {
{1, 1, 1, 1, 2, 2},
{1, 1, 1, 0, 0, 2},
{2, 0, 1, 0, 0, 0},
{2, 0, 1, 0, 1, 1},
{2, 2, 1, 1, 1, 1},
{2, 2, 0, 0, 0, 1},
{2, 1, 1, 1, 1, 1},
{0, 1, 0, 0, 2, 2},
{2, 1, 0, 2, 2, 2},
{0, 1, 0, 0, 2, 2},
};
Any pointers towards the right direction would be fantastic :]
Everytime you run generateSuccessors, you create 4 (or less) NEW instances of AStarNode objects. This is not a problem per se, but AStarNode does not define hashCode and equals. So two nodes with the same coordinates are not considered equals, so closed.contains(successor) will NEVER return true.
Implement hashCode and equals on AStarNode (or get your IDE to do it for you). Something as simple as:
public int hashCode(){
return x*y;
}
public boolean equals(Object o){
if (o instanceof AStarNode){
return x==((AStarNode)o).x && y==((AStarNode)o).y;
}
return false;
}
I would like to paste a given little array into another bigger array at a specified position (or interval) in Java:
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
Does exist a simple Java method (with matlab it's simple) that helps me pasting "smaller_array" for example at position 2 (of the bigger) so that "bigger_array" variable becomes:
{ 0, 0, 1, 2, 3, 0, 0, 0, 0 };
Please don't paste methods with a simple "for". I want to know if an optimized method exists.
You can use System.arraycopy:
public static void arraycopy(
Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the specified source array, beginning at the
specified position, to the specified position of the destination
array. A subsequence of array components are copied from the source
array referenced by src to the destination array referenced by dest.
The number of components copied is equal to the length argument. The
components at positions srcPos through srcPos+length-1 in the source
array are copied into positions destPos through destPos+length-1,
respectively, of the destination array.
Note that the documentation on the System class says (emphasis added):
Among the facilities provided by the System class are standard input,
standard output, and error output streams; access to externally
defined properties and environment variables; a means of loading files
and libraries; and a utility method for quickly copying a portion of
an array.
Here's an exaple and its output:
import java.util.Arrays;
public class ArrayCopyDemo {
public static void main(String[] args) {
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
// start copying from position 1 in source, and into
// position 3 of the destination, and copy 2 elements.
int srcPos = 1, destPos = 3, length = 2;
System.arraycopy(smaller_array, srcPos, bigger_array, destPos, length );
System.out.println( Arrays.toString( bigger_array ));
}
}
[0, 0, 0, 2, 3, 0, 0, 0, 0]
To copy the entire array, just use 0 as srcPos, and src.length as length (where src is the source array; in this case, you'd use smaller_array.length).
You can use System.arraycopy(...). Reusing your example:
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
You could do:
System.arraycopy(smaller_array, 0, bigger_array, 2, 3 );
And you'll end up with your bigger_array modified and now containing the { 0, 0, 1, 2, 3, 0, 0, 0, 0 } you're after.
Use it
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
System.arraycopy(smaller_array, from_position, bigger_array, to_position, smaller_array.length);
I would go for System.arraycopy
int[] bigger_array = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] smaller_array = { 1, 2, 3 };
System.arraycopy(smaller_array, 0, bigger_array, 2, 3 );
System.out.println(Arrays.toString(bigger_array));