Created
October 10, 2011 13:55
-
-
Save shawntan/1275389 to your computer and use it in GitHub Desktop.
Calculates probability distribution for agent doing Right,Right,Right,Up,Up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.DecimalFormat; | |
public class Distribution { | |
static char map[][] = { | |
{0,0,0,0}, | |
{0,1,0,0}, | |
{0,0,0,0} | |
}; | |
//i is row | |
//j is column | |
static double[][] right(double[][] dist) { | |
double[][] dist1 = new double[dist.length][dist[0].length]; | |
for(int i=0;i<dist1.length;i++){ | |
for(int j=0;j<dist1[i].length;j++){ | |
//try to go right | |
if(j+1>=map[i].length || | |
map[i][j+1]==1) | |
dist1[i][j] += dist[i][j]*0.8; | |
else dist1[i][j+1] += dist[i][j]*0.8; | |
//try to go down | |
if(i-1<0 || map[i-1][j]==1) dist1[i][j] += dist[i][j]*0.1; | |
else dist1[i-1][j] += dist[i][j]*0.1; | |
//try to go up | |
if(i+1>=map.length || map[i+1][j]==1) dist1[i][j] += dist[i][j]*0.1; | |
else dist1[i+1][j] += dist[i][j]*0.1; | |
} | |
} | |
return dist1; | |
} | |
static double[][] up(double[][] dist) { | |
double[][] dist1 = new double[dist.length][dist[0].length]; | |
for(int i=0;i<dist1.length;i++){ | |
for(int j=0;j<dist1[i].length;j++){ | |
//try to go up | |
if(i+1>=map.length || map[i+1][j]==1) dist1[i][j] += dist[i][j]*0.8; | |
else dist1[i+1][j] += dist[i][j]*0.8; | |
//try to go left | |
if(j-1<0 || map[i][j-1]==1) dist1[i][j] += dist[i][j]*0.1; | |
else dist1[i][j-1] += dist[i][j]*0.1; | |
//try to go right | |
if(j+1>=map[i].length || map[i][j+1]==1) dist1[i][j] += dist[i][j]*0.1; | |
else dist1[i][j+1] += dist[i][j]*0.1; | |
} | |
} | |
return dist1; | |
} | |
static DecimalFormat formatter = new DecimalFormat(".000"); | |
public static void main(String[] args) { | |
double[][] dist = { | |
{1.0,0,0,0}, | |
{0 ,0,0,0}, | |
{0 ,0,0,0} | |
}; | |
dist = right(dist); | |
dist = right(dist); | |
dist = right(dist); | |
dist = up(dist); | |
dist = up(dist); | |
for(int i=0;i<dist.length;i++){ | |
for(int j=0;j<dist[i].length;j++){ | |
System.out.print(formatter.format(dist[i][j])); | |
System.out.print(' '); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment