Skip to content

Instantly share code, notes, and snippets.

@welterde
Created July 11, 2010 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save welterde/471943 to your computer and use it in GitHub Desktop.
Save welterde/471943 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.util.Random;
import org.uncommons.watchmaker.framework.factories.AbstractCandidateFactory;
/**
* Usage: java BaconGraphSolver <input> [time in seconds]
*/
public class BaconGraphSolver {
public static final String USAGE="Usage: java BaconGraphSolver <filename> [time in seconds]";
/**
* Simple structure to store an solution
*/
public static class Solution {
public Solution(Params params) {
this.places=new int[params.b][2];
}
public int places[][];
}
/**
* Simple structure to store the parameters of this simulation
*/
public static class Params {
public int n;
public int m;
public int b;
public boolean[][] plan;
}
public static class SolutionFactory extends AbstractCandidateFactory<Solution> {
public SolutionFactory(Params params) {
this.params=params;
}
public Solution generateRandomCandidate(Random rng) {
// create a solution
Solution ret=new Solution(this.params);
// now create the possible places
for(int i=0; i < this.params.b; i++) {
// generate some point
int[] point=this.genPoint(rng);
// store it and done ;)
ret.places[i]=point;
}
// done
return ret;
}
protected int[] genPoint(Random rng) {
int x;
int y;
while(true) {
// calculate an random position
x=rng.nextInt(this.params.n);
y=rng.nextInt(this.params.m);
// check if it is available.. retry otherwise
if(!this.params.plan[x][y])
break;
}
// works..
return new int[] {x, y};
}
private Params params;
}
public static void main(String[] args) throws Exception {
// parse arguments -- which file to parse
File input=null;
if(args.length < 1) {
System.err.println("Not enough arguments.");
System.err.println(USAGE);
System.exit(1);
} else {
input=new File(args[0]);
}
// parse arguments -- how long to run
int time=0;
if(args.length > 1) {
try {
time=Integer.parseInt(args[1]);
} catch(NumberFormatException exc) {
System.err.println("Invalid number.");
System.err.println(USAGE);
System.exit(1);
}
} else {
time=300;
}
// parse the input file
Params params=parse(input);
}
public static final Params parse(File input) throws Exception {
// create the parameter object to return
Params ret = new Params();
// create reader to read input
// ignore the FileNotFoundException - readable enough
BufferedReader reader=new BufferedReader(new FileReader(input));
// read the first line and split by space
String[] metaParts=reader.readLine().split(" ");
// create paramter object
// syntax is $nx$m $b
ret.b=Integer.parseInt(metaParts[1]);
ret.n=Integer.parseInt(metaParts[0].split("x")[0]);
ret.m=Integer.parseInt(metaParts[0].split("x")[1]);
// now create the plan
ret.plan=new boolean[ret.n][ret.m];
// parse the file
for(int i=0; i < ret.m; i++) {
String line=reader.readLine();
// char => boolean mapping:
// . => false
// P => true
// parse the line
for(int j=0; j < ret.n; j++)
ret.plan[i][j]=line.charAt(j)=='P';
}
// done
return ret;
}
}
32x32 10
.....................P..........
.......P...........P............
.........................P......
................................
................................
.P.................P............
.................P..............
................................
................................
...................P............
................................
.PP.............................
................................
................................
................................
................................
.......P..P.....................
................................
................................
................................
....P...........................
..............P....P............
..........P...........P.........
................................
................................
.......P.......................P
................P...............
................................
................................
................................
................................
................................
16x16 5
...........P....
...........P....
..............PP
................
................
................
................
................
...............P
.P..............
....P...........
................
..P.............
................
........P.......
...........P....
8x8 3
........
........
....PP..
........
...P....
........
.PP.P...
........
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment