program list:
WAP program to show constructor overloading using static member.
WAP to implement multilevel inheritance and method overriding.
WAP to implement interface class and show use of package.
WAP to implement multilevel exception handling and create your own exception.
WAP to implement 3 threads such that 1st sleeps for 200ms, 2nd for 400ms and 3rd for 600ms.
WAP to create applet of moving banner.
WAP to make a simple calculator.
Build a client server chat application.
JVM Mechanics: Understanding the JIT's TricksDoug Hawkins
In this talk, we'll walkthrough how the JIT optimizes a piece Java code step-by-step. In doing so, you'll learn some of the amazing feats of optimization that JVMs can perform, but also some surprisingly simple things that prevent your code from running fast.
This talk is a look into some of the surprising performance cases in Java -- with the goal of illustrating a few simple truths about the nature of compilers.
Understanding Lemon Generated Parser Finalvivekanandan r
This document describe how the Lemon generated LALR(1) parser code works step by step using the transition table.it also describe how the Shift Reduce done in the parser stack .
program list:
WAP program to show constructor overloading using static member.
WAP to implement multilevel inheritance and method overriding.
WAP to implement interface class and show use of package.
WAP to implement multilevel exception handling and create your own exception.
WAP to implement 3 threads such that 1st sleeps for 200ms, 2nd for 400ms and 3rd for 600ms.
WAP to create applet of moving banner.
WAP to make a simple calculator.
Build a client server chat application.
JVM Mechanics: Understanding the JIT's TricksDoug Hawkins
In this talk, we'll walkthrough how the JIT optimizes a piece Java code step-by-step. In doing so, you'll learn some of the amazing feats of optimization that JVMs can perform, but also some surprisingly simple things that prevent your code from running fast.
This talk is a look into some of the surprising performance cases in Java -- with the goal of illustrating a few simple truths about the nature of compilers.
Understanding Lemon Generated Parser Finalvivekanandan r
This document describe how the Lemon generated LALR(1) parser code works step by step using the transition table.it also describe how the Shift Reduce done in the parser stack .
We aren't sure about you, but working with Java 8 made one of the speakers lose all of his hair and the other lose his sleep (or was it the jetlag?). If you still haven't reached the level of Brian Goetz in mastering lambdas and strings, this talk is for you. And if you think you have, we have some bad news for you, you should attend as well.
Evolving with Java - How to remain Relevant and EffectiveNaresha K
Slides from my GIDS 2019 presentation - Evolving with Java - How to remain relevant and effective. In this presentation, I share examples of evolving with Java to overcome the pain points.
Clips basics how to make expert system in clips | facts adding | rules makin...NaumanMalik30
AOA #CS607 k is tutorials ma meny #clips programming ma ES bnana sikhaya
Facebook: https://web.facebook.com/Nauman1
.Here is my #slideshare #link for downloading slides..
Asssignments k lia facebook link per contact krain
umeed hai ki aapko ye video achi lgi.
Please Share, Support, follow , Subscribe!!! or if u Need help me?
Facebook: https://web.facebook.com/Nauman1
Linkedin : https://bit.ly/2DYFgTg
Download #Artificial_intelligence_slides https://bit.ly/2HTb3dD
Subscribe Nauman Malik channel: https://bit.ly/2t1P3Dd
Cs607 #playlist on Youtube: https://bit.ly/2DNUjQM
Instagram: https://www.instagram.com/nauman_mlik/
Google Plus: https://bit.ly/2MSJq3n
BLOGspot https://naumanai.blogspot.com/
About : Nauman Malik is actually a YouTube Channel, where you will find #University
courses videos #Artificial_intelligence #cs607 #robotic technological videos in Urdu_
Hindi, #keep in touch for your Future #needs So don’t forgot to subscribe :)
The python interpreter converts programs to bytecodes before beginning execution. Execution itself consist of looping over these bytecodes and performing specific operations over each one. This talk gives a very brief overview of the main classes of bytecodes.
This presentation was given as a lightning talk at the Boston Python Meetup group on July 24th, 2012.
We aren't sure about you, but working with Java 8 made one of the speakers lose all of his hair and the other lose his sleep (or was it the jetlag?). If you still haven't reached the level of Brian Goetz in mastering lambdas and strings, this talk is for you. And if you think you have, we have some bad news for you, you should attend as well.
Evolving with Java - How to remain Relevant and EffectiveNaresha K
Slides from my GIDS 2019 presentation - Evolving with Java - How to remain relevant and effective. In this presentation, I share examples of evolving with Java to overcome the pain points.
Clips basics how to make expert system in clips | facts adding | rules makin...NaumanMalik30
AOA #CS607 k is tutorials ma meny #clips programming ma ES bnana sikhaya
Facebook: https://web.facebook.com/Nauman1
.Here is my #slideshare #link for downloading slides..
Asssignments k lia facebook link per contact krain
umeed hai ki aapko ye video achi lgi.
Please Share, Support, follow , Subscribe!!! or if u Need help me?
Facebook: https://web.facebook.com/Nauman1
Linkedin : https://bit.ly/2DYFgTg
Download #Artificial_intelligence_slides https://bit.ly/2HTb3dD
Subscribe Nauman Malik channel: https://bit.ly/2t1P3Dd
Cs607 #playlist on Youtube: https://bit.ly/2DNUjQM
Instagram: https://www.instagram.com/nauman_mlik/
Google Plus: https://bit.ly/2MSJq3n
BLOGspot https://naumanai.blogspot.com/
About : Nauman Malik is actually a YouTube Channel, where you will find #University
courses videos #Artificial_intelligence #cs607 #robotic technological videos in Urdu_
Hindi, #keep in touch for your Future #needs So don’t forgot to subscribe :)
The python interpreter converts programs to bytecodes before beginning execution. Execution itself consist of looping over these bytecodes and performing specific operations over each one. This talk gives a very brief overview of the main classes of bytecodes.
This presentation was given as a lightning talk at the Boston Python Meetup group on July 24th, 2012.
Scala is becoming the language of choice for many development teams. This talk highlights how Scala excels in the world of multi-core processing and explores how it compares to Java 8.
Video Presentation: http://youtu.be/8vxTowBXJSg
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
JAVA OOP project; desperately need help asap im begging.
Been stuck on this for a week since my OOP skills are not good at all. Due in a couple hours so
im desperate. Codes needed will be provided at the end, thank you.
Codes:
DotChaser.java:
import java.util.*;
public class DotChaser {
public static Random rand = new Random(System.currentTimeMillis());
/**
* A "Thing" moves in a grid world. A TypeA Thing randomly
* decides to turn left or right (or not turn) every "round",
* and, afterward, takes a step forward. A TypeB Thing
* only considers making a random turn every 10th round.
*
* A STATIC CLASS? OH NO! GET IT OUT OF HERE!
*/
public static class Thing {
// dir: 0=North, 1=East, 2=South, 3=West.
// timeSinceLast: this is only important for "TypeB" Things.
public int row, col, dir, timeSinceLast;
public char lab = 'r';
public boolean isTypeB;
}
/**
* YOU'LL NEED TO PUT THIS SOMEWHERE ELSE
* HINT: WOULDN'T IT BE NICE TO HAVE A LIST OR QUEUE SO THAT
* WE DON'T HAVE TO USE NODES HERE?
* This class is for linked lists of Thing's
*/
public static class Node {
public Thing data;
public Node next;
}
// EEEEEK! STATIC METHODS!!! PLEASE FIND THEM A BETTER HOME.
public static void rightTurn(Thing t) {
t.dir = (t.dir + 1) % 4;
}
public static void leftTurn(Thing t) {
t.dir = (t.dir + 3) % 4;
}
public static void maybeTurn(Thing t) {
int i = rand.nextInt(3);
if (t.isTypeB) {
t.timeSinceLast++;
if (t.timeSinceLast == 10) {
t.timeSinceLast = 0;
if (i == 1) {
rightTurn(t);
}
if (i == 2) {
leftTurn(t);
}
}
} else {
if (i == 1) {
rightTurn(t);
}
if (i == 2) {
leftTurn(t);
}
}
}
public static void step(Thing t) {
final int[] dc = {
0, 1, 0, -1
}, dr = {
1, 0, -1, 0
};
t.row += dr[t.dir];
t.col += dc[t.dir];
}
/**
* This static method is ok :)
*/
public static void main(String[] args) {
int N = 200;
if( args.length != 0 )
N = Integer.parseInt(args[0]);
// INSTEAD OF A NODE, CREATE SOMETHING MORE USER-FRIENDLY.
Node L = null;
int count = 0;
while( true ) {
// Every N rounds, add another typeA and typeB Thing.
if( count % N == 0 ) {
// Add a typeA thing to the list.
// (GEE, THAT'S A LOT OF CODE FOR JUST CREATING ONE THING)
Thing tA = new Thing();
tA.row = 45;
tA.col = 50;
Node nA = new Node();
nA.data = tA;
nA.next = L;
L = nA;
// Add a typeB thing to the list
Thing tB = new Thing();
tB.row = 55;
tB.col = 50;
tB.lab = 'b';
tB.isTypeB = true;
Node nB = new Node();
nB.data = tB;
nB.next = L;
L = nB;
}
// Print out each thing.
// (SEEMS LIKE A NICE PRINTALL() METHOD CALL WOULD WORK HERE)
// (SEEMS LIKE A toString() METHOD IN THE CLASS WOULD ALSO BE NICE)
for( Node T = L; T != null; T = T.next )
System.out.println(T.data.row + " " + T.data.col + " " + T.data.lab);
System.out.println("done");
System.out.flush();
// Move each thing.
// (SEEMS LIKE A NICE MOVEALL() METHOD CALL WOULD WORK HERE)
for( Node T = L; T != null; T = T.next ) {
maybeTurn(T.data);
step(T.data);
}
count++;
}
}
}
Lab2_Tester.java
import org.junit.Test;
import static org.junit.Assert.assertEqual.
Empathic Programming - How to write comprehensible codeMario Gleichmann
Slides to a (non-commercial) talk i gave 2011 at XPUG Rhein/Main (Germany) about how to write comprehensible code, regarding cognitive abilities of human mind.
In this lab, you will be given a simple code for a min Heap, and you.pdfcharanjit1717
In this lab, you will be given a simple code for a min Heap, and youre going to complete some of
the methods to handle some edge cases. Tasks for today: There are mainly two tasks, and the
logic of both of them is provided in their bodies: 1) Fully implement AddItem(). 2) Fully
implement HeapUp() Use the following start-up code. The main code and a demo class are
provided as .cs files. Use them to test your code. Once you complete the above two tasks, upload
the source code for your MinHeap class to Blackboard. class BinaryHeap : IEnumerable where T
: IComparable { private T[] array; private int count; // Number of elements (nodes) public
BinaryHeap(int size) { array = new T[size]; count = 0; } public T GetItem(int index) { return
array[index]; } private void SetItem(int index, T value) { while (index >= array.Length)
Grow(array.Length * 2); array[index] = value; } private void Grow(int newsize) {
Array.Resize(ref array, newsize); } // Indices of left and right children private int
LeftChildIndex(int pos) { return 2 * pos + 1; } private int RightChildIndex(int pos) { return 2 *
pos + 2; } private int GetParentIndex(int pos) => (pos - 1) / 2; private T GetRightChild(int pos)
=> array[RightChildIndex(pos)]; private T GetLeftChild(int pos) => array[LeftChildIndex(pos)];
private T GetParent(int pos) => array[GetParentIndex(pos)]; // "Has" methods to determine if the
indices exist private bool HasLeftChild(int pos) { if (LeftChildIndex(pos) < count) return true;
else return false; } private bool HasRightChild(int pos) { if (RightChildIndex(pos) < count)
return true; else return false; } private bool IsRoot(int pos) => pos == 0; // (true if element is
root) // Swap, uh, swaps two values given two indicies. This should be private but I originally
had it public for some reason. private void Swap(int position1, int position2) { T first =
array[position1]; array[position1] = array[position2]; array[position2] = first; } public void
AddItem(T value) { // This is part of la //Insert Logic // If the tree is empty, insert at the bottom
(it does that already) // if not, insert at the end, // From the end you either need to swap to the
root, and keep minheapify (Heapdown) // or, you should probably implement move up (for lab 6,
you need to implement // for that you run a while loop, check if the current position both isn't the
root, and is higher priority than a parent (in this case that probably means it's a lower value) // if
it is, swap with parent, and keep doing that until it's its in the array[count] = value; count++;
ReCalculateUp(); } private void HeapDown() { //CompareTo //this.CompareTo(value) returns <
0 if this < value //this.CompareTo(value) returns >0 if this > value int index = 0; while
(HasLeftChild(index)) { var smallerIndex = LeftChildIndex(index); if (HasRightChild(index)
&& (GetRightChild(index).CompareTo(GetLeftChild(index))<0) ) //there's a set of ( ) around the
right expression that are redundant but hopefully easier to read { smallerI.
soft-shake.ch - Java SE 7: The Fork/Join Framework and Project Coinsoft-shake.ch
Julien Ponge
This talk introduces part of the Java SE 7 novelties. The fork/join framework aims at making parallel programming simpler, while Project Coin brings a set of subtle yet useful changes to the Java programming language.
http://soft-shake.ch/2011/conference/sessions/java/2011/09/06/java7.html
This is inspired from Tom Mitchell's book on Machine Learning. You can achieve a bit exact implementation of the back propagation algorithm if you follow the code in this.
A simple client-server application in java in which a client sends a message to a server and the server tries to be funny by sending back a funny response.
Enhancing Research Orchestration Capabilities at ORNL.pdfGlobus
Cross-facility research orchestration comes with ever-changing constraints regarding the availability and suitability of various compute and data resources. In short, a flexible data and processing fabric is needed to enable the dynamic redirection of data and compute tasks throughout the lifecycle of an experiment. In this talk, we illustrate how we easily leveraged Globus services to instrument the ACE research testbed at the Oak Ridge Leadership Computing Facility with flexible data and task orchestration capabilities.
Enterprise Resource Planning System includes various modules that reduce any business's workload. Additionally, it organizes the workflows, which drives towards enhancing productivity. Here are a detailed explanation of the ERP modules. Going through the points will help you understand how the software is changing the work dynamics.
To know more details here: https://blogs.nyggs.com/nyggs/enterprise-resource-planning-erp-system-modules/
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Shahin Sheidaei
Games are powerful teaching tools, fostering hands-on engagement and fun. But they require careful consideration to succeed. Join me to explore factors in running and selecting games, ensuring they serve as effective teaching tools. Learn to maintain focus on learning objectives while playing, and how to measure the ROI of gaming in education. Discover strategies for pitching gaming to leadership. This session offers insights, tips, and examples for coaches, team leads, and enterprise leaders seeking to teach from simple to complex concepts.
Globus Connect Server Deep Dive - GlobusWorld 2024Globus
We explore the Globus Connect Server (GCS) architecture and experiment with advanced configuration options and use cases. This content is targeted at system administrators who are familiar with GCS and currently operate—or are planning to operate—broader deployments at their institution.
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Globus
The U.S. Geological Survey (USGS) has made substantial investments in meeting evolving scientific, technical, and policy driven demands on storing, managing, and delivering data. As these demands continue to grow in complexity and scale, the USGS must continue to explore innovative solutions to improve its management, curation, sharing, delivering, and preservation approaches for large-scale research data. Supporting these needs, the USGS has partnered with the University of Chicago-Globus to research and develop advanced repository components and workflows leveraging its current investment in Globus. The primary outcome of this partnership includes the development of a prototype enterprise repository, driven by USGS Data Release requirements, through exploration and implementation of the entire suite of the Globus platform offerings, including Globus Flow, Globus Auth, Globus Transfer, and Globus Search. This presentation will provide insights into this research partnership, introduce the unique requirements and challenges being addressed and provide relevant project progress.
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteGoogle
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
👉👉 Click Here To Get More Info 👇👇
https://sumonreview.com/ai-pilot-review/
AI Pilot Review: Key Features
✅Deploy AI expert bots in Any Niche With Just A Click
✅With one keyword, generate complete funnels, websites, landing pages, and more.
✅More than 85 AI features are included in the AI pilot.
✅No setup or configuration; use your voice (like Siri) to do whatever you want.
✅You Can Use AI Pilot To Create your version of AI Pilot And Charge People For It…
✅ZERO Manual Work With AI Pilot. Never write, Design, Or Code Again.
✅ZERO Limits On Features Or Usages
✅Use Our AI-powered Traffic To Get Hundreds Of Customers
✅No Complicated Setup: Get Up And Running In 2 Minutes
✅99.99% Up-Time Guaranteed
✅30 Days Money-Back Guarantee
✅ZERO Upfront Cost
See My Other Reviews Article:
(1) TubeTrivia AI Review: https://sumonreview.com/tubetrivia-ai-review
(2) SocioWave Review: https://sumonreview.com/sociowave-review
(3) AI Partner & Profit Review: https://sumonreview.com/ai-partner-profit-review
(4) AI Ebook Suite Review: https://sumonreview.com/ai-ebook-suite-review
Understanding Globus Data Transfers with NetSageGlobus
NetSage is an open privacy-aware network measurement, analysis, and visualization service designed to help end-users visualize and reason about large data transfers. NetSage traditionally has used a combination of passive measurements, including SNMP and flow data, as well as active measurements, mainly perfSONAR, to provide longitudinal network performance data visualization. It has been deployed by dozens of networks world wide, and is supported domestically by the Engagement and Performance Operations Center (EPOC), NSF #2328479. We have recently expanded the NetSage data sources to include logs for Globus data transfers, following the same privacy-preserving approach as for Flow data. Using the logs for the Texas Advanced Computing Center (TACC) as an example, this talk will walk through several different example use cases that NetSage can answer, including: Who is using Globus to share data with my institution, and what kind of performance are they able to achieve? How many transfers has Globus supported for us? Which sites are we sharing the most data with, and how is that changing over time? How is my site using Globus to move data internally, and what kind of performance do we see for those transfers? What percentage of data transfers at my institution used Globus, and how did the overall data transfer performance compare to the Globus users?
Unleash Unlimited Potential with One-Time Purchase
BoxLang is more than just a language; it's a community. By choosing a Visionary License, you're not just investing in your success, you're actively contributing to the ongoing development and support of BoxLang.
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxrickgrimesss22
Discover the essential features to incorporate in your Winzo clone app to boost business growth, enhance user engagement, and drive revenue. Learn how to create a compelling gaming experience that stands out in the competitive market.
May Marketo Masterclass, London MUG May 22 2024.pdfAdele Miller
Can't make Adobe Summit in Vegas? No sweat because the EMEA Marketo Engage Champions are coming to London to share their Summit sessions, insights and more!
This is a MUG with a twist you don't want to miss.
Code reviews are vital for ensuring good code quality. They serve as one of our last lines of defense against bugs and subpar code reaching production.
Yet, they often turn into annoying tasks riddled with frustration, hostility, unclear feedback and lack of standards. How can we improve this crucial process?
In this session we will cover:
- The Art of Effective Code Reviews
- Streamlining the Review Process
- Elevating Reviews with Automated Tools
By the end of this presentation, you'll have the knowledge on how to organize and improve your code review proces
Check out the webinar slides to learn more about how XfilesPro transforms Salesforce document management by leveraging its world-class applications. For more details, please connect with sales@xfilespro.com
If you want to watch the on-demand webinar, please click here: https://www.xfilespro.com/webinars/salesforce-document-management-2-0-smarter-faster-better/
Listen to the keynote address and hear about the latest developments from Rachana Ananthakrishnan and Ian Foster who review the updates to the Globus Platform and Service, and the relevance of Globus to the scientific community as an automation platform to accelerate scientific discovery.
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Mind IT Systems
Healthcare providers often struggle with the complexities of chronic conditions and remote patient monitoring, as each patient requires personalized care and ongoing monitoring. Off-the-shelf solutions may not meet these diverse needs, leading to inefficiencies and gaps in care. It’s here, custom healthcare software offers a tailored solution, ensuring improved care and effectiveness.
Developing Distributed High-performance Computing Capabilities of an Open Sci...Globus
COVID-19 had an unprecedented impact on scientific collaboration. The pandemic and its broad response from the scientific community has forged new relationships among public health practitioners, mathematical modelers, and scientific computing specialists, while revealing critical gaps in exploiting advanced computing systems to support urgent decision making. Informed by our team’s work in applying high-performance computing in support of public health decision makers during the COVID-19 pandemic, we present how Globus technologies are enabling the development of an open science platform for robust epidemic analysis, with the goal of collaborative, secure, distributed, on-demand, and fast time-to-solution analyses to support public health.
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus
As part of the DOE Integrated Research Infrastructure (IRI) program, NERSC at Lawrence Berkeley National Lab and ALCF at Argonne National Lab are working closely with General Atomics on accelerating the computing requirements of the DIII-D experiment. As part of the work the team is investigating ways to speedup the time to solution for many different parts of the DIII-D workflow including how they run jobs on HPC systems. One of these routes is looking at Globus Compute as a way to replace the current method for managing tasks and we describe a brief proof of concept showing how Globus Compute could help to schedule jobs and be a tool to connect compute at different facilities.
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...informapgpstrackings
Keep tabs on your field staff effortlessly with Informap Technology Centre LLC. Real-time tracking, task assignment, and smart features for efficient management. Request a live demo today!
For more details, visit us : https://informapuae.com/field-staff-tracking/
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Java Beagle
1. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
JAVA BEAGLE
THE SOURCE CODE
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 27, 2015
2. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
OUTLINE I
1 INTRODUCTION
2 PUPPY’S MAIN
3 PUPPY
4 NODE
5 EVALUATION
6 TREE
7 STATISTICS
8 STATISTICS IO
9 CONTEXT
10 PRIMITIVES
11 FUNCTIONS
12 SUMMARY
13 REFERENCES
3. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
INTRODUCTION I
Java Beagle is an implementation of a simple genetic
programming (GP) framework.
The algorithm is implemented in Java.
The algorithm is aimed at symbolic regression problems.
With slight modification it can also be used for other types
of problems such as classification.
It is based on Beagle Puppy software for genetic
programming.
The source code has many Java files.
1 PuppyMain.java: The main entry point to the program that
runs the GP algorithmd.
2 Puppy.java: This class implements the algorithms that are
necessary to run the GP, such as crossover and mutation.
4. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
INTRODUCTION II
3 Node.java: The data-type Node specifies the
implementation of the GP genotype.
4 Evaluation.java: The routines for evaluation of a node in GP
population.
5 Tree.java: builds and Keeps the syntax tree of a GP
program.
In what follows you will find the source code of various
components of Java Beagle.
5. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN I
/∗
∗ PuppysMain . java
∗
∗ Created on August 14 , 2007, 8:15 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import GeneticProgramming . P r i m i t i v e s . ∗ ;
import GeneticProgramming . P r i m i t i v e s . Terminals . ∗ ;
import GeneticProgramming . P r i m i t i v e s . Functions . ∗ ;
import java . u t i l . ∗ ;
import java . lang . ∗ ;
import java . io . ∗ ;
public class PuppysMain {
public static float [ ] [ ] X;
public static float [ ] Target ;
6. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN II
public static Context lContext ;
public static int cacheHits , noCacheHits ;
/∗∗ Creates a new instance of PuppysMain ∗/
public PuppysMain ( ) {
}
/∗∗
∗ @param args the command l i n e arguments
∗/
public static void main ( String [ ] args ) {
/ / TODO code a p p l i c a t i o n l o g i c here
new Context ( ) ;
Context . i n s e r t (new Plus ( " plus " , 2 ) ) ;
Context . i n s e r t (new Minus ( " minus " , 2 ) ) ;
lContext . i n s e r t (new MyDivide ( " mydivide " , 2 ) ) ;
lContext . i n s e r t (new AdilPower ( " adilpower " , 2 ) ) ;
Context . i n s e r t (new Times ( " times " , 2 ) ) ;
lContext . i n s e r t (new Sin ( " sin " , 1 ) ) ; / / sin
lContext . i n s e r t (new Cos( " cos " , 1 ) ) ; / / cos
lContext . i n s e r t (new AdilLog ( " a d i l l o g " , 1 ) ) ; / / ln
lContext . i n s e r t (new AdilLog10 ( " adillog10 " , 1 ) ) ; / / log−10
/ / lContext . i n s e r t (new P r i m i t i v e ( " i f " , 3 ) ) ;
Context . i n s e r t (new X1( "X1" , 0 ) ) ;
Context . i n s e r t (new X2( "X2" , 0 ) ) ;
Context . i n s e r t (new X3( "X3" , 0 ) ) ;
Context . i n s e r t (new X4( "X4" , 0 ) ) ;
Context . i n s e r t (new X5( "X5" , 0 ) ) ;
7. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN III
Context . i n s e r t (new X6( "X6" , 0 ) ) ;
Context . i n s e r t (new X7( "X7" , 0 ) ) ;
Context . i n s e r t (new X8( "X8" , 0 ) ) ;
Context . i n s e r t (new X9( "X9" , 0 ) ) ;
Context . i n s e r t (new X10( "X10" , 0 ) ) ;
Context . i n s e r t (new X11( "X11" , 0 ) ) ;
Context . i n s e r t (new X12( "X12" , 0 ) ) ;
Context . i n s e r t (new X13( "X13" , 0 ) ) ;
Context . i n s e r t (new X14( "X14" , 0 ) ) ;
Context . i n s e r t (new X15( "X15" , 0 ) ) ;
Context . i n s e r t (new X16( "X16" , 0 ) ) ;
Context . i n s e r t (new X17( "X17" , 0 ) ) ;
Context . i n s e r t (new X18( "X18" , 0 ) ) ;
Context . i n s e r t (new X19( "X19" , 0 ) ) ;
Context . i n s e r t (new X20( "X20" , 0 ) ) ;
Context . i n s e r t (new X21( "X21" , 0 ) ) ;
Context . i n s e r t (new X22( "X22" , 0 ) ) ;
Context . i n s e r t (new X23( "X23" , 0 ) ) ;
Context . i n s e r t (new X24( "X24" , 0 ) ) ;
Context . i n s e r t (new X25( "X25" , 0 ) ) ;
Context . i n s e r t (new X26( "X26" , 0 ) ) ;
Context . i n s e r t (new X27( "X27" , 0 ) ) ;
Context . i n s e r t (new X28( "X28" , 0 ) ) ;
Context . i n s e r t (new X29( "X29" , 0 ) ) ;
Context . i n s e r t (new X30( "X30" , 0 ) ) ;
Context . i n s e r t (new X31( "X31" , 0 ) ) ;
Context . i n s e r t (new X32( "X32" , 0 ) ) ;
Context . i n s e r t (new X33( "X33" , 0 ) ) ;
8. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN IV
Context . i n s e r t (new X34( "X34" , 0 ) ) ;
Context . i n s e r t (new X35( "X35" , 0 ) ) ;
Context . i n s e r t (new X36( "X36" , 0 ) ) ;
Context . i n s e r t (new X37( "X37" , 0 ) ) ;
Context . i n s e r t (new X38( "X38" , 0 ) ) ;
Context . i n s e r t (new X39( "X39" , 0 ) ) ;
Context . i n s e r t (new X40( "X40" , 0 ) ) ;
Context . i n s e r t (new X41( "X41" , 0 ) ) ;
Context . i n s e r t (new X42( "X42" , 0 ) ) ;
Context . i n s e r t (new X43( "X43" , 0 ) ) ;
Context . i n s e r t (new X44( "X44" , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . i n s e r t (new Ephemeral ( " Ephemeral " , 0 ) ) ;
Context . popSize=2000;
Context . xoverProba =0.95F ;
Context . mutationProba =0.1F ;
Context . inInitGrowProba =0.5F ;
Context . xoverNodeDistribProb =0.5F ;
Context . maxTreeDepth=17;
Context . initTreeDepthMax =6;
9. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN V
Context . initTreeDepthMin =2;
Context . tournamentSize =7;
Context . trainDataFileX=new String ( " . / OnP563 / p563−input−params−cond−n−exp−wise . t x t " ) ;
/ / Context . trainDataFileX =" a d i l . t x t " ;
Context . trainDataFileY=new String ( " . / OnP563 / Subj−MOS−1328−cond−wise−sorted . t x t " ) ;
Context . testDataFileX=new String ( " . / OnP563 / combined−p563−features−Nortel−czech . t x t " ) ;
Context . testDataFileY=new String ( " . / OnP563 / combined−ACR−MOS−Nortel−czech . t x t " ) ;
Context . useTestData=true ;
Context . length_of_run =100;
int numRuns=2;
int numVARs=44;
/ / f o r ( i n t k=0;k <5;k ++){
/ / Context . i n s e r t (new P r i m i t i v e ( "X " . concat ( Integer . t oSt ri ng ( k +1)) , 0 ) ) ;
/ / }
ArrayList < S t a t i s t i c s > s t a t i s t i c s =new ArrayList ( ) ;
/∗
∗Lets do an example problem here
∗/
/ / Context . XData=new f l o a t [ 1 0 0 0 ] [ 2 ] ;
/ / Context . Target=new double [1000];
double f i t n e s s ;
/ / Random dice=new Random ( ) ;
/ / f o r ( i n t i =0; i <1000; i ++){
/ / Context . XData [ i ] [ 0 ] = dice . nextFloat ()∗10−5;
10. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN VI
/ / Context . XData [ i ] [ 1 ] = dice . nextFloat ()∗10−5;
/ / Context . Target [ i ]=Math . pow( Context . XData [ i ] [ 0 ] , 4 ) + Context . XData [ i ] [ 0 ] ∗ Context . XD
/ / Context . Target [ i ] = ( double ) ( Context . XData [ i ] [ 0 ] ∗ ( Context . XData [ i ] [ 0 ] ∗ ( Context . XD
/ / }
Context . readDataPatterns (1328 , 44 , true ) ;
/ / System . e x i t ( 0 ) ;
i f ( Context . useTestData ) / / i f testdata i s used as well
Context . readDataPatterns (276 , 44 , false ) ;
int lPopSize =1000;
Puppy puppy=new Puppy ( ) ;
cacheHits =0;
for ( int i =0; i <numRuns ; i ++){ / / the run s t a r t s here
S t a t i s t i c s thisRunStats=new S t a t i s t i c s ( ) ;
thisRunStats . t r a i n F i t H i s t o r y =new double [ Context . length_of_run ] ;
i f ( Context . useTestData )
thisRunStats . t e s t F i t H i s t o r y =new double [ Context . length_of_run ] ;
ArrayList <Tree> lPopulation=new ArrayList ( Context . popSize ) ;
puppy . i n i t i a l i z e P o p u l a t i o n ( lPopulation ) ;
puppy . evaluatePopulation ( lPopulation ) ;
int j =0;
f i t n e s s =500;
11. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN VII
Tree bestTree=null ;
while ( j <Context . length_of_run ) { / / run s t a r t s from here
System . out . p r i n t l n ( ) ;
bestTree=puppy . applyAllEvolutionarySteps ( lPopulation ) ;
double tmpFitness =200;
tmpFitness=lPopulation . get ( 0 ) . getTrainingFitness ( ) ;
double testFitness =Double .MAX_VALUE;
i f ( Context . useTestData ) {
testFitness =lPopulation . get ( 0 ) . evaluateTreeForTestData ( ) ;
}
thisRunStats . t r a i n F i t H i s t o r y [ j ]= tmpFitness ;
/ / thisRunStats . t e s t F i t H i s t o r y [ j ]= testFitness ;
System . out . p r i n t l n ( " CurrentBest "+tmpFitness+" TestFitness : "+ testFitness +" Generation
System . out . p r i n t l n ( " Xover Prob : "+Context . xoverProba+" Mutation Prob : "+Context . mutati
StringBuilder s t b l d r =new StringBuilder ( ) ;
lPopulation . get ( 0 ) . writeTree (0 , s t b l d r ) ;
System . out . p r i n t l n ( s t b l d r ) ;
System . gc ( ) ;
j ++;
} / / a generation ends here
bestTree . setSExpression ( ) ; / / set the s−expression now, s h a l l be used l a t t e r
thisRunStats . b e s t I n d i v i d u a l =new Tree ( bestTree ) ;
s t a t i s t i c s . add ( thisRunStats ) ;
} / / a run ends here
12. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY’S MAIN VIII
try {
S t a t i s t i c s I O . writeStatsToDisk ( s t a t i s t i c s , " SimulationResults . expr " ) ;
}
catch ( java . io . IOException e ) {
System . out . p r i n t l n ( e ) ;
}
ArrayList < S t a t i s t i c s > stato=null ;
try {
stato =( ArrayList < S t a t i s t i c s >) S t a t i s t i c s I O . readStatsFromDisk ( " SimulationResults . ex
} catch ( java . io . IOException e ) { }
catch ( java . lang . ClassNotFoundException e ) { }
System . out . p r i n t l n ( " Here are the s t a t i s t i c s " ) ;
for ( int i =0; i <stato . size ( ) ; i ++){
System . out . p r i n t l n ( stato . get ( i ) . b e s t I n d i v i d u a l . getTrainingFitness ( ) ) ;
}
}
}
13. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY I
/∗
∗ Puppy . java
∗
∗ Created on August 11 , 2007, 7:31 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;
import com. sun . org . apache . bcel . i n t e r n a l . c l a s s f i l e . JavaClass ;
import java . u t i l . ∗ ;
import java . u t i l .Random;
import java . lang . ∗ ;
public class Puppy {
private int parentPopSize ;
private Random dice ;
private ArrayList <Tree> childPop ;
14. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY II
private ArrayList <Integer > lMateVector ;
public double [ ] [ ] trainingDataX ;
public double [ ] trainingDataY ;
private double bestFitness ;
private int dynamicLevel ;
/∗∗ Creates a new instance of Puppy ∗/
public Puppy ( ) {
dice=new Random ( ) ;
childPop=new ArrayList ( ) ;
lMateVector=new ArrayList ( ) ;
this . dynamicLevel =6; / / to be changed
this . bestFitness =1000;
/ / I n i t i a l i z e the IO data here
}
/∗∗
∗ I n i t i a l i z e ramped half−and−h a l f a population of GP trees .
∗ param ioPopulation Population to i n i t i a l i z e .
∗ param inInitGrowProba P r o b a b i l i t y to use grow i n i t i a l i z a t i o n , in opposition to f u l l .
∗ param inMinDepth Minimum i n i t i a l i z a t i o n tree depth allowed .
∗ param inMaxDepth Maximum i n i t i a l i z a t i o n tree depth allowed .
∗ ingroup Puppy
∗/
public ArrayList <Tree> i n i t i a l i z e P o p u l a t i o n ( ArrayList <Tree> ioPopulation )
{
15. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY III
this . parentPopSize=Context . popSize ;
int inMinDepth=Context . initTreeDepthMin ;
int inMaxDepth=Context . initTreeDepthMax ;
float inInitGrowProba=Context . inInitGrowProba ;
assert ( inMinDepth <= inMaxDepth ) ;
/ / ArrayList <Tree> ioPopulation=new ArrayList ( ) ;
this . dice=new Random ( ) ;
for ( int i =0; i <Context . popSize ; ++ i ) {
ioPopulation . add ( i ,new Tree ( ) ) ;
ioPopulation . get ( i ) . clear ( ) ;
int l I n i t D e p t h = dice . nextInt ( inMaxDepth−inMinDepth )+ inMinDepth +1;
Tree tmpTr=new Tree ( ) ;
i f ( dice . nextFloat ( ) >= inInitGrowProba ) {
i n i t i a l i z e T r e e F u l l ( ioPopulation . get ( i ) , l I n i t D e p t h ) ;
/ / ioPopulation . add ( i , new Tree ( tmpTr ) ) ;
ioPopulation . get ( i ) . setValid ( false ) ;
ioPopulation . get ( i ) . setEvaluated ( false ) ;
/ / tmpTr . clear ( ) ;
}
else {
initializeTreeGrow ( ioPopulation . get ( i ) , inMinDepth , l I n i t D e p t h ) ;
/ / ioPopulation . add ( i , new Tree ( tmpTr ) ) ;
ioPopulation . get ( i ) . setValid ( false ) ;
ioPopulation . get ( i ) . setEvaluated ( false ) ;
/ / tmpTr . clear ( ) ;
16. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY IV
}
}
evaluatePopulation ( ioPopulation ) ;
return ioPopulation ;
}
/∗∗
∗ I n i t i a l i z e a GP tree with f u l l approach .
∗ param ioTree Tree to i n i t i a l i z e .
∗ param ioContext Evolutionary context .
∗ param inDepth Actual depth to go in i n i t i a l i z a t i o n .
∗ return Generated tree size .
∗ ingroup Puppy
∗
∗ I f the tree i s not empty , the i n i t i a l i z a t i o n append the generated sub−tree to the actual t
∗/
public int i n i t i a l i z e T r e e F u l l ( Tree ioTree , int inDepth )
{
i f ( inDepth >= 1){ / / assert was here
i f ( inDepth == 1) {
assert ( Context . mTerminalSet . size ( ) > 0 ) ;
P r i m i t i v e lTerminal = Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ;
ioTree . add (new Node( lTerminal . giveReference ( dice ) , 1 ) ) ;
return 1;
}
17. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY V
assert ( Context . mFunctionSet . size ( ) > 0 ) ;
P r i m i t i v e lFunction =Context . mFunctionSet . get ( dice . nextInt ( Context . mFunctionSet . size ( ) ) ) ;
int lNodeIndex = ioTree . size ( ) ;
ioTree . add (new Node( lFunction . giveReference ( dice ) , 0 ) ) ;
int lNbArgs = ioTree . get ( lNodeIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
int lTreeSize = 1;
/ / Tree tmpT ;
for ( int i =0; i <lNbArgs ; ++ i ) {
lTreeSize+= i n i t i a l i z e T r e e F u l l ( ioTree , inDepth −1);
/ / iTree=tmpT ;
}
ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ;
return lTreeSize ;
}
else return 0;
}
/∗∗
∗ I n i t i a l i z e a GP tree with grow approach .
∗ param ioTree Tree to i n i t i a l i z e .
∗ param ioContext Evolutionary context .
∗ param inMinDepth Minimal depth to go in i n i t i a l i z a t i o n .
∗ param inMaxDepth Maximal depth to go in i n i t i a l i z a t i o n .
∗ return Generated tree size .
∗ ingroup Puppy
∗
∗ I f the tree i s not empty , the i n i t i a l i z a t i o n append the generated sub−tree to the actual t
18. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY VI
∗/
public int initializeTreeGrow ( Tree ioTree , int inMinDepth , int inMaxDepth )
{
i f ( inMinDepth >= 1 && inMinDepth <= inMaxDepth ) {
P r i m i t i v e l P r i m i t = null ;
i f ( inMinDepth > 1) {
assert ( Context . mFunctionSet . size ( ) > 0 ) ;
l P r i m i t = Context . mFunctionSet . get ( dice . nextInt ( Context . mFunctionSet . size ( ) ) ) ;
}
else i f ( inMaxDepth == 1) {
assert ( Context . mTerminalSet . size ( ) > 0 ) ;
l P r i m i t = Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ;
}
else {
int lIndexSel = dice . nextInt ( Context . mFunctionSet . size ( ) + Context . mTerminalSet . size ( ) ) ;
i f ( lIndexSel >= Context . mFunctionSet . size ( ) ) {
l P r i m i t = Context . mTerminalSet . get ( lIndexSel − Context . mFunctionSet . size ( ) ) ;
}
else l P r i m i t = Context . mFunctionSet . get ( lIndexSel ) ;
}
/ / i n t lNodeIndex = tmpArr . size ( ) ;
int lNodeIndex = ioTree . size ( ) ;
ioTree . add (new Node( l P r i m i t . giveReference ( dice ) , 1 ) ) ;
int lTreeSize = 1;
19. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY VII
int lMinDepth = ( inMinDepth > 1) ? ( inMinDepth−1) : 1;
int lNbArgs = ioTree . get ( lNodeIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ; / / index was lNodeIndex
try {
for ( int i =0; i <lNbArgs ; i ++) {
lTreeSize+= initializeTreeGrow ( ioTree , lMinDepth , inMaxDepth −1);
/ / ioTree=tmpT ;
}
}
catch ( java . lang . StackOverflowError e ) {
System . out . p r i n t l n ( e+" in echange subtrees : "+inMinDepth+" "+inMaxDepth ) ;
System . e x i t ( 0 ) ;
}
ioTree . get ( lNodeIndex ) . setSubTreeSize ( lTreeSize ) ;
return lTreeSize ;
}
else {
System . out . p r i n t l n ( "The problem i s here " ) ;
return 0;
}
}
/∗∗
∗ Apply tournament selection to a population of trees .
∗ param ioPopulation Population to apply selection on .
∗ param ioContext Evolutionary context .
20. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY VIII
∗ param inNumberParticipants Number of p a r t i c i p a n t s to each tournament selection .
∗ ingroup Puppy
∗This function has to be modified . This only s u i t s the replacement c r i t e r i a but not an e l i t i s
∗The routine also implements LPP by Sean Luke
∗/
public ArrayList <Tree> applySelectionTournament ( ArrayList <Tree> ioPopulation )
{
i f ( ioPopulation . size ( ) == 0) return null ;
int inNumberParticipants=Context . tournamentSize ;
ArrayList <Tree> childPop=new ArrayList ( ioPopulation . size ( ) ) ;
int [ ] l I n d i c e s =new int [ ioPopulation . size ( ) ] ;
for ( int i =0; i <ioPopulation . size ( ) ; i ++)
l I n d i c e s [ i ]=0;
for ( int i =0; i <ioPopulation . size ( ) ; i ++) {
int lChoosenIndividual = dice . nextInt ( ioPopulation . size ( ) ) ;
for ( int j =1; j <inNumberParticipants ; ++ j ) {
int l T r i e d I n d i v i d u a l = dice . nextInt ( ioPopulation . size ( ) ) ; / / the f o l l o w i n g l i n e implements
i f ( ioPopulation . get ( l T r i e d I n d i v i d u a l ) . compareRank ( ioPopulation . get ( lChoosenIndividual ) )
lChoosenIndividual = l T r i e d I n d i v i d u a l ;
}
}
++ l I n d i c e s [ lChoosenIndividual ] ;
}
int lNextEmpty = 0;
int l N e x t F i l l e d = 0;
21. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY IX
while ( ( l N e x t F i l l e d < ioPopulation . size ( ) ) && ( l I n d i c e s [ l N e x t F i l l e d ] <= 1)) l N e x t F i l l e d ++;
while ( l N e x t F i l l e d < ioPopulation . size ( ) ) {
while ( l I n d i c e s [ l N e x t F i l l e d ] > 1) {
while ( l I n d i c e s [ lNextEmpty ] ! = 0) ++lNextEmpty ;
childPop . add ( ioPopulation . get ( l N e x t F i l l e d ) ) ; / / remove ( lNextEmpty ) ;
/ / ioPopulation . add ( ioPopulation . get ( l N e x t F i l l e d ) ) ;
−−l I n d i c e s [ l N e x t F i l l e d ] ;
++ l I n d i c e s [ lNextEmpty ] ;
}
while ( ( l N e x t F i l l e d < ioPopulation . size ( ) ) && ( l I n d i c e s [ l N e x t F i l l e d ] <= 1)) ++ l N e x t F i l l e d ;
}
while ( childPop . size () < ioPopulation . size ( ) )
childPop . add ( ioPopulation . get ( dice . nextInt ( ioPopulation . size ( ) ) ) ) ; / / f i l l the remaining
return childPop ;
}
/∗∗
∗ Apply tournament selection to a population of trees .
∗ param ioPopulation Population to apply selection on .
∗ param ioContext Evolutionary context .
∗ param inNumberParticipants Number of p a r t i c i p a n t s to each tournament selection .
∗ ingroup Puppy
∗This function has to be modified . This only s u i t s the replacement c r i t e r i a but not an e l i t i s
∗The routine also implements LPP by Sean Luke
∗/
public ArrayList <Tree> applySelectionTournament1 ( ArrayList <Tree> ioPopulation )
{
22. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY X
i f ( ioPopulation . size ( ) == 0) return null ;
ArrayList <Tree> childPop=new ArrayList ( ioPopulation . size ( ) ) ;
int inNumberParticipants=Context . tournamentSize ;
/ / choose the f i r s t candidate and store in the c h i l d population
while ( childPop . size () < ioPopulation . size ( ) ) {
int j j =0;
int lChosenCand1=0 , lChosenCand2=0;
int tmpCand1=ioPopulation . size ()+1 , tmpCand2=ioPopulation . size ( ) + 1 ;
while ( j j <inNumberParticipants ) {
lChosenCand1=dice . nextInt ( ioPopulation . size ( ) ) ;
i f ( tmpCand1<ioPopulation . size ( ) ) {
i f ( ioPopulation . get ( lChosenCand1 ) . compareRank ( ioPopulation . get ( tmpCand1 ) ) ) { / / | | ( ioP
tmpCand1=lChosenCand1 ; / / choose an i n d i v i d u a l ,
}
}
else
tmpCand1=lChosenCand1 ; / / choose t h i s
j j ++;
}
childPop . add (new Tree ( ioPopulation . get ( tmpCand1 ) ) ) ; / / Add t h i s to the c h i l d population
/ / choose the second candidate now and store in the c h i l d population
j j =0;
while ( j j <inNumberParticipants ) {
lChosenCand2=dice . nextInt ( ioPopulation . size ( ) ) ;
i f ( tmpCand2<=ioPopulation . size ( ) ) { / / implements Gustafson and LPP
i f ( ! ioPopulation . get ( tmpCand1 ) . compareTrees ( ioPopulation . get ( tmpCand2 ) )
&& ioPopulation . get ( lChosenCand2 ) . compareRank ( ioPopulation . get ( tmpCand2 ) )
23. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XI
| | ( ioPopulation . get ( lChosenCand2 ) . compareTrees ( ioPopulation . get ( tmpCand2 ) )
&& ioPopulation . get ( lChosenCand2 ) . size () < ioPopulation . get ( tmpCand2 ) . size ( ) ) ) {
tmpCand2=lChosenCand2 ; / / choose an i n d i v i d u a l ,
}
}
else
tmpCand2=lChosenCand2 ; / / choose t h i s
j j ++;
}
childPop . add (new Tree ( ioPopulation . get ( tmpCand2 ) ) ) ; / / Add t h i s to the c h i l d population
}
for ( int i =0; i <ioPopulation . size ( ) ; i ++){
childPop . get ( i ) . usedMutationStd=false ;
childPop . get ( i ) . usedMutationSwap=false ;
childPop . get ( i ) . usedXover=false ;
}
return childPop ;
}
/ / what i s t h i s function doing?
/∗∗
∗ Apply sub−tree crossover operation on a population of GP trees .
∗ param ioPopulation Population to apply crossover on .
∗ param ioContext Evolutionary context .
∗ param inMatingProba P r o b a b i l i t y f o r each i n d i v i d u a l to be modified by crossover .
∗ param inDistribProba P r o b a b i l i t y that a crossover exchange two sub−trees of non−terminal
24. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XII
∗ param inMaxTreeDepth Maximum tree depth allowed .
∗ ingroup Puppy
∗/
public void applyCrossover ( ArrayList <Tree> ioPopulation )
{
int inMaxTreeDepth=Context . maxTreeDepth ;
float inDistribProba=Context . inInitGrowProba ; / / not used here but s t i l l
float inMatingProba=Context . xoverProba ;
i f ( ( ioPopulation . size ( ) % 2) != 0) ioPopulation . remove ( lMateVector . size () −1);
/ / System . out . p r i n t l n ( "MATING VECTOR SIZE= "+ lMateVector . size ( ) ) ;
/ / f o r ( unsigned i n t j =0; j <lMateVector . size ( ) ; ++ j ) {
/ / std : : cout << j << " : " << ioPopulation [ lMateVector [ j ] ] << std : : endl ;
/ / }
/ / childPop . clear ( ) ; / / = new ArrayList ( ) ;
for ( int j =0; j <ioPopulation . size ( ) ; j +=2) {
try {
mateTrees ( ioPopulation . get ( j ) , ioPopulation . get ( j + 1 ) ) ;
}
catch ( java . lang . NullPointerException e ) {
System . out . p r i n t l n ( e+" in apply crossover . . . c a l l to mateTrees " ) ;
System . e x i t ( 0 ) ;
}
try {
/ / childPop . add ( trees . get ( 0 ) ) ; / / t h i s l i n e i s meant to implement Elitism , as
/ / childPop . add ( trees . get ( 1 ) ) ;
/ / ioPopulation . add (new Tree ( ioPopulation . get ( lMateVector . get ( mate ) . intValue
}
25. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XIII
catch ( java . lang . Exception e ) {
/ / System . out . p r i n t l n ( j +" "+mate+" "+"WAITING In applyCrossover "+e ) ; / / + " "+ i
/ / System . e x i t ( 0 ) ;
}
}
}
/∗∗
∗ Mate two GP trees f o r crossover .
∗ param ioTree1 F i r s t tree to mate .
∗ param ioTree2 Second tree to mate .
∗ param ioContext Evolutionary context .
∗ param inDistribProba D i s t r i b u t i o n p r o b a b i l i t y .
∗ param inMaxTreeDepth Maximum tree depth allowed .
∗ ingroup Puppy
∗/
public void mateTrees ( f i n a l Tree ioTree1 , f i n a l Tree ioTree2 )
{
/ / I n i t i a l parameters checks
assert ( ioTree1 . size ( ) > 0 ) ;
assert ( ioTree2 . size ( ) > 0 ) ;
float inDistribProba=Context . xoverNodeDistribProb ;
int inMaxTreeDepth=Context . maxTreeDepth ;
/ / ArrayList <Tree> trees= n u l l ;
/ / Crossover loop . Try the given number of attempts to mate two i n d i v i d u a l s .
26. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XIV
for ( int i =0; i <7; ++ i ) {
/ / Choose a type of node ( branch or l e a f ) f o l l o w i n g the d i s t r i b u t i o n p r o b a b i l i t y and chang
/ / node f o r another node of the same tree i f the types mismatch .
boolean lNode1IsTerminal = true ;
i f ( ioTree1 . size ( ) > 1){
i f ( dice . nextFloat ( ) >= inDistribProba ) lNode1IsTerminal =true ;
else lNode1IsTerminal = false ;
}
int lChoosenNode1=0;
try {
lChoosenNode1= dice . nextInt ( ioTree1 . size ( ) ) ; / / cant choose the root node
}
catch ( java . lang . IllegalArgumentException e ) {
System . out . p r i n t l n ( ioTree1 . size ()+ " mate tree "+e ) ;
System . e x i t ( 0 ) ;
}
while ( ( ioTree1 . get ( lChoosenNode1 ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) == 0) != lNode1IsTerminal ) {
lChoosenNode1 = dice . nextInt ( ioTree1 . size ( ) ) ;
}
i f ( lChoosenNode1==0) lChoosenNode1+=1;
boolean lNode2IsTerminal = true ;
i f ( ioTree2 . size ( ) > 1) lNode2IsTerminal = ( dice . nextFloat ( ) >= inDistribProba ) ;
int lChoosenNode2 = dice . nextInt ( ioTree2 . size ( ) ) ;
while ( ( ioTree2 . get ( lChoosenNode2 ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) == 0) != lNode2IsTerminal ) {
lChoosenNode2 = dice . nextInt ( ioTree2 . size ( ) ) ;
}
27. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XV
i f ( lChoosenNode2==0)lChoosenNode2+=1;
/ / lChoosenNode2=ioTree2 . size () −1;// t r i a l
/ / Set f i r s t stack to the node of the f i r s t tree .
/ / Check i f depth i s ok . Do a new crossover attempt i f not .
ArrayList <Integer > lStack1=ioTree1 . setStackToNode ( lChoosenNode1 ) ;
int lNewDepthTree1 =lStack1 . size ( ) + ioTree2 . getDepth ( lChoosenNode2 ) − 1;
i f ( lNewDepthTree1 > inMaxTreeDepth ) continue ;
/ / Set second stack to the node of the second tree .
/ / Check i f depth i s ok . Do a new crossover attempt i f not .
ArrayList <Integer > lStack2=ioTree2 . setStackToNode ( lChoosenNode2 ) ;
int lNewDepthTree2 =lStack2 . size ( ) + ioTree1 . getDepth ( lChoosenNode1 ) − 1;
i f ( lNewDepthTree2 > inMaxTreeDepth ) continue ;
/ / The crossover i s v a l i d .
/ / t r y {
exchangeSubTrees ( ioTree1 , lChoosenNode1 , lStack1 , ioTree2 , lChoosenNode2 , lStack2 ) ;
/ / ioTree1 . setValid ( false ) ;
/ / ioTree2 . setValid ( false ) ;
/ / trees . get ( 0 ) . setValid ( false ) ;
/ / trees . get ( 1 ) . setValid ( false ) ;
/ / }
/ / catch ( java . lang . Exception e ) {
/ / System . out . p r i n t l n ( e+"The return value i s n u l l in mate−trees : " ) ;
/ / System . e x i t ( 0 ) ;
/ / }
break ;
28. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XVI
}
/ / return trees ;
}
/∗∗
∗ Exchange two sub−trees .
∗ param ioTree1 Tree containing the f i r s t sub−tree to exchange .
∗ param inNode1 Index of root node to sub−tree to swap in f i r s t tree .
∗ param inStack1 Stack containing the parents to the f i r s t sub−tree root node .
∗ param ioTree2 Tree containing the second sub−tree to exchange .
∗ param inNode2 Index of root node to sub−tree to swap in second tree .
∗ param inStack2 Stack containing the parents to the second sub−tree root node .
∗ ingroup Puppy
∗/
public void exchangeSubTrees ( f i n a l Tree iTree1 , int inNode1 , f i n a l ArrayList <Integer > inStack1
{
/ / assert ( iTree1 != iTree2 ) ; / / I can mate mate between s i m i l a r trees as well , in the worst ca
assert ( inStack1 . size ( ) > 0 ) ;
assert ( inStack2 . size ( ) > 0 ) ;
Tree ioTree1=new Tree ( iTree1 ) ;
Tree ioTree2=new Tree ( iTree2 ) ;
int lSwapSize1 = ioTree1 . get ( inNode1 ) . getSubTreeSize ( ) ;
int lSwapSize2 = ioTree2 . get ( inNode2 ) . getSubTreeSize ( ) ;
29. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XVII
Tree ioSubTree1=new Tree ( ) ;
Tree ioSubTree2=new Tree ( ) ;
for ( int i =0; i <lSwapSize1 ; i ++){
try {
ioSubTree1 . add ( ioTree1 . remove ( inNode1 ) ) ; / / I had i here NEW
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( " exchangeSubTrees1 "+e ) ;
System . e x i t ( 0 ) ;
}
}
for ( int i =0; i <lSwapSize2 ; i ++){
try {
ioSubTree2 . add ( ioTree2 . remove ( inNode2 ) ) ; / / I had i here NEW
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( " exchangeSubTrees2 "+e ) ;
System . e x i t ( 0 ) ;
}
}
ioTree1 . addAll ( inNode1 , ioSubTree2 ) ;
ioTree2 . addAll ( inNode2 , ioSubTree1 ) ;
int l D i f f S i z e = lSwapSize1 − lSwapSize2 ;
for ( int i =0; i <( inStack1 . size () −1); ++ i )
try {
30. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XVIII
ioTree1 . get ( inStack1 . get ( i ) . intValue ( ) ) . setSubTreeSize ( ioTree1 . get ( inStack1 . get ( i ) . intValu
}
catch ( java . lang . IndexOutOfBoundsException e )
{
System . out . p r i n t l n ( " exchangeSubTrees4 "+e ) ;
System . e x i t ( 0 ) ;
}
for ( int j =0; j <( inStack2 . size () −1); ++ j )
try {
ioTree2 . get ( inStack2 . get ( j ) . intValue ( ) ) . setSubTreeSize ( ioTree2 . get ( inStack2 . get ( j ) . intValu
}
catch ( java . lang . IndexOutOfBoundsException e )
{
System . out . p r i n t l n ( " exchangeSubTrees5 "+e ) ;
System . e x i t ( 0 ) ;
}
iTree1 . clear ( ) ;
iTree1 . addAll ( ioTree1 ) ;
iTree2 . clear ( ) ;
iTree2 . addAll ( ioTree2 ) ;
iTree1 . setEvaluated ( false ) ;
iTree2 . setEvaluated ( false ) ;
iTree1 . setValid ( false ) ;
iTree2 . setValid ( false ) ;
iTree1 . usedXover=true ;
iTree2 . usedXover=true ;
}
31. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XIX
/∗∗
∗ Apply standard ( Koza ’ s ) mutation on a GP trees .
∗ This performs subtree mutation
∗ Input : − ioTree GP tree to mutate .
∗ ioContext Evolutionary context .
∗ inMaxRegenDepth Maximum tree regeneration depth allowed .
∗ inMaxDepth Maximum tree depth allowed .
∗ Output : −
∗ ioTree − Mutated GP Tree
∗ ingroup Puppy
∗/
public void mutateStandard ( Tree ioTree , int inMaxRegenDepth , int inMaxDepth )
{
/ / System . out . p r i n t l n ( " Following i s the tree before mutation : n " ) ; / / + ioTree . writeTree ( 0 ) ) ;
/ / f o r ( i n t i =0; i <ioTree . size ( ) ; i ++){
/ / System . out . p r i n t ( ioTree . get ( i ) . g e t P r i m i t i v e ( ) . getName ()+ ioTree . get ( i ) . getSubTreeSize
/ / }
i f ( ioTree . size () <=0)System . out . p r i n t l n ( "HUGE ERROR IN MUTATESTANDARD" ) ;
assert ( ioTree . size ( ) > 0 ) ;
int lMutIndex = dice . nextInt ( ioTree . size () −1);
i f ( lMutIndex ==0) lMutIndex +=1;
Tree lNewTree=new Tree ( ) ;
for ( int i =0; i <lMutIndex ; i ++){
lNewTree . add ( ioTree . get ( i ) ) ; / /NEW
}
ArrayList <Integer > lStack=ioTree . setStackToNode ( lMutIndex ) ;
32. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XX
lStack . remove ( lStack . size () −1);
int lTreeDepth = dice . nextInt ( inMaxRegenDepth )+1;
int lTreeDepth2 = inMaxDepth − lStack . size ( ) ;
i f ( lTreeDepth2 < lTreeDepth ) lTreeDepth = lTreeDepth2 ;
assert ( lTreeDepth > 0 ) ;
initializeTreeGrow ( lNewTree , 1 , lTreeDepth ) ;
for ( int i =( lMutIndex+ioTree . get ( lMutIndex ) . getSubTreeSize ( ) ) ; i <ioTree . size ( ) ; i ++){
try {
lNewTree . add ( ioTree . get ( i ) ) ; / /NEW
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" in mutateStandard "+" "+lMutIndex+" "+ioTree . get ( lMutIndex ) . getS
System . e x i t ( 0 ) ;
}
}
int l D i f f S i z e =0;
try {
l D i f f S i z e =ioTree . get ( lMutIndex ) . getSubTreeSize ( ) − lNewTree . get ( lMutIndex ) . getSubTreeS
}
catch ( java . lang . Exception e ) {
System . out . p r i n t l n ( e+" mutIndex : "+lMutIndex ) ;
/ / System . out . p r i n t l n ( ioTree . writeTree ( 0 ) ) ;
for ( int i i =0; i i <ioTree . size ( ) ; i i ++)
System . out . p r i n t ( ioTree . get ( i i ) . getSubTreeSize ()+ " " ) ;
System . e x i t ( 0 ) ;
}
for ( int i =0; i <lStack . size ( ) ; ++ i )
33. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXI
lNewTree . get ( lStack . get ( i ) ) . setSubTreeSize ( lNewTree . get ( lStack . get ( i ) ) . getSubTreeSize()−
/ / ioTree= n u l l ;
/ / ioTree=lNewTree ;
lNewTree . setValid ( false ) ;
lNewTree . setEvaluated ( false ) ;
ioTree . setValid ( false ) ;
ioTree . setEvaluated ( false ) ;
ioTree . clear ( ) ;
for ( int i =0; i <lNewTree . size ( ) ; i ++)
ioTree . add (new Node( lNewTree . get ( i ) ) ) ;
ioTree . usedMutationStd=true ;
/ / System . out . p r i n t l n ( " Following i s the tree a f t e r mutation : n " ) ; / / + ioTree . writeTree ( 0 ) ) ;
/ / f o r ( i n t i =0; i <ioTree . size ( ) ; i ++){
/ / System . out . p r i n t ( ioTree . get ( i ) . g e t P r i m i t i v e ( ) . getName ()+ ioTree . get ( i ) . getSubTreeSize
/ / }
/ / System . out . p r i n t l n ( " nMutIndex : "+ lMutIndex ) ;
/ / return lNewTree ;
}
/∗ !
∗ b r i e f Apply swap point mutation to a population of GP trees .
∗ param ioPopulation Population to mutate .
∗ param ioContext Evolutionary context .
∗ param inMutationProba Mutation p r o b a b i l i t y .
∗ param inDistribProba P r o b a b i l i t y to mutate a function node , in opposition to a terminal .
∗ ingroup Puppy
∗/
public void applyMutationSwap ( ArrayList <Tree> ioPopulation )
34. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXII
{
float inMutationProba=Context . swapMutationProba ;
for ( int i =0; i <ioPopulation . size ( ) ; ++ i ) {
i f ( dice . nextFloat ( ) < inMutationProba ) {
mutateSwap ( ioPopulation . get ( i ) ) ;
}
}
}
/∗ !
∗ b r i e f Swap mutate a GP tree .
∗ param ioTree GP tree to mutate .
∗ param ioContext Evolutionary context .
∗ param inDistribProba P r o b a b i l i t y to mutate a function node , in opposition to a terminal .
∗ ingroup Puppy
∗/
public void mutateSwap ( Tree ioTree )
{
float inDistribProba=Context . xoverNodeDistribProb ;
assert ( ioTree . size ( ) > 0 ) ;
int lMutIndex = dice . nextInt ( ioTree . size ( ) ) ;
i f ( ioTree . size ( ) > 1) {
boolean lType = ( dice . nextFloat ( ) < inDistribProba ) ;
while ( ( ioTree . get ( lMutIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) != 0) != lType ) {
lMutIndex = dice . nextInt ( ioTree . size ( ) ) ;
}
}
int lNbArgs = ioTree . get ( lMutIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
35. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXIII
i f ( lNbArgs == 0) {
assert ( Context . mTerminalSet . size ( ) > 0 ) ;
P r i m i t i v e lTerminal =
Context . mTerminalSet . get ( dice . nextInt ( Context . mTerminalSet . size ( ) ) ) ;
ioTree . get ( lMutIndex ) . s e t P r i m i t i v e ( lTerminal . clone ( ) . giveReference ( dice ) ) ;
}
else {
ArrayList <Integer > lKArgsFunction=new ArrayList ( ) ;
for ( int i =0; i <Context . mFunctionSet . size ( ) ; ++ i ) {
i f ( Context . mFunctionSet . get ( i ) . g e t A r i t y ( ) == lNbArgs ) {
lKArgsFunction . add (new Integer ( i ) ) ;
}
}
assert ( lKArgsFunction . size ( ) > 0 ) ;
P r i m i t i v e lFunction =Context . mFunctionSet . get ( lKArgsFunction . get ( dice . nextInt ( lKArgsFuncti
ioTree . get ( lMutIndex ) . s e t P r i m i t i v e ( lFunction . clone ( ) ) ;
}
ioTree . usedMutationSwap=true ;
}
/∗∗
∗ Apply standard ( Koza ’ s ) mutation to a population of GP trees .
∗ param ioPopulation Population to mutate .
∗ param ioContext Evolutionary context .
∗ param inMutationProba Mutation p r o b a b i l i t y .
∗ param inMaxRegenDepth Maximum tree regeneration depth allowed .
∗ param inMaxDepth Maximum tree depth allowed .
∗ ingroup Puppy
36. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXIV
∗/
public void applyMutationStandard ( ArrayList <Tree> ioPopulation )
{
float inMutationProba=Context . mutationProba ;
int inMaxRegenDepth=Context . initTreeDepthMax ;
int inMaxDepth=Context . maxTreeDepth ;
/ / System . out . p r i n t l n ( " Lets see how Mutation works n"+ ioPopulation . get ( t h i s . parentPopSize ) .
for ( int i =this . parentPopSize ; i <ioPopulation . size ( ) ; i ++) {
i f ( dice . nextFloat () <= inMutationProba ) {
mutateStandard ( ioPopulation . get ( i ) , inMaxRegenDepth , inMaxDepth ) ;
}
}
/ / System . out . p r i n t l n ( " Mutation worked l i k e t h i s n"+ ioPopulation . get ( t h i s . parentPopSize ) . w
/ / return ioPopulation ;
}
/∗∗
∗adapt the p r o b a b i l i t i e s of genetic operators in a dynamic fashion
∗/
public void adaptOperatorProbabilities ( List <Tree> lPopulation ) {
I t e r a t o r <Tree> popItr =lPopulation . i t e r a t o r ( ) ;
int numUsedXover=1 , numUsedMutStd=1 , numUsedMutSwap=1;
int numImpXover=0 , numImpMutStd=0 , numImpMutSwap=0;
while ( popItr . hasNext ( ) ) {
Tree tmpTree= popItr . next ( ) ;
i f ( tmpTree . usedXover ) {
numUsedXover++;
i f ( tmpTree . getTrainingFitness () <= Context . previousBest )
37. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXV
numImpXover++;
}
i f ( tmpTree . usedMutationStd ) {
numUsedMutStd++;
i f ( tmpTree . getTrainingFitness () <= Context . previousBest )
numImpMutStd++;
}
i f ( tmpTree . usedMutationSwap ) {
numUsedMutSwap++;
i f ( tmpTree . getTrainingFitness () <= Context . previousBest )
numImpMutSwap++;
}
}
Context . xoverProba =( float ) ( numImpXover / numUsedXover ) ;
Context . mutationProba =( float ) ( numImpMutStd / numUsedMutStd ) ;
Context . swapMutationProba =( float ) ( numImpMutSwap / numUsedMutSwap ) ;
i f ( Context . xoverProba <0.5F) Context . xoverProba =0.7F ;
i f ( Context . mutationProba <0.01F) Context . mutationProba =0.15F ;
i f ( Context . swapMutationProba <0.01F) Context . swapMutationProba =0.15F ;
}
/∗∗
∗Perform a l l Evolutionary steps in one go
∗1) ApplySelectionTournament
∗2) ApplyCrossover
∗3) ApplyMutationStandard
∗4) Evaluate f i t n e s s of the population
38. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXVI
∗5) Apply e l i t i s t s u r v i v a l
∗/
public Tree applyAllEvolutionarySteps ( ArrayList <Tree> lPopulation ) {
ArrayList <Tree> childPop=applySelectionTournament1 ( lPopulation ) ;
applyCrossover ( childPop ) ;
applyMutationStandard ( childPop ) ;
applyMutationSwap ( childPop ) ;
/ / Set the S−Expressions now here as t h i s w i l l save some computation
lPopulation . addAll ( childPop ) ;
evaluatePopulation ( lPopulation ) ;
adaptOperatorProbabilities ( lPopulation . subList ( Context . popSize , lPopulation . size () −1));
this . applySurvival1 ( lPopulation ) ;
for ( int i =0; i <10; i ++)
lPopulation . get ( i ) . tuneTreesCoeffs ( ) ;
Collections . sort ( lPopulation , new FitnessComparator ( ) ) ; / / sort again i f GA i s used
return lPopulation . get ( 0 ) ; / / return the best tree as i t remains the f i r s t element a f t e r so
}
/∗∗
∗Evaluate a given population
∗/
public void evaluatePopulation ( ArrayList <Tree> lPopulation ) {
I t e r a t o r <Tree> i t =lPopulation . i t e r a t o r ( ) ;
double bestFit =Double .MAX_VALUE;
while ( i t . hasNext ( ) ) {
Tree tmpTree= i t . next ( ) ;
/ / new Thread ( tmpTree ) . s t a r t ( ) ;
39. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXVII
double f i t n e s s =tmpTree . evaluateTree ( ) ;
/ / i t . next ( ) . tuneTreesCoeffs ( ) ;
i f ( fitness <Context . previousBest ) Context . previousBest= f i t n e s s ;
}
}
/∗∗
∗an e l i t i s t s u r v i v a l c r i t e r i o n
∗/
public void applySurvival1 ( ArrayList <Tree> ioPopulation ) {
Collections . sort ( ioPopulation . subList (0 , ioPopulation . size ()/2 −1) , new FitnessComparator ( ) )
Collections . sort ( ioPopulation . subList ( ioPopulation . size ( ) / 2 , ioPopulation . size () −1) , new F
/ / Collections . sort ( ioPopulation , new DepthComparator ( ) ) / / Sort wrt depth as well now −− I g
int dynDepth=ioPopulation . get ( ioPopulation . size ( ) / 2 ) . getDepth ( 0 ) ; / / of the best c h i l d
/ / i f ( dynDepth> t h i s . dynamicLevel )
i f ( dynDepth>=6 && ioPopulation . get ( ioPopulation . size ( ) / 2 ) . getTrainingFitness () < ioPopulatio
this . dynamicLevel=dynDepth ;
int children_ ;
for ( int i =ioPopulation . size ( ) / 2 ; i <ioPopulation . size ( ) ; i ++){ / / remove from among childPop
Tree tmpTree=ioPopulation . get ( i ) ;
i f ( tmpTree . getDepth (0) > this . dynamicLevel ) { / / dynamic tree depth
ioPopulation . remove ( i ) ;
i −−;
}
}
while ( ioPopulation . subList ( this . parentPopSize , ioPopulation . size ( ) ) . size () > this . parentPopS
ioPopulation . remove ( ioPopulation . size () −1); / / remove the extra children here
int i =this . parentPopSize −1;
40. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXVIII
while ( ioPopulation . size () > this . parentPopSize )
ioPopulation . remove ( i −−); / / remove the less f i t parents now
Collections . sort ( ioPopulation , new FitnessComparator ( ) ) ;
Context . previousBest=ioPopulation . get ( 0 ) . getTrainingFitness ( ) ;
System . out . p r i n t l n ( this . dynamicLevel ) ;
}
}
/∗∗
∗Fitness Comparator
∗/
class FitnessComparator implements java . u t i l . Comparator<Tree >{
public int compare ( Tree o1 , Tree o2 ) {
double f i t 1 =o1 . getTrainingFitness ( ) ;
double f i t 2 =o2 . getTrainingFitness ( ) ;
i f ( f i t 1 < f i t 2 ) return −1;
i f ( f i t 1 == f i t 2 ) return 0;
return 1;
}
}
/∗∗
41. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PUPPY XXIX
∗Size ( Tree Depth ) Comparator
∗/
class DepthComparator implements java . u t i l . Comparator<Tree >{
public int compare ( Tree o1 , Tree o2 ) {
int depth1=o1 . getDepth ( 0 ) ;
int depth2=o2 . getDepth ( 0 ) ;
i f ( depth1<depth2 ) return −1;
i f ( depth1==depth2 ) return 0;
return 1;
}
}
42. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
NODE I
/∗
∗ Node . java
∗
∗ Created on August 11 , 2007, 11:44 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming ;
import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;
import java . io . ∗ ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
public class Node implements S e r i a l i z a b l e {
private P r i m i t i v e mPrimitive ; / / ! < Smart pointer to the associated p r i m i t i v e .
private int mSubTreeSize ; / / ! < Sub−tree size , including actual node .
/∗∗ Creates a new instance of Node ∗/
public Node( P r i m i t i v e p r i m i t i v e , int size ) {
this . mPrimitive= p r i m i t i v e . clone ( ) ;
this . mSubTreeSize=size ;
}
43. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
NODE II
/∗∗
∗Copy Constructor
∗/
public Node(Node copy ) {
/ / t h i s . mPrimitive=new P r i m i t i v e ( copy . mPrimitive ) ;
this . mPrimitive=copy . mPrimitive . clone ( ) ;
this . mSubTreeSize=copy . mSubTreeSize ;
}
/∗∗
∗Set the p r i m i t i v e
∗/
public void s e t P r i m i t i v e ( f i n a l P r i m i t i v e prim ) {
/ / t h i s . mPrimitive=new P r i m i t i v e ( prim ) ;
this . mPrimitive=prim . clone ( ) ;
}
/∗∗
∗Return the p r i m i t i v e
∗/
public f i n a l P r i m i t i v e g e t P r i m i t i v e ( ) {
return this . mPrimitive ;
}
/∗∗
∗void subtree size
∗/
public void setSubTreeSize ( f i n a l int treeSize ) {
44. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
NODE III
this . mSubTreeSize=treeSize ;
}
/∗∗
∗Return the subtree size
∗/
public f i n a l int getSubTreeSize ( ) {
return this . mSubTreeSize ;
}
}
45. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
EVALUATION I
/∗
∗ Evaluation . java
∗
∗ Created on September 6 , 2007, 9:18 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗Keeps the Evaluation of a subtee l y i n g in a cache
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
public class Evaluation {
public double [ ] evaluation ;
public boolean cachedFlag ;
/∗∗
∗The default constructor f o r Terminal nodes
∗/
public Evaluation ( ) {
this . cachedFlag=true ; / / Vals f o r Ts always get true
this . evaluation=new double [ 1 ] ;
46. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
EVALUATION II
}
/∗∗
∗The contructor f o r NT subtrees
∗/
public Evaluation ( double [ ] eval , boolean cFlag ) {
this . evaluation=new double [ eval . length ] ;
for ( int i =0; i <eval . length ; i ++){
this . evaluation [ i ]= eval [ i ] ;
this . cachedFlag=cFlag ; / / Defines Whether an entry i s e f f e c t i v e l y cached
}
}
}
47. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE I
/∗
∗ Tree . java
∗
∗ Created on August 10 , 2007, 10:36 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;
import java . u t i l . ∗ ;
import java . lang . ∗ ;
import EvoNursery . ∗ ;
import java . io . ∗ ;
public class Tree extends ArrayList <Node> implements S e r i a l i z a b l e {
private double fitness , testFitness ;
private boolean v a l i d ;
private String s_expression ;
private boolean evaluated ; / / t e l l s i f the f i t n e s s of t h i s tree has been evaluated
private double slope ;
48. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE II
private double i n t e r c e p t ;
private double [ ] t r a i n i n g R e s u l t ; / / This contains the r e s u l t of the i n d i v i d u a l over whole
public static double [ ] forConstants ; / / These two variables s h a l l be used by the p r i m i t i v e s
public boolean usedXover , usedMutationStd , usedMutationSwap ;
/∗∗ Creates a new instance of Tree ∗/
public Tree ( ) {
this . f i t n e s s =Double .MAX_VALUE; / / assign a f i t n e s s value
this . v a l i d =false ;
this . evaluated=false ;
}
/∗∗
∗ Construct a new tree , with given f i t n e s s and v a l i d i t y f l a g .
∗/
public Tree ( double sFitness , boolean i V a l i d ) {
this . f i t n e s s =sFitness ;
this . v a l i d = i V a l i d ;
this . s_expression=new String ( ) ;
this . evaluated=false ; / / that i t has not been evaluated by default
this . slope =1;
this . i n t e r c e p t =0;
}
/∗∗
∗ copy Constructor
∗/
49. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE III
public Tree ( f i n a l Tree copy ) {
this . f i t n e s s =copy . f i t n e s s ;
this . testFitness =copy . testFitness ;
this . v a l i d =copy . v a l i d ;
/ / t h i s . s_expression=copy . writeTree ( 0 ) ;
this . evaluated=copy . evaluated ;
for ( int i =0; i <copy . size ( ) ; i ++)
this . add (new Node( copy . get ( i ) ) ) ;
this . slope=copy . slope ;
this . i n t e r c e p t =copy . i n t e r c e p t ;
}
/∗∗
∗ b r i e f Return tree depth at given index .
∗ param inIndex Index of sub−tree root to get the depth from .
∗ return Sub−tree depth .
∗/
public f i n a l int getDepth ( int inIndex )
{
i f ( inIndex < this . size ( ) ) { / / I had assert here
int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
int lDepth = 1;
int j = inIndex + 1;
for ( int i =0; i <lNbArgs ; ++ i ) {
int lChildDepth = getDepth ( j ) + 1;
i f ( lChildDepth > lDepth ) lDepth = lChildDepth ;
j += this . get ( j ) . getSubTreeSize ( ) ;
}
50. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE IV
return lDepth ;
}
else {
return 0;
}
}
/∗∗
∗ Compare e qu al it y of two trees .
∗ Two trees are condired equal i f t h e i r f i t n e s s d i f f e r s by less than 0.01.
∗ For bucketting reasons
∗ return : True i s trees are equals , false i f not .
∗/
public f i n a l boolean compareTrees ( f i n a l Tree aTree )
{
i f ( this==aTree ) return true ;
return ( this . v a l i d && aTree . v a l i d && ( java . lang . Math . abs ( this . f i t n e s s − aTree . f i t n e s s ) <0.01
}
/∗∗
∗ Compare ranking of two trees .
∗ return True i s actual tree i s less than a tree given as argument , false i f not .
∗/
public f i n a l boolean compareRank ( Tree aTree )
{
return ( this . v a l i d && aTree . v a l i d && ( this . f i t n e s s < aTree . f i t n e s s ) ) ;
}
51. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE V
/∗
∗ I n t e r p r e t the GP tree .
∗ param outResult Datum containing the r e s u l t of the i n t e r p r e t a t i o n .
∗ param ioContext Evolutionary context .
∗
void Puppy : : Tree : : i n t e r p r e t ( void∗ outResult , Puppy : : Context& ioContext )
{
assert ( size ( ) > 0 ) ;
ioContext . mTree = t h i s ;
ioContext . mCallStack . push_back ( 0 ) ;
f r o n t ( ) . mPrimitive−>execute ( outResult , ioContext ) ;
ioContext . mCallStack . pop_back ( ) ;
} ∗/
/∗∗
∗ Set c a l l stack to include the c o r r e c t l y r e f e r to a given node .
∗ param inIndex Node index to which c a l l stack must be set .
∗ param outCallStack Result of c a l l stack s e t t i n g .
∗/
public f i n a l java . u t i l . ArrayList <Integer > setStackToNode ( int inIndex )
{
assert ( inIndex < size ( ) ) ;
ArrayList <Integer > callStack=new ArrayList ( ) ;
int i = 0;
callStack . add (new Integer ( i ) ) ;
while ( i < inIndex ) {
52. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE VI
int lNbArgs=this . get ( i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
int lChildIndex = i + 1;
for ( int j =0; j <lNbArgs ; ++ j ) {
i f ( ( lChildIndex+this . get ( lChildIndex ) . getSubTreeSize ( ) ) > inIndex ) break ;
lChildIndex += this . get ( lChildIndex ) . getSubTreeSize ( ) ;
}
assert ( lChildIndex < this . size ( ) ) ;
i = lChildIndex ;
callStack . add (new Integer ( i ) ) ;
}
assert ( i == inIndex ) ;
return callStack ;
}
/∗∗
∗Returns the slope parameter found by l i n e a r scaling
∗/
public f i n a l double getSlope ( ) {
return this . slope ;
}
/∗∗
∗Returns the i n t e r c e p t parameter found by l i n e a r scaling
∗/
public f i n a l double getIntercept ( ) {
return this . i n t e r c e p t ;
}
53. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE VII
/∗∗
∗ b r i e f Write GP tree at given index as a s−expression i n t o a Java String object .
∗ param ioOS C++ output stream to write tree i n t o .
∗ param inIndex Actual node index in tree .
∗/
public void writeTree ( int inIndex , StringBuilder expression )
{
i f ( inIndex >= this . size ( ) ) return ;
/ / expression . append ( " " ) ;
int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
expression . append ( this . get ( inIndex ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;
i f ( lNbArgs > 0) expression . append ( " ( " ) ;
int j = inIndex + 1;
for ( int i =0; i <lNbArgs ; ++ i ) {
writeTree ( j , expression ) ;
i f ( i <lNbArgs−1)
expression . append ( " , " ) ;
try {
j = j +this . get ( j ) . getSubTreeSize ( ) ;
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" In write tree " ) ;
/ / System . e x i t ( 0 ) ;
}
}
i f ( lNbArgs > 0) expression . append ( " ) " ) ;
/ / return expression . t r t r i m ( ) ; / / removes the " " added in the beginning .
54. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE VIII
}
/∗∗
∗ b r i e f Write GP tree at given index as a s−expression i n t o a Java String object .
∗ param ioOS C++ output stream to write tree i n t o .
∗ param inIndex Actual node index in tree .
∗/
public void writeTree2 ( int inIndex , StringBuilder expression )
{
i f ( inIndex >= this . size ( ) ) return ;
/ / expression . append ( " " ) ;
int lNbArgs = this . get ( inIndex ) . g e t P r i m i t i v e ( ) . g e t A r i t y ( ) ;
expression . append ( this . get ( inIndex ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;
/ / i f ( lNbArgs > 0) expression . append ( " ( " ) ;
int j = inIndex + 1;
for ( int i =0; i <lNbArgs ; ++ i ) {
writeTree ( j , expression ) ;
/ / i f ( i <lNbArgs−1)
/ / expression . append ( " , " ) ;
try {
j = j +this . get ( j ) . getSubTreeSize ( ) ;
}
catch ( java . lang . IndexOutOfBoundsException e ) {
System . out . p r i n t l n ( e+" In write tree " ) ;
/ / System . e x i t ( 0 ) ;
}
}
55. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE IX
/ / i f ( lNbArgs > 0) expression . append ( " ) " ) ;
/ / return expression . t r t r i m ( ) ; / / removes the " " added in the beginning .
}
/∗∗
∗sets the s−expression
∗/
public void setSExpression ( ) {
StringBuilder expr=new StringBuilder ( this . size ( ) ) ;
this . writeTree (0 , expr ) ;
this . s_expression=new String ( expr . t oS tr in g ( ) ) ;
}
/∗∗
∗Gets the s−epression
∗/
public f i n a l String getSExpression ( ) {
return this . s_expression ;
}
/∗∗
∗Set the v a l i d f l a g to something
∗/
public void setValid ( f i n a l boolean inValid ) {
this . v a l i d = inValid ;
}
56. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE X
/∗∗
∗Get the v a l i d i t y status of t h i s tree
∗/
public f i n a l boolean getValid ( ) {
return this . v a l i d ;
}
/∗
∗Sets the p r i m i t i v e to new value
∗/
public void s e t P r i m i t i v e ( f i n a l P r i m i t i v e prim ) {
this . s e t P r i m i t i v e ( prim ) ;
}
/∗∗
∗Set the evaluated f l a g of t h i s tree
∗/
public void setEvaluated ( f i n a l boolean eval ) {
this . evaluated=eval ;
}
/∗∗
∗Get the evaluation f l a g of t h i s tree
∗/
public f i n a l boolean getEvaluated ( ) {
return this . evaluated ;
}
57. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XI
/∗∗
∗Set the f i t n e s s of t h i s Tree
∗/
public void setTrainingFitness ( f i n a l double f i t ) {
this . f i t n e s s = f i t ;
}
/∗∗
∗Return the t r a i n i n g f i t n e s s of t h i s Tree
∗/
public f i n a l double getTrainingFitness ( ) {
return this . f i t n e s s ;
}
/∗∗
∗Return the f i t n e s s of Testing data
∗/
public f i n a l double getTestingFitness ( ) {
return this . testFitness ;
}
/ / public void run ( ) {
/ / t h i s . evaluateTree ( ) ;
/ / }
/∗∗
∗ Evaluate f i t n e s s of a Tree
∗
∗ inX Independant sample values f o r evaluation .
58. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XII
∗ inF Dependant sample values f o r evaluation .
∗ return Number of f i t n e s s evaluated .
∗ ingroup SymbReg
∗/
public double evaluateTree ( )
{
assert ( Context . XData . length == Context . Target . length ) ;
i f ( this . evaluated )
return this . f i t n e s s ;
double [ ] target =Context . Target ;
double [ ] evolved=this . interpretTree ( Context . XData ) ;
i f ( evolved . length != Context . Target . length ) {
double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constant
evolved=new double [ Context . Target . length ] ;
for ( int z=0;z<Context . Target . length ; z++)
evolved [ z ]= tmpEvolved ;
}
double [ ] slope_n_intercept=new double [ 2 ] ;
linearScaling ( slope_n_intercept , evolved , Context . Target ) ;
/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . length ] ;
double tmpFitness =0;
double res ;
for ( int i =0; i <evolved . length ; i ++){
res=Context . Target [ i ]−( slope_n_intercept [0]∗ evolved [ i ]+ slope_n_intercept [ 1 ] ) ;
tmpFitness+=res∗res ;
59. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XIII
}
tmpFitness=tmpFitness / evolved . length ;
/ / i f ( tmpFitness < t h i s . f i t n e s s ) {
this . f i t n e s s =tmpFitness ;
this . slope=slope_n_intercept [ 0 ] ;
this . i n t e r c e p t =slope_n_intercept [ 1 ] ;
/ / }
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
this . f i t n e s s =Double .MAX_VALUE;
/ / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ;
this . v a l i d = true ;
this . evaluated=true ;
return this . f i t n e s s ;
}
/∗∗
∗ Evaluate f i t n e s s of a Tree
∗
∗ inX Independant sample values f o r evaluation .
∗ inF Dependant sample values f o r evaluation .
∗ return Number of f i t n e s s evaluated .
∗ ingroup SymbReg
∗/
public double evaluateTreeForTestData ( )
{
assert ( Context . TestXData . length == Context . TestTarget . length ) ;
60. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XIV
double [ ] target =Context . TestTarget ;
double [ ] evolved=this . interpretTree ( Context . TestXData ) ;
i f ( evolved . length != Context . TestTarget . length ) {
double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constant
evolved=new double [ Context . TestTarget . length ] ;
for ( int z=0;z<Context . TestTarget . length ; z++)
evolved [ z ]= tmpEvolved ;
}
/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . length ] ;
double tmpFitness =0;
double res ;
for ( int i =0; i <evolved . length ; i ++){
res=Context . TestTarget [ i ]−( this . slope∗evolved [ i ]+ this . i n t e r c e p t ) ;
tmpFitness+=res∗res ;
}
tmpFitness=tmpFitness / evolved . length ;
/ / i f ( tmpFitness < t h i s . f i t n e s s ) {
this . testFitness =tmpFitness ;
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
this . testFitness =Double .MAX_VALUE;
/ / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ;
this . v a l i d = true ;
this . evaluated=true ;
return this . testFitness ;
}
61. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XV
/∗∗
∗LinearScaling , by Maarten Keijzer , i s implemented here
∗/
public void linearScaling ( double [ ] slope_n_intercept , double [ ] evolved , double [ ] target ) {
assert ( evolved . length== target . length ) ;
double meanTarget=0 , meanEvolved=0;
for ( int i =0; i <evolved . length ; i ++){
meanEvolved+=evolved [ i ] ;
meanTarget+= target [ i ] ;
}
meanEvolved /= evolved . length ;
meanTarget /= target . length ;
double numerator =0 , denominator =0;
for ( int i =0; i <evolved . length ; i ++){
numerator +=( target [ i ]−meanTarget )∗( evolved [ i ]−meanEvolved ) ;
denominator+=java . lang . Math . pow( evolved [ i ]−meanEvolved , 2 ) ;
}
/ / numerator=numerator / target . length ;
/ / denominator /= evolved . length ;
double slope1 =1;
i f ( denominator !=0) slope1=numerator / denominator ;
else i f ( meanEvolved==0) slope1 =1;
else slope1=meanTarget / meanEvolved ;
slope_n_intercept [0]= slope1 ;
slope_n_intercept [1]= meanTarget−slope∗meanEvolved ;
62. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XVI
}
/∗∗
∗ I n t e r p r e t the GP tree .
∗ outResult Datum containing the r e s u l t of the i n t e r p r e t a t i o n .
∗ XData to be evaluated
∗/
public double [ ] interpretTree ( float [ ] [ ] xdata )
{
assert ( this . size ( ) > 0 ) ;
ArrayList <Integer > callStack=new ArrayList ( ) ;
callStack . add (new Integer ( 0 ) ) ;
double [ ] outResult=this . get ( 0 ) . g e t P r i m i t i v e ( ) . execute ( callStack , this , xdata ) ;
callStack . remove ( callStack . size () −1);
return outResult ;
}
/∗∗
∗ tune the terminals ( constants ) of t h i s tree
∗
∗ inX Independant sample values f o r evaluation .
∗ inF Dependant sample values f o r evaluation .
∗ return Number of f i t n e s s evaluated .
∗ ingroup SymbReg
∗/
public double tuneTreesCoeffs ( )
{
assert ( Context . XData . length == Context . Target . length ) ;
/ / i f ( t h i s . evaluated ) return t h i s . f i t n e s s ;
63. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XVII
I t e r a t o r <Node> nodItr =this . i t e r a t o r ( ) ;
int i i =0;
while ( nodItr . hasNext ( ) ) {
Node tmpNode= nodItr . next ( ) ;
i f ( tmpNode . g e t P r i m i t i v e ( ) . g e t A r i t y ()==0
&& ! tmpNode . g e t P r i m i t i v e ( ) . getName ( ) . startsWith ( "X" ) )
i i ++; / / f i n d the number of constants
}
i f ( i i <=1)
return this . evaluateTree ( ) ; / / i . e . no c o e f f i c i e n t s to be tuned by GA so evaluate the ind
int [ ] constIndex=new int [ i i ] ; / / keeps the indices of constants in t h i s tree
double [ ] constants=new double [ i i ] ; / / t h i s array would keep the constants f o r the interim ( a t
int j j =0;
for ( i i =0; i i <this . size ( ) ; i i ++){
i f ( this . get ( i i ) . g e t P r i m i t i v e ( ) . g e t A r i t y ()==0
&& ! this . get ( i i ) . g e t P r i m i t i v e ( ) . getName ( ) . startsWith ( "X" ) ) {
constIndex [ j j ]= i i ; / / t h i s loop copies the indices to the array
constants [ j j ]= Double . valueOf ( this . get ( i i ) . g e t P r i m i t i v e ( ) . getName ( ) ) ;
j j ++;
}
}
/ / I n i t i a l i z e the GA here
Nursery nurse=new Nursery ( ) ;
ArrayList <Genotype> pop=nurse . i n i t i a l i z e P o p u l a t i o n (100 , constIndex . length , −6, +6); / / should
for ( int i =0; i <50; i ++){ / / numGens i s chosen to be 150
ArrayList <Genotype> childPop=nurse . applySelectionTournament ( pop , 4 ) ;
64. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XVIII
nurse . applySinglePointCrossover ( childPop , constIndex . length , 0 . 8 ) ;
nurse . applyMutationUniform ( childPop , constIndex . length , 0 . 2 ) ;
pop . addAll ( childPop ) ; / / append the two pops here
for ( int j =0; j <pop . size ( ) ; j ++){
i f ( pop . get ( j ) . getEvaluatedFlag ()== false ) { / / i f i t has not been evaluated
double [ ] tmpGenes=pop . get ( j ) . getGenotype ( ) ;
for ( int k=0;k<constIndex . length ; k++)
this . get ( constIndex [ k ] ) . g e t P r i m i t i v e ( ) . setName ( Double . to St rin g ( tmpGenes [ k
double [ ] evolved=this . interpretTree ( Context . XData ) ;
i f ( evolved . length != Context . Target . length ) {
double tmpEvolved=evolved [ 0 ] ; / / i f everything that i s returned i s a constan
evolved=new double [ Context . Target . length ] ;
for ( int z=0;z<Context . Target . length ; z++)
evolved [ z ]= tmpEvolved ;
}
double [ ] slope_n_intercept=new double [ 2 ] ;
linearScaling ( slope_n_intercept , evolved , Context . Target ) ;
/ / t h i s . t r a i n i n g R e s u l t =new double [ evolved . length ] ;
double tmpFitness =0;
double res ;
for ( int i j =0; i j <Context . Target . length ; i j ++){
res=Context . Target [ i j ]−( slope_n_intercept [0]∗ evolved [ i j ]+ slope_n_intercep
tmpFitness+=res∗res ;
}
tmpFitness=tmpFitness / evolved . length ;
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
tmpFitness=Double .MAX_VALUE;
65. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XIX
i f ( tmpFitness <this . f i t n e s s ) {
this . f i t n e s s =tmpFitness ;
this . slope=slope_n_intercept [ 0 ] ;
this . i n t e r c e p t =slope_n_intercept [ 1 ] ;
for ( int k=0;k<tmpGenes . length ; k++)
constants [ k ]=tmpGenes [ k ] ;
}
pop . get ( j ) . setFitness ( tmpFitness ) ;
i f ( Double . isNaN ( tmpFitness ) | | Double . i s I n f i n i t e ( tmpFitness ) )
{
this . f i t n e s s =Double .MAX_VALUE; / / assign a very large f i t n e s s
pop . get ( j ) . setFitness ( Double .MAX_VALUE) ;
}
pop . get ( j ) . setEvaluated ( true ) ;
} / / i f getEvaluated
}
nurse . applySurvival ( pop ) ;
}
for ( int i =0; i <constants . length ; i ++)
this . get ( constIndex [ i ] ) . g e t P r i m i t i v e ( ) . setName ( Double . to St rin g ( constants [ i ] ) ) ; / / set th
/ / System . out . p r i n t l n ( " Current best "+ t h i s . f i t n e s s ) ;
this . v a l i d = true ;
this . evaluated=true ;
return this . f i t n e s s ;
}
66. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
TREE XX
}
67. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS I
/∗
∗ S t a t i s t i c s . java
∗
∗ Created on September 14 , 2007, 3:06 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import java . io . ∗ ;
public class S t a t i s t i c s implements S e r i a l i z a b l e {
/∗∗ Creates a new instance of S t a t i s t i c s ∗/
public S t a t i s t i c s ( ) {
}
/∗∗
∗copy constructor
∗/
public S t a t i s t i c s ( S t a t i s t i c s copy ) {
t r a i n F i t H i s t o r y =new double [ copy . t r a i n F i t H i s t o r y . length ] ;
t e s t F i t H i s t o r y =new double [ copy . t e s t F i t H i s t o r y . length ] ;
68. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS II
for ( int i =0; i <copy . t r a i n F i t H i s t o r y . length ; i ++){
t r a i n F i t H i s t o r y [ i ]= copy . t r a i n F i t H i s t o r y [ i ] ;
t e s t F i t H i s t o r y [ i ]= copy . t e s t F i t H i s t o r y [ i ] ;
}
b e s t I n d i v i d u a l =new Tree ( copy . b e s t I n d i v i d u a l ) ;
}
public static double [ ] t r a i n F i t H i s t o r y ;
public static double [ ] t e s t F i t H i s t o r y ;
public static Tree b e s t I n d i v i d u a l ;
}
69. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS IO I
/∗
∗ S t a t i s t i c s I O . java
∗
∗ Created on September 15 , 2007, 6:30 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import java . io . ∗ ;
import java . u t i l . ∗ ;
public class S t a t i s t i c s I O {
/∗∗ Creates a new instance of S t a t i s t i c s I O ∗/
public S t a t i s t i c s I O ( ) {
}
/∗∗
∗writeStatsToDisk
∗/
70. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS IO II
public static void writeStatsToDisk ( ArrayList < S t a t i s t i c s > simStats , String filename ) throw
ObjectOutputStream out = null ;
try {
out = new ObjectOutputStream (new FileOutputStream ( filename ) ) ;
out . writeObject ( ( Object ) simStats ) ;
out . flush ( ) ;
} f i n a l l y {
i f ( out != null ) {
try {
out . close ( ) ;
} catch ( IOException exception ) { }
}
}
}
public static ArrayList < S t a t i s t i c s > readStatsFromDisk ( String filename ) throws IOException
ObjectInputStream in = null ;
try {
in=new ObjectInputStream (new FileInputStream ( filename ) ) ;
return ( ArrayList < S t a t i s t i c s >) in . readObject ( ) ;
} f i n a l l y {
i f ( in != null ) {
try {
in . close ( ) ;
} catch ( IOException e ) { }
}
}
71. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
STATISTICS IO III
}
}
72. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT I
/∗
∗ Context . java
∗
∗ Created on August 11 , 2007, 8:34 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import GeneticProgramming . P r i m i t i v e s . ∗ ;
import java . u t i l . ∗ ;
import java . io . ∗ ;
public class Context {
/∗∗ Creates a new instance of Context ∗/
public Context ( ) {
this . mFunctionSet=new ArrayList ( ) ;
this . mTerminalSet=new ArrayList ( ) ;
this . mPrimitiveMap=new TreeMap ( ) ;
this .mCache=new MyLinkedHashMap(500 , .75F , true ) ;
this . previousBest=Double .MAX_VALUE;
73. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT II
}
/∗∗
∗ b r i e f Add a new p r i m i t i v e in the sets of p r i m i t i v e .
∗ param i n P r i m i t i v e P r i m i t i v e added .
∗/
public static void i n s e r t ( P r i m i t i v e i n P r i m i t i v e )
{
assert ( ! mPrimitiveMap . containsKey ( i n P r i m i t i v e . getName ( ) ) ) ;
mPrimitiveMap . put ( i n P r i m i t i v e . getName ( ) , i n P r i m i t i v e ) ;
i f ( i n P r i m i t i v e . g e t A r i t y ( ) == 0) mTerminalSet . add ( i n P r i m i t i v e ) ;
else mFunctionSet . add ( i n P r i m i t i v e ) ;
}
/∗∗
∗This i s used to i n i t i a l i z e the data
∗/
public static void i n i t D a t a ( float [ ] [ ] X, float [ ] Y) {
assert (X. length==Y. length ) ;
XData=new float [X. length ] [ X [ 0 ] . length ] ;
Target=new double [X. length ] ;
for ( int i =0; i <X. length ; i ++){
for ( int j =0; j <X [ 0 ] . length ; j ++)
XData [ i ] [ j ]=X[ i ] [ j ] ;
Target [ i ]=Y[ i ] ;
}
74. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT III
}
/∗∗
∗This i s used to i n i t i a l i z e the Test data
∗/
public static void initTestData ( float [ ] [ ] X, double [ ] Y) {
assert (X. length==Y. length ) ;
TestXData=new float [X. length ] [ X [ 0 ] . length ] ;
TestTarget=new double [X. length ] ;
for ( int i =0; i <X. length ; i ++){
for ( int j =0; j <X [ 0 ] . length ; j ++)
TestXData [ i ] [ j ]=X[ i ] [ j ] ;
TestTarget [ i ]=Y[ i ] ;
}
}
/∗∗
∗This function i s used to read in data patterns . To be used by the java program
∗params :
∗filename−the f i l e containing IO patterns
∗sizeX− number of patterns
∗sizeY− number of variables ( f o r input data )
∗trainORtest− i f true , then t r a i n data , else t e s t data
∗/
public static void readDataPatterns ( int sizeX , int sizeY , boolean trainORtest ) {
StreamTokenizer tok=null ;
String filenameX , filenameY ;
i f ( trainORtest ) {
filenameX=trainDataFileX ;
filenameY=trainDataFileY ;
75. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT IV
}
else {
filenameX=testDataFileX ;
filenameY=testDataFileY ;
}
try {
tok = new StreamTokenizer (new BufferedReader (new FileReader ( filenameX ) ) ) ;
tok . ordinaryChar ( ’ n ’ ) ;
/ / tok . ordinaryChar ( )
}
catch ( java . io . FileNotFoundException e ) {
System . out . p r i n t l n ( e+" in Context . java " ) ;
}
int i =0;
int j =0;
float [ ] [ ] curDat=new float [ sizeX ] [ sizeY ] ;
String s t r ;
try {
j =0;
while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF) {
i f ( j >=44){ j =0; i ++;}
switch ( tok . ttype ) {
case StreamTokenizer .TT_EOL:
i ++;
j =0;
break ;
case ’ r ’ :
76. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT V
i ++;
j =0;
break ;
case ’n ’ :
i ++;
j =0;
break ;
case StreamTokenizer .TT_NUMBER:
curDat [ i ] [ j ] = ( float ) tok . nval ;
/ / System . out . p r i n t l n ( tok . nval ) ;
/ / j = j +1;
/ / i f ( j ==3){
/ / j =0;
/ / i ++;
/ / }
j ++;
break ;
default :
/ / i f ( tok . ttype != StreamTokenizer .TT_NUMBER && ( tok . sval . compareTo ( " t " ) ! = 0
/ / j =0;
/ / i ++;
/ / }
break ;
}
}
}
catch ( java . io . EOFException e ) {
/ / do nothing
77. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT VI
}
catch ( java . io . IOException e ) {
/ / do nothing
}
catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
/ / do nothing agin : (
System . out . p r i n t l n ( e+" Here l i e s the bug "+trainORtest+ j + i ) ;
/ / System . e x i t ( 0 ) ;
}
System . out . p r i n t l n ( " Here i s what I read n"+ i + j ) ;
for ( i =0; i <2; i ++){
for ( j =0; j <3; j ++)
System . out . p r i n t ( curDat [ i ] [ j ]+ " t " ) ;
System . out . p r i n t l n ( ) ;
}
try {
tok = new StreamTokenizer (new BufferedReader (new FileReader ( filenameY ) ) ) ;
tok . ordinaryChar ( ’ n ’ ) ;
/ / tok . ordinaryChar ( )
}
catch ( java . io . FileNotFoundException e ) {
System . out . p r i n t l n ( e+" in Context . java " ) ;
}
i =0;
double [ ] DatY=new double [ sizeX ] ;
try {
j =0;
while ( tok . nextToken ( ) ! = StreamTokenizer .TT_EOF) {
78. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT VII
switch ( tok . ttype ) {
case StreamTokenizer .TT_EOL:
/ / i ++;
break ;
/ / case ’ n ’ :
/ / i ++;
/ / j =0;
/ / break ;
case StreamTokenizer .TT_NUMBER:
DatY [ i ]= tok . nval ;
i ++;
/ / System . out . p r i n t l n ( tok . nval ) ;
break ;
default :
/ / j ++;
break ;
}
}
}
catch ( java . io . EOFException e ) {
/ / do nothing
}
catch ( java . io . IOException e ) {
/ / do nothing
}
catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
/ / do nothing agin : (
System . out . p r i n t l n ( e+" Here l i e s the bug " ) ;
79. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT VIII
System . e x i t ( 0 ) ;
}
i f ( trainORtest ) {
XData=curDat ;
Target=DatY ;
}
else {
TestXData=curDat ;
TestTarget=DatY ;
}
/ / f o r ( i n t j j =0; j j <XData . length ; j j ++){
/ / XData [ j j ][1]∗=100;
/ / }
}
public static ArrayList <Primitive > mFunctionSet ; / / ! < Set of functions usable to
public static ArrayList <Primitive > mTerminalSet ; / / ! < Set of terminals usable to
public static TreeMap<String , Primitive > mPrimitiveMap ; / / ! < Name−p r i m i t i v e map.
public static LinkedHashMap<String , Evaluation > mCache;
public static float mutationProba , swapMutationProba , xoverProba , inInitGrowProba , xoverNodeDi
public static int dynamicTreeDepth , popSize , initTreeDepthMin , initTreeDepthMax , tournamentSiz
public static float [ ] [ ] XData ;
public static double [ ] Target ;
public static float [ ] [ ] TestXData ;
public static double [ ] TestTarget ;
public static String trainDataFileX ;
public static String trainDataFileY ;
public static String testDataFileX ;
80. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT IX
public static String testDataFileY ;
public static boolean useTestData ;
public static int length_of_run ;
public static double previousBest ; / / f i t n e s s of the best i n d i v i d u a l of the previous generation
}
class MyLinkedHashMap extends LinkedHashMap {
/ / This method i s called j u s t a f t e r a new entry has been added
public MyLinkedHashMap ( int val1 , float val2 , boolean val3 ) {
super ( val1 , val2 , val3 ) ;
}
public boolean removeEldestEntry (Map. Entry eldest ) {
return size ( ) > 500;
}
/∗ public boolean containsKey ( String key ) {
I t e r a t o r <String > s t r = t h i s . keySet ( ) . i t e r a t o r ( ) ;
while ( s t r . hasNext ( ) ) {
i f ( s t r . next ( ) . compareTo ( key )==0){
return true ;
}
}
return false ;
}
public Evaluation get ( String key ) {
Evaluation eval= n u l l ;
I t e r a t o r <Map. Entry > e n t r y I t r = t h i s . entrySet ( ) . i t e r a t o r ( ) ;
81. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
CONTEXT X
while ( e n t r y I t r . hasNext ( ) ) {
Map. Entry temp= e n t r y I t r . next ( ) ;
i f ( temp . getKey ( ) . t oS tri ng ( ) . compareTo ( key )==0){
return ( Evaluation ) temp . getValue ( ) ;
}
}
return n u l l ;
} ∗/
}
82. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES I
/∗
∗ PrimitiveHandle . java
∗
∗ Created on August 10 , 2007, 9:02 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming . P r i m i t i v e s ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import com. sun . org . apache . bcel . i n t e r n a l . c l a s s f i l e . JavaClass ;
import java . u t i l . ∗ ;
import GeneticProgramming . ∗ ;
import java . io . ∗ ;
public abstract class P r i m i t i v e implements S e r i a l i z a b l e {
protected String name;
protected int a r i t y ;
protected int mRefCounter ;
protected Random dice ;
83. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES II
/∗∗ Creates a new instance of PrimitiveHandle ∗/
public P r i m i t i v e ( ) {
}
/∗∗
∗ param inNumberArguments Number of arguments of the p r i m i t i v e .
∗ param inName Name of the p r i m i t i v e .
∗/
public P r i m i t i v e ( String inName , int a r i t y )
{
this .name=new String ( inName ) ;
this . a r i t y = a r i t y ;
mRefCounter=0;
}
/∗∗
∗ b r i e f Copy−construct a p r i m i t i v e .
∗ param i n R i g h t P r i m i t P r i m i t i v e to copy .
∗/
public P r i m i t i v e ( f i n a l P r i m i t i v e copy )
{
this .name=new String ( copy .name ) ;
this . a r i t y =copy . a r i t y ;
mRefCounter=0;
}
/∗∗
∗Assign the input argument ( P r i m i t i v e object ) to
84. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES III
∗t h i s object
∗/
public P r i m i t i v e assign ( f i n a l P r i m i t i v e p r i m i t i v e )
{
i f ( this == p r i m i t i v e ) return this ;
name = new String ( p r i m i t i v e .name ) ;
this . a r i t y = p r i m i t i v e . a r i t y ;
return this ;
}
/∗∗
∗ b r i e f Give a reference on the actual p r i m i t i v e .
∗ param object of Random
∗ return P r i m i t i v e handle to t h i s pointer .
∗/
public abstract P r i m i t i v e giveReference (Random dice ) ; / / { return n u l l ; }
/∗ {
i f ( t h i s . getName ( ) . equalsIgnoreCase ( " Ephemeral " ) ) { / / should return the Ephemeral random numbe
return new P r i m i t i v e ( java . lang . Double . t oS tr in g ( dice . nextDouble ()∗12 −6) ,0);
}
return t h i s ;
} ∗/
/∗∗
∗ b r i e f Set the name of the p r i m i t i v e .
∗ param inName Name of the p r i m i t i v e .
∗/
85. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES IV
public void setName ( String inName )
{
this .name =new String ( inName ) ;
}
/∗∗
∗return the name of t h i s p r i m i t i v e
∗/
public f i n a l String getName ( ) {
return this .name;
}
/∗∗
∗ b r i e f Set the number of arguments of the p r i m i t i v e .
∗ param inNumberArguments Number of arguments of the p r i m i t i v e .
∗/
public void s e t A r i t y ( int i n A r i t y )
{
this . a r i t y = i n A r i t y ;
}
/∗ !
∗ b r i e f Set the value of the p r i m i t i v e ( do nothing f o r basic p r i m i t i v e ) .
∗ param inValue New value to use .
∗/
/ / void Puppy : : P r i m i t i v e : : setValue ( const void∗ inValue )
/ / { }
86. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES V
/∗∗
∗Returns the a r i t y of t h i s p r i m i t i v e
∗/
public f i n a l int g e t A r i t y ( ) {
return this . a r i t y ;
}
/∗∗
∗ b r i e f Get the value of the nth argument .
∗ param inN Index of the argument to get .
∗ param outResult Value of the nth argument .
∗ param ioContext Evolutionary context .
∗ XData
∗ patternNumber
∗/
public double [ ] getArgument ( int inN , ArrayList <Integer > callStack , Tree tree ,
f i n a l float [ ] [ ] XData )
{
assert ( inN >= 0 ) ;
assert ( inN < this . g e t A r i t y ( ) ) ;
/ /
/ /
int lIndex = callStack . get ( callStack . size () −1). intValue ( ) + 1;
for ( int i =0; i <inN ; ++ i ) lIndex += tree . get ( lIndex ) . getSubTreeSize ( ) ;
callStack . add (new Integer ( lIndex ) ) ;
double [ ] outResult=tree . get ( lIndex ) . g e t P r i m i t i v e ( ) . execute ( callStack , tree , XData ) ;
87. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES VI
callStack . remove ( callStack . size () −1);
return outResult ;
}
public abstract P r i m i t i v e clone ( ) ;
/∗∗
∗Function i s used to execute t h i s p r i m i t i v e
∗outResult
∗callStack
∗tree to which t h i s p r i m i t i v e belongs
∗XData
∗patternNumber , that comes in from evaluateTree in Tree
∗/
/ / public abstract double [ ] execute ( ArrayList <Integer > callStack , Tree tree , f l o a t [ ] [ ] XData ) ;
public abstract double [ ] execute ( ArrayList <Integer > callStack , Tree tree , float [ ] [ ] XData ) ; / /
/ / c a l l the cache here?
/∗ StringBuilder key=new StringBuilder ( tree . size ()− callStack . get ( callStack . size () −1). intVal
tree . writeTree2 ( callStack . get ( callStack . size () −1). intValue ( ) , key ) ;
Evaluation evaluation=PuppysMain . lContext .mCache. get ( key . to St rin g ( ) ) ;
i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . only non−terminals are evaluated here
i f ( evaluation != n u l l ) {
/ / double [ ] evaled=lContext .mCache. get ( key . t oSt ri ng ( ) ) . evaluation ;
/ / f o r ( i n t i =0; i <evaled . length ; i ++)
/ / outResult [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lContext .mCache. get ( key . t oS tr in g ( ) ) . cachedFlag=true ; / / the entry i s now
PuppysMain . cacheHits+=tree . get ( callStack . size () −1). getSubTreeSize ( ) ; ;
return evaluation . evaluation ;
}
88. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES VII
}
∗/
/ / double lArg2 ;
/∗ double [ ] outResult= n u l l ;
switch ( t h i s . a r i t y ) {
case 2 : { / / f o r a l l the two a r i t y functions
double [ ] lArg2= n u l l ;
outResult=getArgument (0 , callStack , tree , XData ) ;
lArg2=getArgument (1 , callStack , tree , XData ) ;
i f ( t h i s . getName ( ) . compareTo ( " plus ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] += lArg2 [ i ] ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]+= outResult [ 0 ] ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]+= lArg2 [ 0 ] ;
}
}
else i f ( t h i s . getName ( ) . compareTo ( " minus ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] −= lArg2 [ i ] ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
89. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES VIII
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]−=outResult [ 0 ] ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]−=lArg2 [ 0 ] ;
}
}
else i f ( t h i s . getName ( ) . compareTo ( " times ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] ∗= lArg2 [ i ] ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]∗= outResult [ 0 ] ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]∗= lArg2 [ 0 ] ;
}
}
else i f ( t h i s . getName ( ) . compareTo ( " adilpower ")==0){
i f ( outResult . length==lArg2 . length )
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] =Math . pow( outResult [ i ] , lArg2 [ i ] ) ;
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
90. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES IX
f o r ( i n t i =lArg2 . length;−−i >=0;)
lArg2 [ i ]=Math . pow( lArg2 [ i ] , outResult [ 0 ] ) ;
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]=Math . pow( outResult [ i ] , lArg2 [ 0 ] ) ;
}
}
else { / / divide
i f ( outResult . length==lArg2 . length ) {
f o r ( i n t i =outResult . length;−−i >=0;){
t r y {
outResult [ i ] /= lArg2 [ i ] ;
}
catch ( Exception e ) {
outResult [ i ]= Double .NaN;
}
}
}
else i f ( outResult . length = = 1 ) { / / i . e . a terminal returned
f o r ( i n t i =lArg2 . length;−−i >=0;){
t r y {
lArg2 [ i ]+= outResult [ 0 ] ;
}
catch ( Exception e ) {
outResult [ i ]= Double .NaN;
}
91. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES X
}
outResult=lArg2 ; / / point outResult to t h i s array now
}
else i f ( lArg2 . length ==1){
f o r ( i n t i =outResult . length;−−i >=0;){
t r y {
outResult [ i ]+= lArg2 [ 0 ] ;
}
catch ( Exception e ) {
outResult [ i ]= Double .NaN;
}
}
}
}
break ;
}
case 1 : {
outResult=getArgument (0 , callStack , tree , XData ) ;
i f ( t h i s .name. compareTo ( " sin " ) = = 0 ) / / sin
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . sin ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " cos " ) = = 0 ) / / cos
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . cos ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " a d i l l o g ")==0)
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . log ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " adillog10 ")==0)
92. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XI
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( Math . log10 ( outResult [ i ] ) ) ;
else i f ( t h i s .name. compareTo ( " a d i l s q r t ")==0){
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ]=Math . sqrt ( outResult [ i ] ) ;
}
break ;
}
default : { / / A terminal has been reached
i f ( ! t h i s .name. startsWith ( "X " ) ) {
/ / double tmpVal=Double . valueOf ( t h i s .name ) . doubleValue ( ) ;
/ / f o r ( i n t i =0; i <outResult . length ; i ++)
/ / outResult [ i ]= tmpVal ;
outResult=new double [ 1 ] ;
outResult [0]= Double . valueOf ( t h i s .name ) . doubleValue ( ) ;
}
else i f ( t h i s .name. substring ( 0 , 1 ) . compareTo ( "X")==0){
/ / The f o l l o w i n g l i n e would automatically assign the p e r t i n e n t entry in the pattern
i n t tmp=Integer . valueOf ( t h i s .name. substring ( 1 ) ) . intValue () −1;
outResult=new double [ XData . length ] ;
f o r ( i n t i =outResult . length;−−i >=0;)
outResult [ i ] = ( double ) XData [ i ] [ tmp ] ; / / Return the whole array of the Input data ,
}
break ;
}
}
/∗ PuppysMain . noCacheHits ++;
i f ( evaluation== n u l l ) { / / i . e . i f the entry i s not present
93. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XII
i f ( t h i s . g e t A r i t y ()==0){
lContext .mCache. put ( key . to St ri ng ( ) , new Evaluation ( ) ) ; / / c a l l the default construc
/ / but the actual evaluation i s done here . Aimed at saving extra array−copying
}
else { / / the tree has c h i l d ( ren ) .
i n t lIndex = callStack . get ( callStack . size () −1). intValue ( ) + 1 ; / / index of the next
boolean hasCached=true ; / / that the subtree i s cahched
f o r ( i n t i =0; i < t h i s . a r i t y ; i ++){
StringBuilder subTree=new StringBuilder ( tree . size ()− lIndex ) ;
tree . writeTree2 ( lIndex , subTree ) ;
String s t r =new String ( subTree . t oS tr in g ( ) ) ;
Evaluation evaluation2=lContext .mCache. get ( s t r ) ;
t r y {
i f ( evaluation2 != n u l l )
i f ( evaluation2 . cachedFlag== false )
hasCached= false ;
/ / System . out . p r i n t l n ( " Alarming " ) ;
}
catch ( Exception e ) {
hasCached= false ;
System . out . p r i n t l n ( "Do I reach here? "+e ) ;
/ / System . e x i t ( 0 ) ;
}
lIndex+=tree . get ( lIndex ) . getSubTreeSize ( ) ;
}
i f ( hasCached ) { / / That i s hasCached survived through a l l the subChildren
lContext .mCache. put (new String ( key . to St ri ng ( ) ) , new Evaluation ( outResult , fals
94. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XIII
}
}
} ∗/
/ / return outResult ;
/ / }
}
/∗
∗ Ephemeral . java
∗
∗ Created on September 27 , 2007, 11:49 PM
∗
∗ To change t h i s template , choose Tools | Template Manager
∗ and open the template in the e d i t o r .
∗/
package GeneticProgramming . P r i m i t i v e s . Terminals ;
/∗∗
∗
∗ @author a d i l r a j a
∗/
import GeneticProgramming . ∗ ;
import GeneticProgramming . P r i m i t i v e s . P r i m i t i v e ;
import java . u t i l . ∗ ;
95. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XIV
public class Ephemeral extends P r i m i t i v e {
/∗∗ Creates a new instance of Ephemeral ∗/
public Ephemeral ( ) {
}
/∗∗
∗ param inNumberArguments Number of arguments of the p r i m i t i v e .
∗ param inName Name of the p r i m i t i v e .
∗/
public Ephemeral ( String inName , int a r i t y ) {
super ( inName , a r i t y ) ;
}
/∗∗
∗ b r i e f Copy−construct a p r i m i t i v e .
∗ param i n R i g h t P r i m i t P r i m i t i v e to copy .
∗/
public Ephemeral ( f i n a l Ephemeral copy )
{
super ( copy ) ;
/ / t h i s .name=new String ( copy .name ) ;
/ / t h i s . a r i t y =copy . a r i t y ;
/ / mRefCounter=0;
}
/∗∗
∗ b r i e f Give a reference on the actual p r i m i t i v e .
96. Introduction Puppy’s Main Puppy Node Evaluation Tree Statistics Statistics IO Context Primitives Functions S
PRIMITIVES XV
∗ param object of Random
∗ return P r i m i t i v e handle to t h i s pointer .
∗/
public P r i m i t i v e giveReference (Random dice )
{
this .name=java . lang . Double . toS tr in g ( dice . nextDouble ()∗12 −6);
return this ;
}
/∗∗
∗Function i s used to execute t h i s p r i m i t i v e
∗outResult
∗callStack
∗tree to which t h i s p r i m i t i v e belongs
∗XData
∗patternNumber , that comes in from evaluateTree in Tree
∗/
public double [ ] execute ( ArrayList <Integer > callStack , Tree tree , float [ ] [ ] XData ) {
/ / c a l l the cache here?
/∗ StringBuilder key=new StringBuilder ( tree . size ()− callStack . get ( callStack . size () −1). intVal
tree . writeTree2 ( callStack . get ( callStack . size () −1). intValue ( ) , key ) ;
Evaluation evaluation=PuppysMain . lContext .mCache. get ( key . to St rin g ( ) ) ;
i f ( t h i s . g e t A r i t y ( ) > 0 ) { / / i . e . only non−terminals are evaluated here
i f ( evaluation != n u l l ) {
/ / double [ ] evaled=lContext .mCache. get ( key . t oSt ri ng ( ) ) . evaluation ;
/ / f o r ( i n t i =0; i <evaled . length ; i ++)
/ / outResult [ i ] = ( f l o a t ) evaled [ i ] ;
PuppysMain . lContext .mCache. get ( key . t oS tr in g ( ) ) . cachedFlag=true ; / / the entry i s now