Archive for October, 2009

Multiple inputs

Wednesday, October 28th, 2009

This week I was able to mess around in Psh enough to figure out how to return the push program with the best fitness, which was a big hump for a while. Now me and Jeremy are working on looking to how we want move forward. One large hurdle is how we can impliment categorical and ordinal variables as well as the quantitative ones we’re looking at now.

setting PshGP user definable parameters in R

Friday, October 23rd, 2009

In order to set user definable parameters in R that can be read in Java I have made the exact text that the java PshGP uses to configure the linear regression GP in R. This text is separated in a function with all the aspects of the text that are user definable (such as data input) as variables in the function. Hence users will only have to do SetParameters(…) and fill in any specifics they want to change, or let it run at default.

##Data Formatting (They would set Xdata and Ydata as their own data)
Xdata <- c(1,2,3,4);
Ydata <- c(2,4,6,8);
XYdata <- cbind(Xdata,Ydata);
DataFormatted <- cbind(Xdata,Ydata);
for (i in 1:length(Ydata))
{
FirstParenth <- c(“( “);
LastParenth <- c(” )”);
DataFormatted[i,1] <- paste(FirstParenth, DataFormatted[i,1]);
DataFormatted[i,2] <- paste(DataFormatted[i,2], LastParenth);
DataForm <- paste(DataFormatted[1:i,1], DataFormatted[1:i,2]);
};
Data <- (“(“);
for (i in 1:length(DataForm))
{
Data1 <- DataForm[i];
Data <- paste(Data,Data1);
};

## making user definable
SetParameters <- function(Pop=2000, Exec=80, Tourn=7, Mut=20, Cross=70, Gen=200, Code=40, Fair=.3,
DataIn=Data, FunctionSet=”FLOAT.* FLOAT.+ FLOAT.% FLOAT.DUP FLOAT.- FLOAT.SWAP FLOAT.ERC”) {
## Random Comments, formatting and Text preservation
TextIntro <- c(“\n#\n# A sample PushGP file for a simple test symbolic regression problem.# In this demo, the system solves the equation y = 12x^2 + 5.\n#\n# Jon Klein <jk [at] spiderland.org>\n# 06/14/2008\n#\n\n### We can optionally read in some additional parameters\ninclude standard.pushgp\n\n### Setup some standard GP parameters\n\npopulation-size =”);
TextIntro2 <- c(“\n\n### The problem class determines how test cases are setup and\n### how fitness scores are computed.  The FloatSymbolicRegression\n### class uses a set of float inputs and a single output.\n\nproblem-class\t\t=org.spiderland.Psh.FloatSymbolicRegression\n\ntest-cases\t\t= “);
TextIntro3 <- c(“)\n\n### Other possible ways of defining test cases\n\n# test-case-class \t= Problem1Generator\t# at runtime from code\n# test-case-xml\t\t= Problem1.xml\t\t# from an XML file\n# test-case-csv\t\t= Problem1.csv\t\t# from a comma separated value file\n\ninstruction-set = ( “);
TextIntro4 <- c(“))”);
TextExecLimit <- c(“\nexecution-limit         = “);
TextTournSize <- c(“\ntournament-size         = “);
TextMutPerc <- c(“\nmutation-percent        = “);
TextCrossOver <- c(“crossover-percent       = “);
TextGenerations <- c(“\nmax-generations         = “);
TextCodeSize <- c(“\nmax-random-code-size    = “);
TextFairMut <- c(“fair-mutation-range     =”);
## Evolution Parameters user determined (or not)
Population <- Pop;
ExecLimit <- Exec;
TournSize <- Tourn;
MutPerc <- Mut;
CrossOver <- Cross;
Generations <- Gen;
CodeSize <- Code;
FairMut    <- Fair;
FunctSet <- FunctionSet;
##Setting up whole regression text file
Regression1 <- paste(TextIntro, Population, TextExecLimit, ExecLimit, TextTournSize, TournSize, TextMutPerc,
MutPerc, TextCrossOver, CrossOver, TextGenerations, Generations, TextCodeSize, CodeSize, TextFairMut, FairMut,
TextIntro2, Data[1:length(Data)], TextIntro3, FunctSet, TextIntro4);
Regression1;
}

Java troubles

Wednesday, October 14th, 2009

This week Jeremy put together a function in R that can take a push function and output its result. My original plan was to have a working version of the Psh library running in R to take advantage of this but I’ve been having some serious issues on the java side of things. Essentially i’m able to load up a class into R but then when I try to run its methods the R-java interpreter is unable to run the method. I have no idea why at this point so I’ll need to bug somebody about it.

Push Library in R (Simple Float Functions)

Wednesday, October 14th, 2009

Instruction set:

PushLibrary.R contains all the Push Functions made in R so far. It also sets up Floats with NaN as the original value which will be deleted as soon as the interpreter is run on the first float in the function (see NewFloat.R).

In the future this function will be automatically constructed from a text file or other output from the java pushGP regression implementation, which was constructed prior by a data set inserted into R.

Run InterpretFunction.R to run the PushGP function on a chosen x value and return a vector of numbers (object(Floats)) with the y value as the final output.

RefreshStack.R: Very important! Every time any of the functions/operators/floats are called and affect the Floats vector all the positions of the stack may change as well as the values. This simply makes sure that the objects serving as place trackers in the vector are constantly updated when a function is called.

When function interpreter sees a float put it into the Floats vector.

Example of how to run this package in the R terminal:

> setwd(”~/Desktop/AI/R/PackageFiles”);
Error: syntax error
> setwd(“~/Desktop/AI/R/PackageFiles”);
> source(“PushLibrary.R”)
> objects()
[1] “a”             “A”             “b”             “B”
[5] “C”             “FLOATadd”      “FLOATdivide”   “FLOATdup”
[9] “FLOATmultiply” “Floats”        “FLOATsubtract” “FLOATswap”
> source(“Function.R”)
> GPfunction
function(x) c(5.0, 7.0, “FLOAT.+”, “FLOAT.*”, “FLOAT.%”, “FLOAT.SWAP”, x, 6.0, “FLOAT.+”, 1.0, “FLOAT.SWAP”)
> x <- 42
> Floats
[1] NaN
> source(“InterpretFunction.R”)
Warning messages:
1: NAs introduced by coercion
2: NAs introduced by coercion
3: NAs introduced by coercion
4: NAs introduced by coercion
5: NAs introduced by coercion
6: NAs introduced by coercion
> Floats
[1] 12  1 48

Data into PushGP Regression Model

Wednesday, October 7th, 2009

//HTML code to plug an array of data into an evolved push function. Evolved function made in the PshGP example regression1.pushgp by Jon Klein modeling data from y = 12x^2 + 5. Help with HTML java script push implementation from Paul Sawaya

<html>
<head>
<script src=”push.js”></script>
<script>
function main()
{
var x = new Array(“1.1″,”2.1″,”3.1″,”4.1”);
for (i=0;i<=3;i++)

{

// push function manually pasted from regression1.pushgp
var form = new String(“5.0 7.0 FLOAT.+ FLOAT.* FLOAT.% FLOAT.% FLOAT.* FLOAT.SWAP        FLOAT.+ ” + parseFloat(x[i]) + ” FLOAT.* 6.0 FLOAT.+ 1.0 FLOAT.- FLOAT.SWAP FLOAT.-“);
var program = pushParseString(form);
var interpreter = new pushInterpreter();
var info = pushRunProgram( interpreter, program )
document.write(interpreter.floatStack.toString());
}

}
main();
</script>
</head>
<body>
</body>
</html>

Jeremy & Dan

R Basics

Wednesday, October 7th, 2009

R basics
October 7, 2009
by Jeremy Koelmel (jpk07)

R Basics, Here is how to:
1) to manually set up data in R
2) Import data from outside R (as .txt)
3) export data out of R (as .txt)
4) To export and import R code to run in R

# writing simple exponential data in R
#assign arrays
X <- c(1:10);
Y <- c(X^2);
#matrix
XYdata <- cbind(X,Y);
#transpose data
XYdata <- t(XYdata);
#select one
XYdata[,7];
# Plot
plot(X, Y, main=”Exponential Function”);

#exporting
write.table(XYdata, file=”Desktop/XYdata.txt”, sep=”,”);
#getting the working directory
getwd();
#setting the working directory
setwd(”~/Desktop”);

#Importing
RoseData <- read.table(file=”RoseData.txt”);
#see all plots
plot(RoseData, main=”Multiflora Rose Distribution in a Pine Stand”);
#comparing two variables
MFRdensity <- RoseData[,3];
DistanceFromEdge <- RoseData[,9];
plot(DistanceFromEdge,MFRdensity);
#lm(DistanceFromEdge,MFRdensity); not working because more than one instance of y for single x
# Writing .R function: Write in a simple text format (.txt) and Save as “blahblah”.R

# ie. in this format
X <- c(1:10);
Y <- c(X^2);
XYdata <- cbind(X,Y);
plot(X, Y, main=”Exponential Function”);

# get to the right directory
setwd(”Dir”);
# enact it in R
source(”Graph.R”);

Love Jeremy

PS: Further Resources:

http://www.math.ilstu.edu/dhkim/Rstuff/Rtutor.html

http://www.cyclismo.org/tutorial/R/

rJava and psh

Wednesday, October 7th, 2009

Since last class what i’ve wanted to do is just get a simple working function in r that would take some variable in R, put it through Psh and then return the output to R. The original plan was to have minimal work done on the R side of things, R would just give some values and call the function. Then the function itself would go crazy, run the tests and save the model for future tests to be run. As of last night me and Jeremy have had all the seperate components working and we just had to put them all together into a cohesive program.

While we were working on this though all sorts of little problems came up that we needed obscure workarounds to deal with. At the core of most these issues was that all the work was being done externally from R. So I think it might be actually useful for us to go back and actually implement the core classes of Psh in R using rJava rather than obscure workarounds. This will take a little longer but it will give us more control over how everything runs and in the end should be much more useful.