├── Markov Decision Processes
├── assignment4
│ ├── target
│ │ └── classes
│ │ │ └── edu
│ │ │ └── gatech
│ │ │ └── cs7641
│ │ │ └── assignment4
│ │ │ ├── App.class
│ │ │ ├── Main.class
│ │ │ ├── Main$1.class
│ │ │ ├── Main$2.class
│ │ │ ├── Main$3.class
│ │ │ ├── Main$4.class
│ │ │ └── artifacts
│ │ │ ├── Analysis.class
│ │ │ ├── Hazard.class
│ │ │ ├── Problem.class
│ │ │ ├── Algorithm.class
│ │ │ ├── Analysis$1.class
│ │ │ ├── Coordinates.class
│ │ │ ├── Analysis$Result.class
│ │ │ ├── PlannerFactory.class
│ │ │ └── Hazard$HazardType.class
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ └── edu
│ │ │ └── gatech
│ │ │ └── cs7641
│ │ │ └── assignment4
│ │ │ ├── artifacts
│ │ │ ├── Coordinates.java
│ │ │ ├── Algorithm.java
│ │ │ ├── PlannerFactory.java
│ │ │ ├── Hazard.java
│ │ │ ├── Analysis.java
│ │ │ └── Problem.java
│ │ │ ├── App.java
│ │ │ └── Main.java
│ └── pom.xml
└── README.md
├── README.md
├── Assignment 4 Markov Decision Processes
├── assignment4
│ ├── target
│ │ └── classes
│ │ │ └── edu
│ │ │ └── gatech
│ │ │ └── cs7641
│ │ │ └── assignment4
│ │ │ ├── App.class
│ │ │ ├── Main.class
│ │ │ ├── Main$1.class
│ │ │ ├── Main$2.class
│ │ │ ├── Main$3.class
│ │ │ ├── Main$4.class
│ │ │ └── artifacts
│ │ │ ├── Hazard.class
│ │ │ ├── Problem.class
│ │ │ ├── Algorithm.class
│ │ │ ├── Analysis$1.class
│ │ │ ├── Analysis.class
│ │ │ ├── Coordinates.class
│ │ │ ├── Analysis$Result.class
│ │ │ ├── PlannerFactory.class
│ │ │ └── Hazard$HazardType.class
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ └── edu
│ │ │ └── gatech
│ │ │ └── cs7641
│ │ │ └── assignment4
│ │ │ ├── artifacts
│ │ │ ├── Coordinates.java
│ │ │ ├── Algorithm.java
│ │ │ ├── PlannerFactory.java
│ │ │ ├── Hazard.java
│ │ │ ├── Analysis.java
│ │ │ └── Problem.java
│ │ │ ├── App.java
│ │ │ └── Main.java
│ └── pom.xml
└── README.md
├── Assignment 1 Supervised Learning
├── README.md
└── CS 7641 HW1 Code.py
├── Assignment 3 Unsupervised Learning
└── README.md
└── Assignment 2 Randomized Optimization
├── README.md
├── FourPeaksTest_Toy.java
├── phishing_rhc.java
├── phishingwebsite_finaltest.java
├── ContinuousPeaks_Toy.java
├── phishing_ga_val.java
├── phishing_sa_val.java
└── TravelingSalesman_Toy.java
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/App.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/App.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$1.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$2.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$3.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$4.class
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CS-7641---Machine-Learning
2 | Repository for assignments from Georgia Tech's CS 7641 course.
3 |
4 | If you find my code useful, feel free to connect with me on [LinkedIn](https://www.linkedin.com/in/kyle-west-34482526/). Mention that you're from OMSA!
5 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/App.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/App.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Problem.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Problem.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$1.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$2.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$3.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/Main$4.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Algorithm.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Algorithm.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$1.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Coordinates.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Coordinates.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$Result.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$Result.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/PlannerFactory.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/PlannerFactory.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard$HazardType.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard$HazardType.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Problem.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Problem.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Algorithm.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Algorithm.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$1.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Coordinates.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Coordinates.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$Result.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Analysis$Result.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/PlannerFactory.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/PlannerFactory.class
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard$HazardType.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kylewest520/CS-7641---Machine-Learning/HEAD/Assignment 4 Markov Decision Processes/assignment4/target/classes/edu/gatech/cs7641/assignment4/artifacts/Hazard$HazardType.class
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Coordinates.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | public class Coordinates {
4 | public int x;
5 | public int y;
6 |
7 | public Coordinates(int x, int y) {
8 | this.x = x;
9 | this.y = y;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/App.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4;
2 |
3 | /**
4 | * Hello world!
5 | *
6 | */
7 | public class App
8 | {
9 | public static void main( String[] args )
10 | {
11 | System.out.println( "Hello World!" );
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Coordinates.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | public class Coordinates {
4 | public int x;
5 | public int y;
6 |
7 | public Coordinates(int x, int y) {
8 | this.x = x;
9 | this.y = y;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/App.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4;
2 |
3 | /**
4 | * Hello world!
5 | *
6 | */
7 | public class App
8 | {
9 | public static void main( String[] args )
10 | {
11 | System.out.println( "Hello World!" );
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Algorithm.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | public enum Algorithm {
4 | ValueIteration("Value Iteration"),
5 | PolicyIteration("Policy Iteration"),
6 | QLearning("Q-Learning");
7 |
8 | private String title;
9 |
10 | private Algorithm(String title) {
11 | this.title = title;
12 | }
13 |
14 | public String getTitle() {
15 | return this.title;
16 | }
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Algorithm.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | public enum Algorithm {
4 | ValueIteration("Value Iteration"),
5 | PolicyIteration("Policy Iteration"),
6 | QLearning("Q-Learning");
7 |
8 | private String title;
9 |
10 | private Algorithm(String title) {
11 | this.title = title;
12 | }
13 |
14 | public String getTitle() {
15 | return this.title;
16 | }
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/PlannerFactory.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | import burlap.behavior.singleagent.planning.Planner;
4 | import burlap.mdp.singleagent.SADomain;
5 | import burlap.mdp.singleagent.environment.SimulatedEnvironment;
6 | import burlap.statehashing.HashableStateFactory;
7 |
8 | public interface PlannerFactory {
9 | Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment);
10 | }
11 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/PlannerFactory.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | import burlap.behavior.singleagent.planning.Planner;
4 | import burlap.mdp.singleagent.SADomain;
5 | import burlap.mdp.singleagent.environment.SimulatedEnvironment;
6 | import burlap.statehashing.HashableStateFactory;
7 |
8 | public interface PlannerFactory {
9 | Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment);
10 | }
11 |
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Hazard.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | public class Hazard {
4 | public enum HazardType {
5 | SMALL, MEDIUM, LARGE
6 | }
7 |
8 | private Coordinates location;
9 | private double reward;
10 | private HazardType type;
11 |
12 | public Hazard(int x, int y, double reward, HazardType type) {
13 | this.location = new Coordinates(x, y);
14 | this.reward = reward;
15 | this.type = type;
16 | }
17 |
18 | public Coordinates getLocation() {
19 | return this.location;
20 | }
21 |
22 | public double getReward() {
23 | return this.reward;
24 | }
25 |
26 | public HazardType getType() {
27 | return this.type;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Hazard.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | public class Hazard {
4 | public enum HazardType {
5 | SMALL, MEDIUM, LARGE
6 | }
7 |
8 | private Coordinates location;
9 | private double reward;
10 | private HazardType type;
11 |
12 | public Hazard(int x, int y, double reward, HazardType type) {
13 | this.location = new Coordinates(x, y);
14 | this.reward = reward;
15 | this.type = type;
16 | }
17 |
18 | public Coordinates getLocation() {
19 | return this.location;
20 | }
21 |
22 | public double getReward() {
23 | return this.reward;
24 | }
25 |
26 | public HazardType getType() {
27 | return this.type;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | edu.gatech.cs7641
6 | assignment4
7 | 1.0
8 | jar
9 | CS7641 - Assignment 4 - Markov Decision Processes
10 | https://github.com/svpino/cs7641-assignment4
11 |
12 |
13 | UTF-8
14 |
15 |
16 |
17 |
18 | edu.brown.cs.burlap
19 | burlap
20 | 3.0.0
21 |
22 |
23 |
24 |
25 |
26 |
27 | org.apache.maven.plugins
28 | maven-compiler-plugin
29 |
30 | 1.8
31 | 1.8
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | edu.gatech.cs7641
6 | assignment4
7 | 1.0
8 | jar
9 | CS7641 - Assignment 4 - Markov Decision Processes
10 | https://github.com/svpino/cs7641-assignment4
11 |
12 |
13 | UTF-8
14 |
15 |
16 |
17 |
18 | edu.brown.cs.burlap
19 | burlap
20 | 3.0.0
21 |
22 |
23 |
24 |
25 |
26 |
27 | org.apache.maven.plugins
28 | maven-compiler-plugin
29 |
30 | 1.8
31 | 1.8
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/Assignment 1 Supervised Learning/README.md:
--------------------------------------------------------------------------------
1 | ## CS 7641 Assignment 1: Supervised Learning Classification
2 | This project seeks to understand the computatitonal and predictive qualities of five classification algorithms (Neural Network, SVM, kNN, Decision Tree, and Boosted Trees).
3 | Each algorithm will be run for two binary classification datasets so that we can compare and contrast them for two different problems (one for a balanced target variable and the other for an unbalanced target variable).
4 |
5 | Dataset 1: Phishing Websites - available at https://www.openml.org/d/4534
6 |
7 | Dataset 2: Bank Marketing - available at https://www.openml.org/d/1461
8 |
9 |
10 | ## Getting Started & Prerequisites
11 | For testing on your own machine, you need only to install python 3.6 and the following packages:
12 | - pandas, numpy, scikit-learn, matplotlib, itertools, timeit
13 |
14 |
15 | ## Running the Classifiers
16 | Optimal Way: Work with the iPython notebook (.ipnyb) using Jupyter or a similar environment. This allows you to "Run All" or you can run only the classifiers that you are interested in.
17 |
18 | Second Best Option: Run the python script (.py) after first editing the location where you have the two datasets saved on your local machine.
19 |
20 | Final Option (view only): Feel free to open up the (.html) file to see a sample output of all of the algorithms for both datasets.
21 |
22 | The code is broken up into three main sections:
23 | 1. Data Load & Preprocessing -> Exactly as it sounds. This section loads the data, performs one-hot encoding, scales numeric features, and reorders some of the columns.
24 | 2. Helper Functions -> This section defines a few functions that are used across all of the classifiers. The functions include building learning curves and evaluating the final classifers.
25 | 3. The Fun Part: Machine Learning! -> This section has funcions and execution cells for each of the 5 classifiers.
26 | 4. Model Comparison Plots -> Compare the classifiers with plots for training and prediction times as well as learning rate.
27 |
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Analysis.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | import java.util.HashMap;
4 | import java.util.List;
5 | import java.util.function.Consumer;
6 |
7 | public class Analysis {
8 | private HashMap results;
9 |
10 | public Analysis() {
11 | this.results = new HashMap();
12 | }
13 |
14 | public void add(int episode, List rewardSequence, int steps, long milliseconds) {
15 | Result result = new Result(0, steps, milliseconds);
16 | rewardSequence.forEach(new Consumer() {
17 |
18 | @Override
19 | public void accept(Double t) {
20 | result.reward += t;
21 | }
22 | });
23 |
24 | this.results.put(episode, result);
25 | }
26 |
27 | public void print() {
28 | System.out.println("Episode, Steps, Reward, Time (in milliseconds)");
29 |
30 | double totalReward = 0.0;
31 | int totalSteps = 0;
32 | long totalMilliseconds = 0;
33 | int minSteps = Integer.MAX_VALUE;
34 |
35 | for (Integer episodeIndex : this.results.keySet()) {
36 | Result result = this.results.get(episodeIndex);
37 |
38 | totalReward += result.reward;
39 | totalSteps += result.steps;
40 | totalMilliseconds += result.milliseconds;
41 |
42 | if (result.steps < minSteps) {
43 | minSteps = result.steps;
44 | }
45 |
46 | System.out.println(episodeIndex + ", " + result.steps + ", " + result.reward + ", " + result.milliseconds);
47 | }
48 |
49 | System.out.println("\nAverage Reward: " + totalReward / this.results.size());
50 | System.out.println("Average Number of Steps: " + totalSteps / this.results.size());
51 | System.out.println("Minimum Number of Steps: " + minSteps);
52 | System.out.println("Average Time (in milliseconds): " + totalMilliseconds / this.results.size());
53 | }
54 |
55 | public HashMap getResults() {
56 | return this.results;
57 | }
58 |
59 | public class Result {
60 | public double reward;
61 | public int steps;
62 | public long milliseconds;
63 |
64 | public Result(double reward, int steps, long milliseconds) {
65 | this.reward = reward;
66 | this.steps = steps;
67 | this.milliseconds = milliseconds;
68 | }
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Analysis.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | import java.util.HashMap;
4 | import java.util.List;
5 | import java.util.function.Consumer;
6 |
7 | public class Analysis {
8 | private HashMap results;
9 |
10 | public Analysis() {
11 | this.results = new HashMap();
12 | }
13 |
14 | public void add(int episode, List rewardSequence, int steps, long milliseconds) {
15 | Result result = new Result(0, steps, milliseconds);
16 | rewardSequence.forEach(new Consumer() {
17 |
18 | @Override
19 | public void accept(Double t) {
20 | result.reward += t;
21 | }
22 | });
23 |
24 | this.results.put(episode, result);
25 | }
26 |
27 | public void print() {
28 | System.out.println("Episode, Steps, Reward, Time (in milliseconds)");
29 |
30 | double totalReward = 0.0;
31 | int totalSteps = 0;
32 | long totalMilliseconds = 0;
33 | int minSteps = Integer.MAX_VALUE;
34 |
35 | for (Integer episodeIndex : this.results.keySet()) {
36 | Result result = this.results.get(episodeIndex);
37 |
38 | totalReward += result.reward;
39 | totalSteps += result.steps;
40 | totalMilliseconds += result.milliseconds;
41 |
42 | if (result.steps < minSteps) {
43 | minSteps = result.steps;
44 | }
45 |
46 | System.out.println(episodeIndex + ", " + result.steps + ", " + result.reward + ", " + result.milliseconds);
47 | }
48 |
49 | System.out.println("\nAverage Reward: " + totalReward / this.results.size());
50 | System.out.println("Average Number of Steps: " + totalSteps / this.results.size());
51 | System.out.println("Minimum Number of Steps: " + minSteps);
52 | System.out.println("Average Time (in milliseconds): " + totalMilliseconds / this.results.size());
53 | }
54 |
55 | public HashMap getResults() {
56 | return this.results;
57 | }
58 |
59 | public class Result {
60 | public double reward;
61 | public int steps;
62 | public long milliseconds;
63 |
64 | public Result(double reward, int steps, long milliseconds) {
65 | this.reward = reward;
66 | this.steps = steps;
67 | this.milliseconds = milliseconds;
68 | }
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/Assignment 3 Unsupervised Learning/README.md:
--------------------------------------------------------------------------------
1 | ## CS 7641 Assignment 3: Unsupervised Learning and Dimensionality Reduction
2 | This project seeks to understand the computatitonal and predictive qualities of two unsupervised learning techniques and four dimensionality reduction techniques.
3 |
4 | Unsupervised Learning Methods: k-means, expectation maximization (EM)
5 |
6 | Dimensionality Reduction Methods: principal components analysis (PCA), independent components analysis (ICA), random components analysis (RCA) and random forest feature selection.
7 |
8 | The two example datasets used in this project are:
9 |
10 | Dataset 1: Phishing Websites - available at https://www.openml.org/d/4534
11 |
12 | Dataset 2: Bank Marketing - available at https://www.openml.org/d/1461
13 |
14 |
15 | ## Getting Started & Prerequisites
16 | For testing on your own machine, you need to install python 3.6 and the following packages:
17 | - pandas, numpy, scikit-learn, matplotlib, itertools, timeit
18 |
19 |
20 | ## Running the Code
21 | All code can be found in my public github repository: https://github.com/kylewest520/CS-7641---Machine-Learning/tree/master/Assignment%203%20Unsupervised%20Learning
22 |
23 | Optimal Way: Work with the iPython notebook (.ipnyb) using Jupyter or a similar environment. This allows you to "Run All" or you can run only the subsections that you are interested in.
24 |
25 | Second Best Option: Run the python script (.py) after first editing the location where you have the two datasets saved on your local machine.
26 |
27 | Final Option (view only): Feel free to open up the (.html) file to see a snapshot of the cleaned code without any output.
28 |
29 | The code is broken up into the following sections sections:
30 | 1. Data Load & Preprocessing -> Exactly as it sounds. This section loads the data, performs one-hot encoding, scales numeric features, and reorders some of the columns.
31 | 2. Helper Functions -> This section defines a few functions that are used throughout the assignment The functions include building learning curves, evaluating the classifers, performing a mode-vote routine within clusters.
32 | 3. Clustering: k-means and EM -> This section runs k-means and EM and produces some heuristic plots so that one can select the optimal number of clusters.
33 | 4. Dimensionality Reduction -> Performs PCA, ICA, RCA, and random forest dimensionality reduction on the original datasets.
34 | 5. Training Neural Network on Projected Data -> A fixed architecture neural network is trained from each of the reduced datasets. Learning curves are generated.
35 | 6. Model Comparison Plots -> Generates plots to compare each of the neural networks.
36 | 7. Training Neural Network on Projected Data and Cluster Lables -> Same as section 5, but this section adds new one-hot encoded features that represent cluster structure in the dataset.
37 |
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/README.md:
--------------------------------------------------------------------------------
1 | ## CS 7641 Assignment 2: Randomized Optimization
2 | This project seeks to understand the behavioral and computatitonal and predictive qualities of four random search optimzation methods:
3 | - Randomized Hill Climb (RHC)
4 | - Simulated Annealing (SA)
5 | - Genetic Algorithms (GA)
6 | - Mutual Information Maximizing Input Clustering (MIMIC)
7 |
8 | ## Prerequisites
9 | These instructions apply for Windows 10 x64.
10 | For testing on your own machine, you need to install the following libraries.
11 | - ABAGAIL: https://github.com/pushkar/ABAGAIL
12 | - Apache Ant: https://ant.apache.org/bindownload.cgi
13 | - Java Development Kit: https://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html
14 | - Add Java and Ant to your windows environment and path variables. A helpful guide is found at: https://www.mkyong.com/ant/how-to-install-apache-ant-on-windows/
15 |
16 | Once all of the prerequisites are installed, all of the methods are run from the Windows Command Prompt
17 |
18 | ## Getting Started
19 | 1. Download the dataset, PhishingWebsitesData_preprocessed.csv
20 | - Original Phishing Websites Data - available at https://www.openml.org/d/4534
21 | 2. Edit the following .java files to point them towards your downloaded PhishingWebsitesData_preprocessed.csv file location. You can also use this time to edit the .java files to change the neurnal network structure
22 | - phishing_rhc.java
23 | - phishing_sa_val.java
24 | - phishing_ga_val.java
25 | - phishingwebsite_finaltest.java
26 | 3. Convert all .java files to .class files with the following code from the command prompt.
27 | - javac phishing_rhc.java
28 | - javac phishing_sa_val.java
29 | - javac phishing_ga_val.java
30 | - javac phishingwebsite_finaltest.java
31 | 4. Move all .class files to the location ~\ABAGAIL\opt\test
32 | - Includes the 4 'phishing_' class files and the 3 '_Toy' class files
33 |
34 |
35 | ## Part 1: Training a Neural Network using Random Search (RHC, SA, GA)
36 | This section will train a neural network on the phishing websites dataset using RHC, SA, and GA. These methods are compared to each other and to the same network structure trained using backpropagation.
37 |
38 | Running the Models (via command prompt):
39 | - cd ~\ABAGAIL
40 | - ant
41 | - java -cp ABAGAIL.jar opt.test.phishing_rhc
42 | - java -cp ABAGAIL.jar opt.test.phishing_sa_val
43 | - java -cp ABAGAIL.jar opt.test.phishing_ga_val
44 | - java -cp ABAGAIL.jar opt.test.phishingwebsite_finaltest
45 |
46 | The model results (training times and neural network accuracies) are stored in .csv files located at ~\ABAGAIL\Optimization_Results
47 |
48 |
49 | ## Part 2: Random Search Toy Problems
50 | This section presents 3 toy optimization problems for which RHC, SA, GA, and MIMIC are all used to maximize the function fitness.
51 |
52 | ### 1. Traveling Salesman Problem - Highlights GA
53 | - java -cp ABAGAIL.jar opt.test.TravelingSalesman_Toy
54 | ### 2. Continuous Peaks Problem - Highlights SA
55 | - java -cp ABAGAIL.jar opt.test.ContinuousPeaks_Toy
56 | ### 3. Four Peaks Problem - Highlights MIMIC
57 | - java -cp ABAGAIL.jar opt.test.TravelingSalesman_Toy
58 |
59 | The model results (training times and fitness function values) are stored in .csv files located at ~\ABAGAIL\Optimization_Results
60 |
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Problem.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 |
7 | import edu.gatech.cs7641.assignment4.artifacts.Hazard.HazardType;
8 |
9 | public class Problem {
10 | private double defaultReward;
11 | private double goalReward;
12 | private Coordinates start;
13 | private Coordinates goal;
14 | private int[][] matrix;
15 | private List hazards;
16 | private HashMap hazardRewards;
17 | private HashMap numIterations;
18 |
19 | public Problem(String[] map, HashMap numIterations, double defaultReward, double goalReward, HashMap hazardRewards) {
20 | this.numIterations = numIterations;
21 | this.defaultReward = defaultReward;
22 | this.goalReward = goalReward;
23 | this.hazardRewards = hazardRewards;
24 |
25 | this.matrix = new int[map.length][map.length];
26 | this.hazards = new ArrayList();
27 |
28 | /*
29 | * There's really not much to talk about here. Well, actually, there's something important:
30 | * notice how the code below inverts the matrix before feeding it to BURLAP. If you don't
31 | * invert the matrix, you'll have to invert your display to properly read the output of
32 | * BURLAP. Believe me, it gets really annoying.
33 | */
34 | for (int i = 0; i < map.length; i++) {
35 | for (int j = 0; j < map[i].length(); j++) {
36 | int x = j;
37 | int y = getWidth() - 1 - i;
38 |
39 | this.matrix[x][y] = 0;
40 | if (map[i].charAt(j) == '1') {
41 | this.matrix[x][y] = 1;
42 | }
43 | else if (map[i].charAt(j) == 'X') {
44 | this.start = new Coordinates(x, y);
45 | }
46 | else if (map[i].charAt(j) == 'G') {
47 | this.goal = new Coordinates(x, y);
48 | }
49 | else if (this.hazardRewards != null) {
50 | if (map[i].charAt(j) == 'S') {
51 | this.hazards.add(new Hazard(x, y, this.hazardRewards.get(Hazard.HazardType.SMALL), Hazard.HazardType.SMALL));
52 | }
53 | else if (map[i].charAt(j) == 'M') {
54 | this.hazards.add(new Hazard(x, y, this.hazardRewards.get(Hazard.HazardType.MEDIUM), Hazard.HazardType.MEDIUM));
55 | }
56 | else if (map[i].charAt(j) == 'L') {
57 | this.hazards.add(new Hazard(x, y, this.hazardRewards.get(Hazard.HazardType.LARGE), Hazard.HazardType.LARGE));
58 | }
59 | }
60 | }
61 | }
62 | }
63 |
64 | public Coordinates getStart() {
65 | return this.start;
66 | }
67 |
68 | public Coordinates getGoal() {
69 | return this.goal;
70 | }
71 |
72 | public int[][] getMatrix() {
73 | return this.matrix;
74 | }
75 |
76 | public int getWidth() {
77 | return this.matrix.length;
78 | }
79 |
80 | public List getHazards() {
81 | return this.hazards;
82 | }
83 |
84 | public double getDefaultReward() {
85 | return this.defaultReward;
86 | }
87 |
88 | public double getGoalReward() {
89 | return this.goalReward;
90 | }
91 |
92 | public int getNumberOfIterations(Algorithm algorithm) {
93 | if (this.numIterations != null && this.numIterations.containsKey(algorithm)) {
94 | return this.numIterations.get(algorithm);
95 | }
96 |
97 | return 100;
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/artifacts/Problem.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4.artifacts;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 |
7 | import edu.gatech.cs7641.assignment4.artifacts.Hazard.HazardType;
8 |
9 | public class Problem {
10 | private double defaultReward;
11 | private double goalReward;
12 | private Coordinates start;
13 | private Coordinates goal;
14 | private int[][] matrix;
15 | private List hazards;
16 | private HashMap hazardRewards;
17 | private HashMap numIterations;
18 |
19 | public Problem(String[] map, HashMap numIterations, double defaultReward, double goalReward, HashMap hazardRewards) {
20 | this.numIterations = numIterations;
21 | this.defaultReward = defaultReward;
22 | this.goalReward = goalReward;
23 | this.hazardRewards = hazardRewards;
24 |
25 | this.matrix = new int[map.length][map.length];
26 | this.hazards = new ArrayList();
27 |
28 | /*
29 | * There's really not much to talk about here. Well, actually, there's something important:
30 | * notice how the code below inverts the matrix before feeding it to BURLAP. If you don't
31 | * invert the matrix, you'll have to invert your display to properly read the output of
32 | * BURLAP. Believe me, it gets really annoying.
33 | */
34 | for (int i = 0; i < map.length; i++) {
35 | for (int j = 0; j < map[i].length(); j++) {
36 | int x = j;
37 | int y = getWidth() - 1 - i;
38 |
39 | this.matrix[x][y] = 0;
40 | if (map[i].charAt(j) == '1') {
41 | this.matrix[x][y] = 1;
42 | }
43 | else if (map[i].charAt(j) == 'X') {
44 | this.start = new Coordinates(x, y);
45 | }
46 | else if (map[i].charAt(j) == 'G') {
47 | this.goal = new Coordinates(x, y);
48 | }
49 | else if (this.hazardRewards != null) {
50 | if (map[i].charAt(j) == 'S') {
51 | this.hazards.add(new Hazard(x, y, this.hazardRewards.get(Hazard.HazardType.SMALL), Hazard.HazardType.SMALL));
52 | }
53 | else if (map[i].charAt(j) == 'M') {
54 | this.hazards.add(new Hazard(x, y, this.hazardRewards.get(Hazard.HazardType.MEDIUM), Hazard.HazardType.MEDIUM));
55 | }
56 | else if (map[i].charAt(j) == 'L') {
57 | this.hazards.add(new Hazard(x, y, this.hazardRewards.get(Hazard.HazardType.LARGE), Hazard.HazardType.LARGE));
58 | }
59 | }
60 | }
61 | }
62 | }
63 |
64 | public Coordinates getStart() {
65 | return this.start;
66 | }
67 |
68 | public Coordinates getGoal() {
69 | return this.goal;
70 | }
71 |
72 | public int[][] getMatrix() {
73 | return this.matrix;
74 | }
75 |
76 | public int getWidth() {
77 | return this.matrix.length;
78 | }
79 |
80 | public List getHazards() {
81 | return this.hazards;
82 | }
83 |
84 | public double getDefaultReward() {
85 | return this.defaultReward;
86 | }
87 |
88 | public double getGoalReward() {
89 | return this.goalReward;
90 | }
91 |
92 | public int getNumberOfIterations(Algorithm algorithm) {
93 | if (this.numIterations != null && this.numIterations.containsKey(algorithm)) {
94 | return this.numIterations.get(algorithm);
95 | }
96 |
97 | return 100;
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/Markov Decision Processes/README.md:
--------------------------------------------------------------------------------
1 | ## CS 7641 Assignment 4: Markov Decision Processes and Reinforcement Learning
2 | This project seeks to understand the three reinforcement learning algorithms by applying them each to two different Markov decision processes (MDP). The reinforcement learning methods are value iteration, policy iteration, and Q-learning. The two MDP toy problems are inspired by Pacman! There is a small 5x5 grid world, and a large 20x20 grid world.
3 |
4 | For each grid, Pacman (our learning agent) starts in the top left corner and attempts to navigate his way to the goal by collecting a high score along his journey. Like the real game, Pacman has the opportunity to earn points by eating pellets and fruit, but he must avoid hitting the ghost at all costs. The reward structure for each grid world is represented by:
5 | - Small pellets (S) = +1 point
6 | - Medium fruit (M) = +2.5 points
7 | - Large ghosts (L) = -50 points
8 | - Reaching the goal = +100 points
9 | - Every step = -5 points to encourage Pacman to reach his goal quickly.
10 |
11 | ## Getting Started & Prerequisites
12 | For testing on your own machine, the easiest way to implement is to follow the steps below:
13 | 1. Download Eclipse IDE: https://www.eclipse.org/
14 | 2. Download or clone my github file repository: https://github.com/kylewest520/CS-7641---Machine-Learning/edit/master/Markov%20Decision%20Processes
15 | 3. Import the project into Eclipse: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-importproject.htm
16 | 4. Update the current Eclipse project using Maven by right-clicking the top level project folder > Maven > Update Project
17 | 5. Download the latest Java SE Development kit: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html
18 | 6. Ready to go! You can now run the main.java class in the project folder using Eclipse
19 |
20 | ## Modifying the Code (in Main.java)
21 | The code is set up to run all three reinforcement learning algorithms for both problems sets by modifying a few lines.
22 | 1. Change from running the small grid vs. large grid by modifying the PROBLEM parameter in line 50.
23 | 2. Change from running value iteration, policy iteration, Q-learning using the algorithm parameter in line 56.
24 | 3. Change stochastic behavior in line 88.
25 | 4. Change value iteration and policy iteration algorithm conditions under the line 134 switch statement
26 | 5. Change the maximum number of iteration steps in line 218.
27 |
28 | ## Creating Your Own Grid Worlds
29 | You can create your own grid worlds in Main.java line 282 (small grid) and line 320 (large grid). Note that this implementation requires both grid worlds to be square grids. The code allows for inputting a starting location (X), goal location (G), walls (1's), and small (S), medium (M), and large (L) rewards or penalties. To make S/M/L block a penalty, simply set the HazardType to be a negative number.
30 | An example for a small 5x5 grid is shown below:
31 |
32 | X0001
33 | 00001
34 | 0LML0
35 | 0000S
36 | 110SG
37 |
38 | ## Code Outputs
39 | After running the code for a given grid/algorithm there will be a few outputs.
40 | 1. Pop-up window of final policy showing actions and state values.
41 | 2. Data dump to eclipse terminal of the results of each iteration including number of steps, reward, and wall clock time to complete the iteration. This output is structure for easy copy and paste into a text editor so that the results can be quickly saved in a .csv format and used for further analysis in other programs (python, excel, etc.)
42 | 3. Algorithm summary statistics across all iterations: average reward, average steps, minimum steps, and average wall clock time.
43 |
44 | ## Acknowledgements
45 | The source code for this assignment was modified from the original versions found at:
46 | 1. http://burlap.cs.brown.edu/
47 | 2. https://github.com/svpino/cs7641-assignment4. I am indebted to this github repository. Please visit it to see the original implementation with extensive notes in the README.
48 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/README.md:
--------------------------------------------------------------------------------
1 | ## CS 7641 Assignment 4: Markov Decision Processes and Reinforcement Learning
2 | This project seeks to understand the three reinforcement learning algorithms by applying them each to two different Markov decision processes (MDP). The reinforcement learning methods are value iteration, policy iteration, and Q-learning. The two MDP toy problems are inspired by Pacman! There is a small 5x5 grid world, and a large 20x20 grid world.
3 |
4 | For each grid, Pacman (our learning agent) starts in the top left corner and attempts to navigate his way to the goal by collecting a high score along his journey. Like the real game, Pacman has the opportunity to earn points by eating pellets and fruit, but he must avoid hitting the ghost at all costs. The reward structure for each grid world is represented by:
5 | - Small pellets (S) = +1 point
6 | - Medium fruit (M) = +2.5 points
7 | - Large ghosts (L) = -50 points
8 | - Reaching the goal = +100 points
9 | - Every step = -5 points to encourage Pacman to reach his goal quickly.
10 |
11 | ## Getting Started & Prerequisites
12 | For testing on your own machine, the easiest way to implement is to follow the steps below:
13 | 1. Download Eclipse IDE: https://www.eclipse.org/
14 | 2. Download or clone my github file repository: https://github.com/kylewest520/CS-7641---Machine-Learning/tree/master/Assignment%204%20Markov%20Decision%20Processes
15 | 3. Import the project into Eclipse: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-importproject.htm
16 | 4. Update the current Eclipse project using Maven by right-clicking the top level project folder > Maven > Update Project
17 | 5. Download the latest Java SE Development kit: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html
18 | 6. Ready to go! You can now run the main.java class in the project folder using Eclipse
19 |
20 | ## Modifying the Code (in Main.java)
21 | The code is set up to run all three reinforcement learning algorithms for both problems sets by modifying a few lines.
22 | 1. Change from running the small grid vs. large grid by modifying the PROBLEM parameter in line 50.
23 | 2. Change from running value iteration, policy iteration, Q-learning using the algorithm parameter in line 56.
24 | 3. Change stochastic behavior in line 88.
25 | 4. Change value iteration and policy iteration algorithm conditions under the line 134 switch statement
26 | 5. Change the maximum number of iteration steps in line 218.
27 |
28 | ## Creating Your Own Grid Worlds
29 | You can create your own grid worlds in Main.java line 282 (small grid) and line 320 (large grid). Note that this implementation requires both grid worlds to be square grids. The code allows for inputting a starting location (X), goal location (G), walls (1's), and small (S), medium (M), and large (L) rewards or penalties. To make S/M/L block a penalty, simply set the HazardType to be a negative number.
30 | An example for a small 5x5 grid is shown below:
31 |
32 | X0001
33 | 00001
34 | 0LML0
35 | 0000S
36 | 110SG
37 |
38 | ## Code Outputs
39 | After running the code for a given grid/algorithm there will be a few outputs.
40 | 1. Pop-up window of final policy showing actions and state values.
41 | 2. Data dump to eclipse terminal of the results of each iteration including number of steps, reward, and wall clock time to complete the iteration. This output is structure for easy copy and paste into a text editor so that the results can be quickly saved in a .csv format and used for further analysis in other programs (python, excel, etc.)
42 | 3. Algorithm summary statistics across all iterations: average reward, average steps, minimum steps, and average wall clock time.
43 |
44 | ## Acknowledgements
45 | The source code for this assignment was modified from the original versions found at:
46 | 1. http://burlap.cs.brown.edu/
47 | 2. https://github.com/svpino/cs7641-assignment4. I am indebted to this github repository. Please visit it to see the original implementation with extensive notes in the README.
48 |
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/FourPeaksTest_Toy.java:
--------------------------------------------------------------------------------
1 | package opt.test;
2 |
3 | import java.io.BufferedWriter;
4 | import java.io.FileWriter;
5 | import java.io.PrintWriter;
6 | import java.nio.file.Files;
7 | import java.nio.file.Path;
8 | import java.nio.file.Paths;
9 | import java.text.SimpleDateFormat;
10 | import java.util.Arrays;
11 | import java.util.Date;
12 |
13 | import dist.DiscreteDependencyTree;
14 | import dist.DiscreteUniformDistribution;
15 | import dist.Distribution;
16 |
17 | import opt.DiscreteChangeOneNeighbor;
18 | import opt.EvaluationFunction;
19 | import opt.GenericHillClimbingProblem;
20 | import opt.HillClimbingProblem;
21 | import opt.NeighborFunction;
22 | import opt.RandomizedHillClimbing;
23 | import opt.SimulatedAnnealing;
24 | import opt.example.*;
25 | import opt.ga.CrossoverFunction;
26 | import opt.ga.DiscreteChangeOneMutation;
27 | import opt.ga.SingleCrossOver;
28 | import opt.ga.GenericGeneticAlgorithmProblem;
29 | import opt.ga.GeneticAlgorithmProblem;
30 | import opt.ga.MutationFunction;
31 | import opt.ga.StandardGeneticAlgorithm;
32 | import opt.prob.GenericProbabilisticOptimizationProblem;
33 | import opt.prob.MIMIC;
34 | import opt.prob.ProbabilisticOptimizationProblem;
35 | import shared.FixedIterationTrainer;
36 |
37 | public class FourPeaksTest_Toy {
38 | /** The n value */
39 | private static final int N = 200;
40 | /** The t value */
41 | private static final int T = N / 5;
42 |
43 | public static void write_output_to_file(String output_dir, String file_name, String results, boolean final_result) {
44 | try {
45 | if (final_result) {
46 | String augmented_output_dir = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
47 | String full_path = augmented_output_dir + "/" + file_name;
48 | Path p = Paths.get(full_path);
49 | if (Files.notExists(p)) {
50 | Files.createDirectories(p.getParent());
51 | }
52 | PrintWriter pwtr = new PrintWriter(new BufferedWriter(new FileWriter(full_path, true)));
53 | synchronized (pwtr) {
54 | pwtr.println(results);
55 | pwtr.close();
56 | }
57 | }
58 | else {
59 | String full_path = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file_name;
60 | Path p = Paths.get(full_path);
61 | Files.createDirectories(p.getParent());
62 | Files.write(p, results.getBytes());
63 | }
64 | }
65 | catch (Exception e) {
66 | e.printStackTrace();
67 | }
68 |
69 | }
70 | public static void main(String[] args) {
71 |
72 |
73 | /*Editted for Clock Time*/
74 |
75 | double start, end, time;
76 | int[] iters = {10, 100, 1000, 5000, 10000, 25000, 50000, 100000, 250000};
77 | //int[] iters = {10};
78 | int testRuns = 5;
79 |
80 | for (int iter : iters) {
81 |
82 | int sum_rhc = 0;
83 | int sum_sa = 0;
84 | int sum_ga = 0;
85 | int sum_mimic = 0;
86 |
87 | double time_rhc = 0;
88 | double time_sa = 0;
89 | double time_ga = 0;
90 | double time_mimic = 0;
91 |
92 | for (int j = 0; j < testRuns; j++) {
93 | int[] ranges = new int[N];
94 | Arrays.fill(ranges, 2);
95 | EvaluationFunction ef = new FourPeaksEvaluationFunction(T);
96 | Distribution odd = new DiscreteUniformDistribution(ranges);
97 | NeighborFunction nf = new DiscreteChangeOneNeighbor(ranges);
98 | MutationFunction mf = new DiscreteChangeOneMutation(ranges);
99 | CrossoverFunction cf = new SingleCrossOver();
100 | Distribution df = new DiscreteDependencyTree(.1, ranges);
101 | HillClimbingProblem hcp = new GenericHillClimbingProblem(ef, odd, nf);
102 | GeneticAlgorithmProblem gap = new GenericGeneticAlgorithmProblem(ef, odd, mf, cf);
103 | ProbabilisticOptimizationProblem pop = new GenericProbabilisticOptimizationProblem(ef, odd, df);
104 |
105 |
106 | start = System.nanoTime();
107 | RandomizedHillClimbing rhc = new RandomizedHillClimbing(hcp);
108 | FixedIterationTrainer fit = new FixedIterationTrainer(rhc, iter);
109 | fit.train();
110 | end = System.nanoTime();
111 | time = end - start;
112 | time /= Math.pow(10, 9);
113 | sum_rhc += ef.value(rhc.getOptimal());
114 | time_rhc += time;
115 | //System.out.println("rhc: " + ef.value(rhc.getOptimal()));
116 | //System.out.println(time);
117 |
118 | start = System.nanoTime();
119 | SimulatedAnnealing sa = new SimulatedAnnealing(1E11, .95, hcp);
120 | fit = new FixedIterationTrainer(sa, iter);
121 | fit.train();
122 | end = System.nanoTime();
123 | time = end - start;
124 | time /= Math.pow(10, 9);
125 | sum_sa += ef.value(sa.getOptimal());
126 | time_sa += time;
127 | //System.out.println("sa: " + ef.value(sa.getOptimal()));
128 | //System.out.println(time);
129 |
130 |
131 | start = System.nanoTime();
132 | StandardGeneticAlgorithm ga = new StandardGeneticAlgorithm(200, 20, 10, gap);
133 | fit = new FixedIterationTrainer(ga, iter);
134 | fit.train();
135 | end = System.nanoTime();
136 | time = end - start;
137 | time /= Math.pow(10, 9);
138 | sum_ga += ef.value(ga.getOptimal());
139 | time_ga += time;
140 | //System.out.println("ga: " + ef.value(ga.getOptimal()));
141 | //System.out.println(time);
142 |
143 |
144 |
145 | start = System.nanoTime();
146 | MIMIC mimic = new MIMIC(200, 20, pop);
147 | fit = new FixedIterationTrainer(mimic, iter);
148 | fit.train();
149 | end = System.nanoTime();
150 | time = end - start;
151 | time /= Math.pow(10, 9);
152 | sum_mimic += ef.value(mimic.getOptimal());
153 | time_mimic += time;
154 | //System.out.println("Mimic: " + ef.value(mimic.getOptimal()));
155 | //System.out.println(time);
156 |
157 | }
158 |
159 | int average_rhc = sum_rhc / testRuns;
160 | int average_sa = sum_sa / testRuns;
161 | int average_ga = sum_ga / testRuns;
162 | int average_mimic = sum_mimic / testRuns;
163 |
164 | double averagetime_rhc = time_rhc / testRuns;
165 | double averagetime_sa = time_sa / testRuns;
166 | double averagetime_ga = time_ga / testRuns;
167 | double averagetime_mimic = time_mimic / testRuns;
168 |
169 | System.out.println("##############");
170 | System.out.println("this is iteration " + iter);
171 | System.out.println("rhc average is " + average_rhc + ", time average is " + averagetime_rhc);
172 | System.out.println("sa average is " + average_sa + ", time average is " + averagetime_sa);
173 | System.out.println("ga average is " + average_ga + ", time average is " + averagetime_ga);
174 | System.out.println("mimic average is " + average_mimic + ", time average is " + averagetime_mimic);
175 |
176 | String final_result = "";
177 | final_result = "rhc" + "," + iter + "," + Integer.toString(average_rhc) + "," + Double.toString(averagetime_rhc) + "," +
178 | "sa" + "," + iter + "," + Integer.toString(average_sa) + "," + Double.toString(averagetime_sa) + "," +
179 | "ga" + "," + iter + "," + Integer.toString(average_ga) + "," + Double.toString(averagetime_ga) + "," +
180 | "mimic" + "," + iter + "," + Integer.toString(average_mimic) + "," + Double.toString(averagetime_mimic);
181 |
182 | write_output_to_file("Optimization_Results", "FourPeaks_results.csv", final_result, true);
183 | }
184 |
185 | }
186 | }
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/phishing_rhc.java:
--------------------------------------------------------------------------------
1 | package opt.test;
2 |
3 | /*
4 | * Created by Kyle West on 9/29/2018.
5 | * Adapted from PhishingWebsites.java by Daniel Cai (in turn adapted from AbaloneTest.java by Hannah Lau)
6 | */
7 | import func.nn.backprop.BackPropagationNetwork;
8 | import func.nn.backprop.BackPropagationNetworkFactory;
9 | import func.nn.backprop.BatchBackPropagationTrainer;
10 | import func.nn.backprop.RPROPUpdateRule;
11 | import opt.OptimizationAlgorithm;
12 | import opt.RandomizedHillClimbing;
13 | import opt.SimulatedAnnealing;
14 | import opt.example.NeuralNetworkOptimizationProblem;
15 | import opt.ga.StandardGeneticAlgorithm;
16 | import shared.*;
17 | import func.nn.activation.*;
18 |
19 | import java.io.*;
20 | import java.nio.file.Files;
21 | import java.nio.file.Path;
22 | import java.nio.file.Paths;
23 | import java.text.DecimalFormat;
24 | import java.text.SimpleDateFormat;
25 | import java.util.Arrays;
26 | import java.util.Date;
27 | import java.util.Scanner;
28 | public class phishing_rhc {
29 | private static Instance[] instances = initializeInstances();
30 | private static Instance[] train_set = Arrays.copyOfRange(instances, 0, 8844);
31 | private static Instance[] test_set = Arrays.copyOfRange(instances, 8844, 11055);
32 |
33 | private static DataSet set = new DataSet(train_set);
34 |
35 | private static int inputLayer = 46, hiddenLayer = 50, outputLayer = 1;
36 | private static BackPropagationNetworkFactory factory = new BackPropagationNetworkFactory();
37 |
38 | private static ErrorMeasure measure = new SumOfSquaresError();
39 |
40 | private static BackPropagationNetwork networks[] = new BackPropagationNetwork[1];
41 | private static NeuralNetworkOptimizationProblem[] nnop = new NeuralNetworkOptimizationProblem[1];
42 |
43 | private static OptimizationAlgorithm[] oa = new OptimizationAlgorithm[1];
44 | private static String[] oaNames = {"RHC"};
45 | private static String results = "";
46 |
47 | private static DecimalFormat df = new DecimalFormat("0.000");
48 |
49 |
50 |
51 | public static void write_output_to_file(String output_dir, String file_name, String results, boolean final_result) {
52 | try {
53 | if (final_result) {
54 | String augmented_output_dir = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
55 | String full_path = augmented_output_dir + "/" + file_name;
56 | Path p = Paths.get(full_path);
57 | if (Files.notExists(p)) {
58 | Files.createDirectories(p.getParent());
59 | }
60 | PrintWriter pwtr = new PrintWriter(new BufferedWriter(new FileWriter(full_path, true)));
61 | synchronized (pwtr) {
62 | pwtr.println(results);
63 | pwtr.close();
64 | }
65 | }
66 | else {
67 | String full_path = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file_name;
68 | Path p = Paths.get(full_path);
69 | Files.createDirectories(p.getParent());
70 | Files.write(p, results.getBytes());
71 | }
72 | }
73 | catch (Exception e) {
74 | e.printStackTrace();
75 | }
76 |
77 | }
78 |
79 |
80 | public static void main(String[] args) {
81 |
82 | String final_result = "";
83 |
84 |
85 | for(int i = 0; i < oa.length; i++) {
86 | networks[i] = factory.createClassificationNetwork(
87 | new int[] {inputLayer, hiddenLayer, outputLayer});
88 | nnop[i] = new NeuralNetworkOptimizationProblem(set, networks[i], measure);
89 | }
90 |
91 | oa[0] = new RandomizedHillClimbing(nnop[0]);
92 |
93 | int[] iterations = {10,100,500,1000,2500,5000};
94 | //int[] iterations = {10};
95 |
96 | for (int trainingIterations : iterations) {
97 | results = "";
98 | for (int i = 0; i < oa.length; i++) {
99 | double start = System.nanoTime(), end, trainingTime, testingTime, correct = 0, incorrect = 0;
100 | train(oa[i], networks[i], oaNames[i], trainingIterations); //trainer.train();
101 | end = System.nanoTime();
102 | trainingTime = end - start;
103 | trainingTime /= Math.pow(10, 9);
104 |
105 | Instance optimalInstance = oa[i].getOptimal();
106 | networks[i].setWeights(optimalInstance.getData());
107 |
108 | // Calculate Training Set Statistics //
109 | double predicted, actual;
110 | start = System.nanoTime();
111 | for (int j = 0; j < train_set.length; j++) {
112 | networks[i].setInputValues(train_set[j].getData());
113 | networks[i].run();
114 |
115 | actual = Double.parseDouble(train_set[j].getLabel().toString());
116 | predicted = Double.parseDouble(networks[i].getOutputValues().toString());
117 |
118 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
119 |
120 | }
121 | end = System.nanoTime();
122 | testingTime = end - start;
123 | testingTime /= Math.pow(10, 9);
124 |
125 | results += "\nTrain Results for " + oaNames[i] + ": \nCorrectly classified " + correct + " instances." +
126 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
127 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
128 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
129 |
130 | final_result = oaNames[i] + "," + trainingIterations + "," + "training accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
131 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
132 | "," + df.format(testingTime);
133 | write_output_to_file("Optimization_Results", "phishing_results_rhc.csv", final_result, true);
134 |
135 | // Calculate Test Set Statistics //
136 | start = System.nanoTime();
137 | correct = 0;
138 | incorrect = 0;
139 | for (int j = 0; j < test_set.length; j++) {
140 | networks[i].setInputValues(test_set[j].getData());
141 | //System.out.println("input set:" + test_set[j].getData());
142 | //System.out.println("target:" + );
143 | networks[i].run();
144 |
145 | actual = Double.parseDouble(test_set[j].getLabel().toString());
146 | predicted = Double.parseDouble(networks[i].getOutputValues().toString());
147 |
148 | //System.out.println("actual:" + actual + ", predicted:" + predicted);
149 |
150 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
151 | }
152 | end = System.nanoTime();
153 | testingTime = end - start;
154 | testingTime /= Math.pow(10, 9);
155 |
156 | results += "\nTest Results for " + oaNames[i] + ": \nCorrectly classified " + correct + " instances." +
157 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
158 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
159 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
160 |
161 | final_result = oaNames[i] + "," + trainingIterations + "," + "testing accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
162 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
163 | "," + df.format(testingTime);
164 | write_output_to_file("Optimization_Results", "phishing_results_rhc.csv", final_result, true);
165 | }
166 | System.out.println("results for iteration: " + trainingIterations + "---------------------------");
167 | System.out.println(results);
168 | }
169 | }
170 |
171 | private static void train(OptimizationAlgorithm oa, BackPropagationNetwork network, String oaName, int iteration) {
172 | //System.out.println("\nError results for " + oaName + "\n---------------------------");
173 | int trainingIterations = iteration;
174 | for(int i = 0; i < trainingIterations; i++) {
175 | oa.train();
176 |
177 | double train_error = 0;
178 | for(int j = 0; j < train_set.length; j++) {
179 | network.setInputValues(train_set[j].getData());
180 | network.run();
181 |
182 | Instance output = train_set[j].getLabel(), example = new Instance(network.getOutputValues());
183 | example.setLabel(new Instance(Double.parseDouble(network.getOutputValues().toString())));
184 | train_error += measure.value(output, example);
185 | }
186 |
187 |
188 | //System.out.println("training error :" + df.format(train_error)+", testing error: "+df.format(test_error));
189 | }
190 | }
191 |
192 | private static Instance[] initializeInstances() {
193 |
194 | double[][][] attributes = new double[11055][][];
195 |
196 | try {
197 | BufferedReader br = new BufferedReader(new FileReader(new File("C:/Users/westy/OneDrive/01 - CS 7641 - Machine Learning/HW2/PhishingWebsitesData_preprocessed.csv")));
198 |
199 | //for each sample
200 | for(int i = 0; i < attributes.length; i++) {
201 | Scanner scan = new Scanner(br.readLine());
202 | scan.useDelimiter(",");
203 |
204 | attributes[i] = new double[2][];
205 | attributes[i][0] = new double[46]; // 46 attributes
206 | attributes[i][1] = new double[1]; // classification
207 |
208 | // read features
209 | for(int j = 0; j < 46; j++)
210 | attributes[i][0][j] = Double.parseDouble(scan.next());
211 |
212 | attributes[i][1][0] = Double.parseDouble(scan.next());
213 | //System.out.println("target var:" + attributes[i][1][0]);
214 |
215 | }
216 | }
217 | catch(Exception e) {
218 | e.printStackTrace();
219 | }
220 |
221 | Instance[] instances = new Instance[attributes.length];
222 |
223 | for(int i = 0; i < instances.length; i++) {
224 | instances[i] = new Instance(attributes[i][0]);
225 | instances[i].setLabel(new Instance(attributes[i][1][0]));
226 | }
227 |
228 | return instances;
229 | }
230 | }
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/phishingwebsite_finaltest.java:
--------------------------------------------------------------------------------
1 | package opt.test;
2 |
3 | /*
4 | * Created by Kyle West on 9/29/2018.
5 | * Adapted from PhishingWebsites.java by Daniel Cai (in turn adapted from AbaloneTest.java by Hannah Lau)
6 | */
7 | import func.nn.backprop.BackPropagationNetwork;
8 | import func.nn.backprop.BackPropagationNetworkFactory;
9 | import func.nn.backprop.BatchBackPropagationTrainer;
10 | import func.nn.backprop.RPROPUpdateRule;
11 | import opt.OptimizationAlgorithm;
12 | import opt.RandomizedHillClimbing;
13 | import opt.SimulatedAnnealing;
14 | import opt.example.NeuralNetworkOptimizationProblem;
15 | import opt.ga.StandardGeneticAlgorithm;
16 | import shared.*;
17 | import func.nn.activation.*;
18 |
19 | import java.io.*;
20 | import java.nio.file.Files;
21 | import java.nio.file.Path;
22 | import java.nio.file.Paths;
23 | import java.text.DecimalFormat;
24 | import java.text.SimpleDateFormat;
25 | import java.util.Arrays;
26 | import java.util.Date;
27 | import java.util.Scanner;
28 |
29 | public class phishingwebsite_finaltest {
30 | private static Instance[] instances = initializeInstances();
31 | private static Instance[] train_set = Arrays.copyOfRange(instances, 0, 6633);
32 | private static Instance[] test_set = Arrays.copyOfRange(instances, 8844, 11055);
33 |
34 | private static DataSet set = new DataSet(train_set);
35 |
36 | private static int inputLayer = 46, hiddenLayer=50, outputLayer = 1, trainingIterations = 5000;
37 | private static BackPropagationNetworkFactory factory = new BackPropagationNetworkFactory();
38 |
39 | private static ErrorMeasure measure = new SumOfSquaresError();
40 |
41 | private static BackPropagationNetwork networks[] = new BackPropagationNetwork[3];
42 | private static NeuralNetworkOptimizationProblem[] nnop = new NeuralNetworkOptimizationProblem[3];
43 |
44 | private static OptimizationAlgorithm[] oa = new OptimizationAlgorithm[3];
45 | private static String[] oaNames = {"RHC", "SA", "GA"};
46 | private static String results = "";
47 |
48 | private static DecimalFormat df = new DecimalFormat("0.000");
49 |
50 |
51 |
52 | public static void write_output_to_file(String output_dir, String file_name, String results, boolean final_result) {
53 | try {
54 | if (final_result) {
55 | String augmented_output_dir = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
56 | String full_path = augmented_output_dir + "/" + file_name;
57 | Path p = Paths.get(full_path);
58 | if (Files.notExists(p)) {
59 | Files.createDirectories(p.getParent());
60 | }
61 | PrintWriter pwtr = new PrintWriter(new BufferedWriter(new FileWriter(full_path, true)));
62 | synchronized (pwtr) {
63 | pwtr.println(results);
64 | pwtr.close();
65 | }
66 | }
67 | else {
68 | String full_path = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file_name;
69 | Path p = Paths.get(full_path);
70 | Files.createDirectories(p.getParent());
71 | Files.write(p, results.getBytes());
72 | }
73 | }
74 | catch (Exception e) {
75 | e.printStackTrace();
76 | }
77 |
78 | }
79 |
80 |
81 |
82 | public static void main(String[] args) {
83 |
84 | String final_result = "";
85 |
86 |
87 | for(int i = 0; i < oa.length; i++) {
88 | networks[i] = factory.createClassificationNetwork(
89 | new int[] {inputLayer, hiddenLayer, outputLayer});
90 | nnop[i] = new NeuralNetworkOptimizationProblem(set, networks[i], measure);
91 | }
92 |
93 | oa[0] = new RandomizedHillClimbing(nnop[0]);
94 | oa[1] = new SimulatedAnnealing(1e3, .4, nnop[1]);
95 | oa[2] = new StandardGeneticAlgorithm(75, 25, 1, nnop[2]);
96 |
97 | int[] iterations = {10,100,500,1000,2500,5000};
98 |
99 | for (int trainingIterations : iterations) {
100 | results = "";
101 | for (int i = 0; i < oa.length; i++) {
102 | double start = System.nanoTime(), end, trainingTime, testingTime, correct = 0, incorrect = 0;
103 | train(oa[i], networks[i], oaNames[i]); //trainer.train();
104 | end = System.nanoTime();
105 | trainingTime = end - start;
106 | trainingTime /= Math.pow(10, 9);
107 |
108 | Instance optimalInstance = oa[i].getOptimal();
109 | networks[i].setWeights(optimalInstance.getData());
110 |
111 | // Calculate Training Set Statistics //
112 | double predicted, actual;
113 | start = System.nanoTime();
114 | for (int j = 0; j < train_set.length; j++) {
115 | networks[i].setInputValues(train_set[j].getData());
116 | networks[i].run();
117 |
118 | actual = Double.parseDouble(train_set[j].getLabel().toString());
119 | predicted = Double.parseDouble(networks[i].getOutputValues().toString());
120 |
121 | // System.out.println("actual is " + actual);
122 | // System.out.println("predicted is " + predicted);
123 |
124 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
125 |
126 | }
127 | end = System.nanoTime();
128 | testingTime = end - start;
129 | testingTime /= Math.pow(10, 9);
130 |
131 | results += "\nTrain Results for " + oaNames[i] + ": \nCorrectly classified " + correct + " instances." +
132 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
133 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
134 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
135 |
136 | final_result = oaNames[i] + "," + trainingIterations + "," + "training accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
137 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
138 | "," + df.format(testingTime);
139 | write_output_to_file("Optimization_Results", "phishing_results.csv", final_result, true);
140 |
141 | // Calculate Test Set Statistics //
142 | start = System.nanoTime();
143 | correct = 0;
144 | incorrect = 0;
145 | for (int j = 0; j < test_set.length; j++) {
146 | networks[i].setInputValues(test_set[j].getData());
147 | networks[i].run();
148 |
149 | actual = Double.parseDouble(test_set[j].getLabel().toString());
150 | predicted = Double.parseDouble(networks[i].getOutputValues().toString());
151 |
152 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
153 | }
154 | end = System.nanoTime();
155 | testingTime = end - start;
156 | testingTime /= Math.pow(10, 9);
157 |
158 | results += "\nTest Results for " + oaNames[i] + ": \nCorrectly classified " + correct + " instances." +
159 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
160 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
161 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
162 |
163 | final_result = oaNames[i] + "," + trainingIterations + "," + "testing accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
164 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
165 | "," + df.format(testingTime);
166 | write_output_to_file("Optimization_Results", "phishing_results.csv", final_result, true);
167 | }
168 | System.out.println("results for iteration: " + trainingIterations + "---------------------------");
169 | System.out.println(results);
170 | }
171 | }
172 |
173 | private static void train(OptimizationAlgorithm oa, BackPropagationNetwork network, String oaName) {
174 | System.out.println("\nError results for " + oaName + "\n---------------------------");
175 |
176 | for(int i = 0; i < trainingIterations; i++) {
177 | oa.train();
178 |
179 | double train_error = 0;
180 | for(int j = 0; j < train_set.length; j++) {
181 | network.setInputValues(train_set[j].getData());
182 | network.run();
183 |
184 | Instance output = train_set[j].getLabel(), example = new Instance(network.getOutputValues());
185 | example.setLabel(new Instance(Double.parseDouble(network.getOutputValues().toString())));
186 | train_error += measure.value(output, example);
187 | }
188 |
189 |
190 |
191 | //System.out.println("training error :" + df.format(train_error)+", testing error: "+df.format(test_error));
192 | }
193 | }
194 |
195 | private static Instance[] initializeInstances() {
196 |
197 | double[][][] attributes = new double[11055][][];
198 |
199 | try {
200 | BufferedReader br = new BufferedReader(new FileReader(new File("C:/Users/westy/OneDrive/01 - CS 7641 - Machine Learning/HW2/PhishingWebsitesData_preprocessed.csv")));
201 |
202 | //for each sample
203 | for(int i = 0; i < attributes.length; i++) {
204 | Scanner scan = new Scanner(br.readLine());
205 | scan.useDelimiter(",");
206 |
207 | attributes[i] = new double[2][];
208 | attributes[i][0] = new double[46]; // 16 attributes
209 | attributes[i][1] = new double[1]; // classification
210 |
211 | // read features
212 | for(int j = 0; j < 46; j++)
213 | attributes[i][0][j] = Double.parseDouble(scan.next());
214 |
215 | attributes[i][1][0] = Double.parseDouble(scan.next());
216 | //System.out.println(attributes[i][1][0]);
217 |
218 | }
219 | }
220 | catch(Exception e) {
221 | e.printStackTrace();
222 | }
223 |
224 | Instance[] instances = new Instance[attributes.length];
225 |
226 | for(int i = 0; i < instances.length; i++) {
227 | instances[i] = new Instance(attributes[i][0]);
228 | instances[i].setLabel(new Instance(attributes[i][1][0]));
229 | }
230 |
231 | return instances;
232 | }
233 | }
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/ContinuousPeaks_Toy.java:
--------------------------------------------------------------------------------
1 | package opt.test;
2 |
3 | import java.io.BufferedWriter;
4 | import java.io.FileWriter;
5 | import java.io.PrintWriter;
6 | import java.nio.file.Files;
7 | import java.nio.file.Path;
8 | import java.nio.file.Paths;
9 | import java.text.SimpleDateFormat;
10 | import java.util.Arrays;
11 | import java.util.Date;
12 |
13 | import dist.DiscreteDependencyTree;
14 | import dist.DiscreteUniformDistribution;
15 | import dist.Distribution;
16 |
17 | import opt.DiscreteChangeOneNeighbor;
18 | import opt.EvaluationFunction;
19 | import opt.GenericHillClimbingProblem;
20 | import opt.HillClimbingProblem;
21 | import opt.NeighborFunction;
22 | import opt.RandomizedHillClimbing;
23 | import opt.SimulatedAnnealing;
24 | import opt.example.*;
25 | import opt.ga.CrossoverFunction;
26 | import opt.ga.DiscreteChangeOneMutation;
27 | import opt.ga.SingleCrossOver;
28 | import opt.ga.GenericGeneticAlgorithmProblem;
29 | import opt.ga.GeneticAlgorithmProblem;
30 | import opt.ga.MutationFunction;
31 | import opt.ga.StandardGeneticAlgorithm;
32 | import opt.prob.GenericProbabilisticOptimizationProblem;
33 | import opt.prob.MIMIC;
34 | import opt.prob.ProbabilisticOptimizationProblem;
35 | import shared.FixedIterationTrainer;
36 |
37 | /**
38 | *
39 | * @author Andrew Guillory gtg008g@mail.gatech.edu
40 | * @version 1.0
41 | */
42 | public class ContinuousPeaks_Toy {
43 | /** The n value */
44 | private static final int N = 60;
45 | /** The t value */
46 | private static final int T = N / 10;
47 |
48 | public static void write_output_to_file(String output_dir, String file_name, String results, boolean final_result) {
49 | try {
50 | if (final_result) {
51 | String augmented_output_dir = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
52 | String full_path = augmented_output_dir + "/" + file_name;
53 | Path p = Paths.get(full_path);
54 | if (Files.notExists(p)) {
55 | Files.createDirectories(p.getParent());
56 | }
57 | PrintWriter pwtr = new PrintWriter(new BufferedWriter(new FileWriter(full_path, true)));
58 | synchronized (pwtr) {
59 | pwtr.println(results);
60 | pwtr.close();
61 | }
62 | }
63 | else {
64 | String full_path = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file_name;
65 | Path p = Paths.get(full_path);
66 | Files.createDirectories(p.getParent());
67 | Files.write(p, results.getBytes());
68 | }
69 | }
70 | catch (Exception e) {
71 | e.printStackTrace();
72 | }
73 |
74 | }
75 |
76 | public static void main(String[] args) {
77 |
78 |
79 | /*Editted for Clock Time*/
80 |
81 | double start, end, time;
82 | int[] iters = {10, 100, 1000, 5000, 10000, 25000, 50000, 100000, 250000};
83 | //int[] iters = {10};
84 | int testRuns = 5;
85 |
86 | for (int iter : iters) {
87 |
88 | int sum_rhc = 0;
89 | int sum_sa = 0;
90 | int sum_ga = 0;
91 | int sum_mimic = 0;
92 |
93 | double time_rhc = 0;
94 | double time_sa = 0;
95 | double time_ga = 0;
96 | double time_mimic = 0;
97 |
98 | for (int j = 0; j < testRuns; j++) {
99 | int[] ranges = new int[N];
100 | Arrays.fill(ranges, 2);
101 | EvaluationFunction ef = new ContinuousPeaksEvaluationFunction(T);
102 | Distribution odd = new DiscreteUniformDistribution(ranges);
103 | NeighborFunction nf = new DiscreteChangeOneNeighbor(ranges);
104 | MutationFunction mf = new DiscreteChangeOneMutation(ranges);
105 | CrossoverFunction cf = new SingleCrossOver();
106 | Distribution df = new DiscreteDependencyTree(.1, ranges);
107 | HillClimbingProblem hcp = new GenericHillClimbingProblem(ef, odd, nf);
108 | GeneticAlgorithmProblem gap = new GenericGeneticAlgorithmProblem(ef, odd, mf, cf);
109 | ProbabilisticOptimizationProblem pop = new GenericProbabilisticOptimizationProblem(ef, odd, df);
110 |
111 |
112 | start = System.nanoTime();
113 | RandomizedHillClimbing rhc = new RandomizedHillClimbing(hcp);
114 | FixedIterationTrainer fit = new FixedIterationTrainer(rhc, iter);
115 | fit.train();
116 | end = System.nanoTime();
117 | time = end - start;
118 | time /= Math.pow(10, 9);
119 | sum_rhc += ef.value(rhc.getOptimal());
120 | time_rhc += time;
121 | //System.out.println("rhc: " + ef.value(rhc.getOptimal()));
122 | //System.out.println(time);
123 |
124 | start = System.nanoTime();
125 | SimulatedAnnealing sa = new SimulatedAnnealing(1E11, .95, hcp);
126 | fit = new FixedIterationTrainer(sa, iter);
127 | fit.train();
128 | end = System.nanoTime();
129 | time = end - start;
130 | time /= Math.pow(10, 9);
131 | sum_sa += ef.value(sa.getOptimal());
132 | time_sa += time;
133 | //System.out.println("sa: " + ef.value(sa.getOptimal()));
134 | //System.out.println(time);
135 |
136 |
137 | start = System.nanoTime();
138 | StandardGeneticAlgorithm ga = new StandardGeneticAlgorithm(200, 20, 10, gap);
139 | fit = new FixedIterationTrainer(ga, iter);
140 | fit.train();
141 | end = System.nanoTime();
142 | time = end - start;
143 | time /= Math.pow(10, 9);
144 | sum_ga += ef.value(ga.getOptimal());
145 | time_ga += time;
146 | //System.out.println("ga: " + ef.value(ga.getOptimal()));
147 | //System.out.println(time);
148 |
149 |
150 |
151 | start = System.nanoTime();
152 | MIMIC mimic = new MIMIC(200, 20, pop);
153 | fit = new FixedIterationTrainer(mimic, iter);
154 | fit.train();
155 | end = System.nanoTime();
156 | time = end - start;
157 | time /= Math.pow(10, 9);
158 | sum_mimic += ef.value(mimic.getOptimal());
159 | time_mimic += time;
160 | //System.out.println("Mimic: " + ef.value(mimic.getOptimal()));
161 | //System.out.println(time);
162 |
163 | }
164 |
165 | int average_rhc = sum_rhc / testRuns;
166 | int average_sa = sum_sa / testRuns;
167 | int average_ga = sum_ga / testRuns;
168 | int average_mimic = sum_mimic / testRuns;
169 |
170 | double averagetime_rhc = time_rhc / testRuns;
171 | double averagetime_sa = time_sa / testRuns;
172 | double averagetime_ga = time_ga / testRuns;
173 | double averagetime_mimic = time_mimic / testRuns;
174 |
175 | System.out.println("##############");
176 | System.out.println("this is iteration " + iter);
177 | System.out.println("rhc average is " + average_rhc + ", time average is " + averagetime_rhc);
178 | System.out.println("sa average is " + average_sa + ", time average is " + averagetime_sa);
179 | System.out.println("ga average is " + average_ga + ", time average is " + averagetime_ga);
180 | System.out.println("mimic average is " + average_mimic + ", time average is " + averagetime_mimic);
181 |
182 | String final_result = "";
183 | final_result = "rhc" + "," + iter + "," + Integer.toString(average_rhc) + "," + Double.toString(averagetime_rhc) + "," +
184 | "sa" + "," + iter + "," + Integer.toString(average_sa) + "," + Double.toString(averagetime_sa) + "," +
185 | "ga" + "," + iter + "," + Integer.toString(average_ga) + "," + Double.toString(averagetime_ga) + "," +
186 | "mimic" + "," + iter + "," + Integer.toString(average_mimic) + "," + Double.toString(averagetime_mimic);
187 |
188 | write_output_to_file("Optimization_Results", "continuousPeaks_results.csv", final_result, true);
189 | }
190 | /**
191 | double[] coolings = {0.05, 0.15, 0.25,0.35,0.45,0.55,0.65,0.75,0.85,0.95};
192 | for (int i = 0; i < coolings.length; i++){
193 | int sum_sa = 0;
194 | double time_sa = 0;
195 | for (int j = 0; j < testRuns; j++) {
196 | int[] ranges = new int[N];
197 | Arrays.fill(ranges, 2);
198 | EvaluationFunction ef = new ContinuousPeaksEvaluationFunction(T);
199 | Distribution odd = new DiscreteUniformDistribution(ranges);
200 | NeighborFunction nf = new DiscreteChangeOneNeighbor(ranges);
201 | MutationFunction mf = new DiscreteChangeOneMutation(ranges);
202 | CrossoverFunction cf = new SingleCrossOver();
203 | Distribution df = new DiscreteDependencyTree(.1, ranges);
204 | HillClimbingProblem hcp = new GenericHillClimbingProblem(ef, odd, nf);
205 | GeneticAlgorithmProblem gap = new GenericGeneticAlgorithmProblem(ef, odd, mf, cf);
206 | ProbabilisticOptimizationProblem pop = new GenericProbabilisticOptimizationProblem(ef, odd, df);
207 | //System.out.println("this is test run # " + j);
208 | start = System.nanoTime();
209 | SimulatedAnnealing sa = new SimulatedAnnealing(1E11, coolings[i], hcp);
210 | FixedIterationTrainer fit = new FixedIterationTrainer(sa, 5000);
211 | fit.train();
212 | end = System.nanoTime();
213 | time = end - start;
214 | time /= Math.pow(10, 9);
215 | sum_sa += ef.value(sa.getOptimal());
216 | time_sa += time;
217 | //System.out.println("sa: " + ef.value(sa.getOptimal()));
218 | //System.out.println(time);
219 | }
220 | int average_sa = sum_sa / testRuns;
221 | double averagetime_sa = time_sa / testRuns;
222 | System.out.println("##############");
223 | System.out.println("this is cooling " + coolings[i]);
224 | System.out.println("mimic average is " + average_sa + ", time average is " + averagetime_sa);
225 | String final_result = "";
226 | final_result =
227 | "mimic" + "," + Double.toString(coolings[i]) + "," + Integer.toString(average_sa) + "," + Double.toString(averagetime_sa);
228 | write_output_to_file("Optimization_Results", "continuousPeaks_sa_results.csv", final_result, true);
229 | }
230 | **/
231 | }
232 | }
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/phishing_ga_val.java:
--------------------------------------------------------------------------------
1 | package opt.test;
2 |
3 | /*
4 | * Created by Kyle West on 9/29/2018.
5 | * Adapted from PhishingWebsites.java by Daniel Cai (in turn adapted from AbaloneTest.java by Hannah Lau)
6 | */
7 | import func.nn.backprop.BackPropagationNetwork;
8 | import func.nn.backprop.BackPropagationNetworkFactory;
9 | import func.nn.backprop.BatchBackPropagationTrainer;
10 | import func.nn.backprop.RPROPUpdateRule;
11 | import opt.OptimizationAlgorithm;
12 | import opt.RandomizedHillClimbing;
13 | import opt.SimulatedAnnealing;
14 | import opt.example.NeuralNetworkOptimizationProblem;
15 | import opt.ga.StandardGeneticAlgorithm;
16 | import shared.*;
17 | import func.nn.activation.*;
18 |
19 | import java.io.*;
20 | import java.nio.file.Files;
21 | import java.nio.file.Path;
22 | import java.nio.file.Paths;
23 | import java.text.DecimalFormat;
24 | import java.text.SimpleDateFormat;
25 | import java.util.Arrays;
26 | import java.util.Date;
27 | import java.util.Scanner;
28 |
29 | public class phishing_ga_val {
30 | private static Instance[] instances = initializeInstances();
31 | private static Instance[] train_set = Arrays.copyOfRange(instances, 0, 6633);
32 | private static Instance[] test_set = Arrays.copyOfRange(instances, 6633, 8844);
33 |
34 | private static DataSet set = new DataSet(train_set);
35 |
36 | private static int inputLayer = 46, hiddenLayer=50, outputLayer = 1;
37 | private static BackPropagationNetworkFactory factory = new BackPropagationNetworkFactory();
38 |
39 | private static ErrorMeasure measure = new SumOfSquaresError();
40 |
41 | private static BackPropagationNetwork networks[] = new BackPropagationNetwork[1];
42 | private static NeuralNetworkOptimizationProblem[] nnop = new NeuralNetworkOptimizationProblem[1];
43 |
44 | private static OptimizationAlgorithm[] oa = new OptimizationAlgorithm[1];
45 | private static String[] oaNames = {"GA"};
46 | private static String results = "";
47 |
48 | private static DecimalFormat df = new DecimalFormat("0.000");
49 |
50 |
51 |
52 | public static void write_output_to_file(String output_dir, String file_name, String results, boolean final_result) {
53 | try {
54 | if (final_result) {
55 | String augmented_output_dir = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
56 | String full_path = augmented_output_dir + "/" + file_name;
57 | Path p = Paths.get(full_path);
58 | if (Files.notExists(p)) {
59 | Files.createDirectories(p.getParent());
60 | }
61 | PrintWriter pwtr = new PrintWriter(new BufferedWriter(new FileWriter(full_path, true)));
62 | synchronized (pwtr) {
63 | pwtr.println(results);
64 | pwtr.close();
65 | }
66 | }
67 | else {
68 | String full_path = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file_name;
69 | Path p = Paths.get(full_path);
70 | Files.createDirectories(p.getParent());
71 | Files.write(p, results.getBytes());
72 | }
73 | }
74 | catch (Exception e) {
75 | e.printStackTrace();
76 | }
77 |
78 | }
79 |
80 |
81 |
82 | public static void main(String[] args) {
83 |
84 | String final_result = "";
85 |
86 |
87 | for(int i = 0; i < oa.length; i++) {
88 | networks[i] = factory.createClassificationNetwork(
89 | new int[] {inputLayer, hiddenLayer, outputLayer});
90 | nnop[i] = new NeuralNetworkOptimizationProblem(set, networks[i], measure);
91 | }
92 |
93 |
94 | int[] iterations = {10,100,500,1000,2500,5000};
95 |
96 | int[] population = {10,25,50,75,100,125};
97 | int[] mate = {5,10,30,40,50,60};
98 | int[] mute = {2,5,10,15,20,25};
99 |
100 | for (int trainingIterations : iterations) {
101 | results = "";
102 | for (int q = 0; q < population.length; q++) {
103 | double start = System.nanoTime(), end, trainingTime, testingTime, correct = 0, incorrect = 0;
104 | oa[0] = new StandardGeneticAlgorithm(population[q], mate[q], mute[q], nnop[0]);
105 | train(oa[0], networks[0], oaNames[0], trainingIterations); //trainer.train();
106 | end = System.nanoTime();
107 | trainingTime = end - start;
108 | trainingTime /= Math.pow(10, 9);
109 |
110 | Instance optimalInstance = oa[0].getOptimal();
111 | networks[0].setWeights(optimalInstance.getData());
112 |
113 | // Calculate Training Set Statistics //
114 | double predicted, actual;
115 | start = System.nanoTime();
116 | for (int j = 0; j < train_set.length; j++) {
117 | networks[0].setInputValues(train_set[j].getData());
118 | networks[0].run();
119 |
120 | //predicted = Double.parseDouble(train_set[j].getLabel().toString());
121 | //actual = Double.parseDouble(networks[i].getOutputValues().toString());
122 |
123 | actual = Double.parseDouble(train_set[j].getLabel().toString());
124 | predicted = Double.parseDouble(networks[0].getOutputValues().toString());
125 |
126 |
127 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
128 |
129 | }
130 | end = System.nanoTime();
131 | testingTime = end - start;
132 | testingTime /= Math.pow(10, 9);
133 |
134 | results += "\nTrain Results for GA:" + "," + population[q] + "," + mate[q] + "," + mute[q] + "," + ": \nCorrectly classified " + correct + " instances." +
135 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
136 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
137 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
138 |
139 | final_result = oaNames[0] + "," + trainingIterations + "," + population[q] + "," + mate[q] + "," + mute[q] + "," + "training accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
140 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
141 | "," + df.format(testingTime);
142 | write_output_to_file("Optimization_Results", "phishing_results_ga.csv", final_result, true);
143 |
144 | // Calculate Test Set Statistics //
145 | start = System.nanoTime();
146 | correct = 0;
147 | incorrect = 0;
148 | for (int j = 0; j < test_set.length; j++) {
149 | networks[0].setInputValues(test_set[j].getData());
150 | networks[0].run();
151 |
152 | actual = Double.parseDouble(test_set[j].getLabel().toString());
153 | predicted = Double.parseDouble(networks[0].getOutputValues().toString());
154 |
155 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
156 | }
157 | end = System.nanoTime();
158 | testingTime = end - start;
159 | testingTime /= Math.pow(10, 9);
160 |
161 | results += "\nTest Results for GA: " + "," + population[q] + "," + mate[q] + "," + mute[q] + "," + ": \nCorrectly classified " + correct + " instances." +
162 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
163 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
164 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
165 |
166 | final_result = oaNames[0] + "," + trainingIterations + "," + population[q] + "," + mate[q] + "," + mute[q] + "," + "testing accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
167 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
168 | "," + df.format(testingTime);
169 | write_output_to_file("Optimization_Results", "phishing_results_ga.csv", final_result, true);
170 | }
171 | System.out.println("results for iteration: " + trainingIterations + "---------------------------");
172 | System.out.println(results);
173 | }
174 | }
175 |
176 | private static void train(OptimizationAlgorithm oa, BackPropagationNetwork network, String oaName, int iteration) {
177 | //System.out.println("\nError results for " + oaName + "\n---------------------------");
178 | int trainingIterations = iteration;
179 | for(int i = 0; i < trainingIterations; i++) {
180 | oa.train();
181 |
182 | double train_error = 0;
183 | for(int j = 0; j < train_set.length; j++) {
184 | network.setInputValues(train_set[j].getData());
185 | network.run();
186 |
187 | Instance output = train_set[j].getLabel(), example = new Instance(network.getOutputValues());
188 | example.setLabel(new Instance(Double.parseDouble(network.getOutputValues().toString())));
189 | train_error += measure.value(output, example);
190 | }
191 |
192 |
193 | //System.out.println("training error :" + df.format(train_error)+", testing error: "+df.format(test_error));
194 | }
195 | }
196 |
197 | private static Instance[] initializeInstances() {
198 |
199 | double[][][] attributes = new double[11055][][];
200 |
201 | try {
202 | BufferedReader br = new BufferedReader(new FileReader(new File("C:/Users/westy/OneDrive/01 - CS 7641 - Machine Learning/HW2/PhishingWebsitesData_preprocessed.csv")));
203 |
204 | //for each sample
205 | for(int i = 0; i < attributes.length; i++) {
206 | Scanner scan = new Scanner(br.readLine());
207 | scan.useDelimiter(",");
208 |
209 | attributes[i] = new double[2][];
210 | attributes[i][0] = new double[46]; // 46 attributes
211 | attributes[i][1] = new double[1]; // classification
212 |
213 | // read features
214 | for(int j = 0; j < 46; j++)
215 | attributes[i][0][j] = Double.parseDouble(scan.next());
216 |
217 | attributes[i][1][0] = Double.parseDouble(scan.next());
218 | //System.out.println(attributes[i][1][0]);
219 |
220 | }
221 | }
222 | catch(Exception e) {
223 | e.printStackTrace();
224 | }
225 |
226 | Instance[] instances = new Instance[attributes.length];
227 |
228 | for(int i = 0; i < instances.length; i++) {
229 | instances[i] = new Instance(attributes[i][0]);
230 | instances[i].setLabel(new Instance(attributes[i][1][0]));
231 | }
232 |
233 | return instances;
234 | }
235 | }
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/phishing_sa_val.java:
--------------------------------------------------------------------------------
1 | package opt.test;
2 |
3 | /*
4 | * Created by Kyle West on 9/29/2018.
5 | * Adapted from PhishingWebsites.java by Daniel Cai (in turn adapted from AbaloneTest.java by Hannah Lau)
6 | */
7 | import func.nn.backprop.BackPropagationNetwork;
8 | import func.nn.backprop.BackPropagationNetworkFactory;
9 | import func.nn.backprop.BatchBackPropagationTrainer;
10 | import func.nn.backprop.RPROPUpdateRule;
11 | import opt.OptimizationAlgorithm;
12 | import opt.RandomizedHillClimbing;
13 | import opt.SimulatedAnnealing;
14 | import opt.example.NeuralNetworkOptimizationProblem;
15 | import opt.ga.StandardGeneticAlgorithm;
16 | import shared.*;
17 | import func.nn.activation.*;
18 |
19 | import java.io.*;
20 | import java.nio.file.Files;
21 | import java.nio.file.Path;
22 | import java.nio.file.Paths;
23 | import java.text.DecimalFormat;
24 | import java.text.SimpleDateFormat;
25 | import java.util.Arrays;
26 | import java.util.Date;
27 | import java.util.Scanner;
28 | public class phishing_sa_val {
29 | private static Instance[] instances = initializeInstances();
30 | private static Instance[] train_set = Arrays.copyOfRange(instances, 0, 6633);
31 | private static Instance[] test_set = Arrays.copyOfRange(instances, 6633, 8844);
32 |
33 | private static DataSet set = new DataSet(train_set);
34 |
35 | private static int inputLayer = 46, hiddenLayer=50, outputLayer = 1;
36 | private static BackPropagationNetworkFactory factory = new BackPropagationNetworkFactory();
37 |
38 | private static ErrorMeasure measure = new SumOfSquaresError();
39 |
40 | private static BackPropagationNetwork networks[] = new BackPropagationNetwork[1];
41 | private static NeuralNetworkOptimizationProblem[] nnop = new NeuralNetworkOptimizationProblem[1];
42 |
43 | private static OptimizationAlgorithm[] oa = new OptimizationAlgorithm[1];
44 | private static String[] oaNames = {"SA"};
45 | private static String results = "";
46 |
47 | private static DecimalFormat df = new DecimalFormat("0.000");
48 |
49 |
50 |
51 | public static void write_output_to_file(String output_dir, String file_name, String results, boolean final_result) {
52 | try {
53 | if (final_result) {
54 | String augmented_output_dir = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
55 | String full_path = augmented_output_dir + "/" + file_name;
56 | Path p = Paths.get(full_path);
57 | if (Files.notExists(p)) {
58 | Files.createDirectories(p.getParent());
59 | }
60 | PrintWriter pwtr = new PrintWriter(new BufferedWriter(new FileWriter(full_path, true)));
61 | synchronized (pwtr) {
62 | pwtr.println(results);
63 | pwtr.close();
64 | }
65 | }
66 | else {
67 | String full_path = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file_name;
68 | Path p = Paths.get(full_path);
69 | Files.createDirectories(p.getParent());
70 | Files.write(p, results.getBytes());
71 | }
72 | }
73 | catch (Exception e) {
74 | e.printStackTrace();
75 | }
76 |
77 | }
78 |
79 |
80 |
81 | public static void main(String[] args) {
82 |
83 | String final_result = "";
84 |
85 |
86 | for(int i = 0; i < oa.length; i++) {
87 | networks[i] = factory.createClassificationNetwork(
88 | new int[] {inputLayer, hiddenLayer, outputLayer});
89 | nnop[i] = new NeuralNetworkOptimizationProblem(set, networks[i], measure);
90 | }
91 |
92 |
93 | int[] iterations = {10,100,500,1000,2500,5000};
94 |
95 | double[] coolings = {.05,.2,.35,.5,.65,.8,.95};
96 | double[] temperatures = {1e1,1e2,1e3,1e4,1e5,1e6,1e7};
97 |
98 | for (int trainingIterations : iterations) {
99 | results = "";
100 | for (int q = 0; q < coolings.length; q++) {
101 | //for (double cooling : coolings) {
102 | double start = System.nanoTime(), end, trainingTime, testingTime, correct = 0, incorrect = 0;
103 | oa[0] = new SimulatedAnnealing(temperatures[q], coolings[q], nnop[0]);
104 | train(oa[0], networks[0], oaNames[0], trainingIterations); //trainer.train();
105 | end = System.nanoTime();
106 | trainingTime = end - start;
107 | trainingTime /= Math.pow(10, 9);
108 |
109 | Instance optimalInstance = oa[0].getOptimal();
110 | networks[0].setWeights(optimalInstance.getData());
111 |
112 | // Calculate Training Set Statistics //
113 | double predicted, actual;
114 | start = System.nanoTime();
115 | for (int j = 0; j < train_set.length; j++) {
116 | networks[0].setInputValues(train_set[j].getData());
117 | networks[0].run();
118 |
119 | //predicted = Double.parseDouble(train_set[j].getLabel().toString());
120 | //actual = Double.parseDouble(networks[i].getOutputValues().toString());
121 |
122 | actual = Double.parseDouble(train_set[j].getLabel().toString());
123 | predicted = Double.parseDouble(networks[0].getOutputValues().toString());
124 |
125 | //System.out.println("actual is " + actual);
126 | //System.out.println("predicted is " + predicted);
127 |
128 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
129 |
130 | }
131 | end = System.nanoTime();
132 | testingTime = end - start;
133 | testingTime /= Math.pow(10, 9);
134 |
135 | results += "\nTrain Results for SA:" + "," + coolings[q] + "," + temperatures[q] + "," + ": \nCorrectly classified " + correct + " instances." +
136 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
137 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
138 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
139 |
140 | final_result = oaNames[0] + "," + trainingIterations + "," + coolings[q] + "," + temperatures[q] + "," + "training accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
141 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
142 | "," + df.format(testingTime);
143 | write_output_to_file("Optimization_Results", "phishing_results_sa.csv", final_result, true);
144 |
145 | // Calculate Test Set Statistics //
146 | start = System.nanoTime();
147 | correct = 0;
148 | incorrect = 0;
149 | for (int j = 0; j < test_set.length; j++) {
150 | networks[0].setInputValues(test_set[j].getData());
151 | networks[0].run();
152 |
153 | actual = Double.parseDouble(test_set[j].getLabel().toString());
154 | predicted = Double.parseDouble(networks[0].getOutputValues().toString());
155 |
156 | double trash = Math.abs(predicted - actual) < 0.5 ? correct++ : incorrect++;
157 | }
158 | end = System.nanoTime();
159 | testingTime = end - start;
160 | testingTime /= Math.pow(10, 9);
161 |
162 | results += "\nTest Results for SA: " + coolings[q] + "," + temperatures[q] + ": \nCorrectly classified " + correct + " instances." +
163 | "\nIncorrectly classified " + incorrect + " instances.\nPercent correctly classified: "
164 | + df.format(correct / (correct + incorrect) * 100) + "%\nTraining time: " + df.format(trainingTime)
165 | + " seconds\nTesting time: " + df.format(testingTime) + " seconds\n";
166 |
167 | final_result = oaNames[0] + "," + trainingIterations + "," + coolings[q] + "," + temperatures[q] + "," + "testing accuracy" + "," + df.format(correct / (correct + incorrect) * 100)
168 | + "," + "training time" + "," + df.format(trainingTime) + "," + "testing time" +
169 | "," + df.format(testingTime);
170 | write_output_to_file("Optimization_Results", "phishing_results_sa.csv", final_result, true);
171 | }
172 | System.out.println("results for iteration: " + trainingIterations + "---------------------------");
173 | System.out.println(results);
174 | }
175 | }
176 |
177 | private static void train(OptimizationAlgorithm oa, BackPropagationNetwork network, String oaName, int iteration) {
178 | //System.out.println("\nError results for " + oaName + "\n---------------------------");
179 | int trainingIterations = iteration;
180 | for(int i = 0; i < trainingIterations; i++) {
181 | oa.train();
182 |
183 | double train_error = 0;
184 | for(int j = 0; j < train_set.length; j++) {
185 | network.setInputValues(train_set[j].getData());
186 | network.run();
187 |
188 | Instance output = train_set[j].getLabel(), example = new Instance(network.getOutputValues());
189 | example.setLabel(new Instance(Double.parseDouble(network.getOutputValues().toString())));
190 | train_error += measure.value(output, example);
191 | }
192 |
193 |
194 | //System.out.println("training error :" + df.format(train_error)+", testing error: "+df.format(test_error));
195 | }
196 | }
197 |
198 | private static Instance[] initializeInstances() {
199 |
200 | double[][][] attributes = new double[11055][][];
201 |
202 | try {
203 | BufferedReader br = new BufferedReader(new FileReader(new File("C:/Users/westy/OneDrive/01 - CS 7641 - Machine Learning/HW2/PhishingWebsitesData_preprocessed.csv")));
204 |
205 | //for each sample
206 | for(int i = 0; i < attributes.length; i++) {
207 | Scanner scan = new Scanner(br.readLine());
208 | scan.useDelimiter(",");
209 |
210 | attributes[i] = new double[2][];
211 | attributes[i][0] = new double[46]; // 46 attributes
212 | attributes[i][1] = new double[1]; // classification
213 |
214 | // read features
215 | for(int j = 0; j < 46; j++)
216 | attributes[i][0][j] = Double.parseDouble(scan.next());
217 |
218 | attributes[i][1][0] = Double.parseDouble(scan.next());
219 | //System.out.println(attributes[i][1][0]);
220 |
221 | }
222 | }
223 | catch(Exception e) {
224 | e.printStackTrace();
225 | }
226 |
227 | Instance[] instances = new Instance[attributes.length];
228 |
229 | for(int i = 0; i < instances.length; i++) {
230 | instances[i] = new Instance(attributes[i][0]);
231 | instances[i].setLabel(new Instance(attributes[i][1][0]));
232 | }
233 |
234 | return instances;
235 | }
236 | }
--------------------------------------------------------------------------------
/Assignment 2 Randomized Optimization/TravelingSalesman_Toy.java:
--------------------------------------------------------------------------------
1 | package opt.test;
2 |
3 | import java.io.BufferedWriter;
4 | import java.io.FileWriter;
5 | import java.io.PrintWriter;
6 | import java.nio.file.Files;
7 | import java.nio.file.Path;
8 | import java.nio.file.Paths;
9 | import java.text.SimpleDateFormat;
10 | import java.util.Arrays;
11 | import java.util.Date;
12 | import java.util.Random;
13 |
14 | import dist.DiscreteDependencyTree;
15 | import dist.DiscretePermutationDistribution;
16 | import dist.DiscreteUniformDistribution;
17 | import dist.Distribution;
18 |
19 | import opt.SwapNeighbor;
20 | import opt.GenericHillClimbingProblem;
21 | import opt.HillClimbingProblem;
22 | import opt.NeighborFunction;
23 | import opt.RandomizedHillClimbing;
24 | import opt.SimulatedAnnealing;
25 | import opt.example.*;
26 | import opt.ga.CrossoverFunction;
27 | import opt.ga.SwapMutation;
28 | import opt.ga.GenericGeneticAlgorithmProblem;
29 | import opt.ga.GeneticAlgorithmProblem;
30 | import opt.ga.MutationFunction;
31 | import opt.ga.StandardGeneticAlgorithm;
32 | import opt.prob.GenericProbabilisticOptimizationProblem;
33 | import opt.prob.MIMIC;
34 | import opt.prob.ProbabilisticOptimizationProblem;
35 | import shared.FixedIterationTrainer;
36 |
37 | /**
38 | *
39 | * @author Andrew Guillory gtg008g@mail.gatech.edu
40 | * @version 1.0
41 | */
42 | public class TravelingSalesman_Toy {
43 | /** The n value */
44 | private static final int N = 50;
45 |
46 | public static void write_output_to_file(String output_dir, String file_name, String results, boolean final_result) {
47 | try {
48 | if (final_result) {
49 | String augmented_output_dir = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
50 | String full_path = augmented_output_dir + "/" + file_name;
51 | Path p = Paths.get(full_path);
52 | if (Files.notExists(p)) {
53 | Files.createDirectories(p.getParent());
54 | }
55 | PrintWriter pwtr = new PrintWriter(new BufferedWriter(new FileWriter(full_path, true)));
56 | synchronized (pwtr) {
57 | pwtr.println(results);
58 | pwtr.close();
59 | }
60 | }
61 | else {
62 | String full_path = output_dir + "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/" + file_name;
63 | Path p = Paths.get(full_path);
64 | Files.createDirectories(p.getParent());
65 | Files.write(p, results.getBytes());
66 | }
67 | }
68 | catch (Exception e) {
69 | e.printStackTrace();
70 | }
71 |
72 | }
73 | public static void main(String[] args) {
74 |
75 |
76 |
77 | double start, end, time;
78 | int[] iters = {10, 100, 1000, 5000, 10000, 25000, 50000, 100000, 250000};
79 | //int[] iters = {10};
80 | int testRuns = 5;
81 |
82 | for (int iter : iters) {
83 |
84 | double sum_rhc = 0;
85 | double sum_sa = 0;
86 | double sum_ga = 0;
87 | double sum_mimic = 0;
88 |
89 | double time_rhc = 0;
90 | double time_sa = 0;
91 | double time_ga = 0;
92 | double time_mimic = 0;
93 |
94 | for (int j = 0; j < testRuns; j++) {
95 | Random random = new Random();
96 | // create the random points
97 | double[][] points = new double[N][2];
98 | for (int i = 0; i < points.length; i++) {
99 | points[i][0] = random.nextDouble();
100 | points[i][1] = random.nextDouble();
101 | }
102 | // for rhc, sa, and ga we use a permutation based encoding
103 | TravelingSalesmanEvaluationFunction ef = new TravelingSalesmanRouteEvaluationFunction(points);
104 | Distribution odd = new DiscretePermutationDistribution(N);
105 | NeighborFunction nf = new SwapNeighbor();
106 | MutationFunction mf = new SwapMutation();
107 | CrossoverFunction cf = new TravelingSalesmanCrossOver(ef);
108 | HillClimbingProblem hcp = new GenericHillClimbingProblem(ef, odd, nf);
109 | GeneticAlgorithmProblem gap = new GenericGeneticAlgorithmProblem(ef, odd, mf, cf);
110 |
111 |
112 | start = System.nanoTime();
113 | RandomizedHillClimbing rhc = new RandomizedHillClimbing(hcp);
114 | FixedIterationTrainer fit = new FixedIterationTrainer(rhc, iter);
115 | fit.train();
116 | end = System.nanoTime();
117 | time = end - start;
118 | time /= Math.pow(10, 9);
119 | sum_rhc += ef.value(rhc.getOptimal());
120 | time_rhc += time;
121 | //System.out.println("rhc: " + ef.value(rhc.getOptimal()));
122 | //System.out.println(time);
123 |
124 | start = System.nanoTime();
125 | SimulatedAnnealing sa = new SimulatedAnnealing(1E11, .95, hcp);
126 | fit = new FixedIterationTrainer(sa, iter);
127 | fit.train();
128 | end = System.nanoTime();
129 | time = end - start;
130 | time /= Math.pow(10, 9);
131 | sum_sa += ef.value(sa.getOptimal());
132 | time_sa += time;
133 | //System.out.println("sa: " + ef.value(sa.getOptimal()));
134 | //System.out.println(time);
135 |
136 | start = System.nanoTime();
137 | StandardGeneticAlgorithm ga = new StandardGeneticAlgorithm(200, 100, 10, gap);
138 | fit = new FixedIterationTrainer(ga, iter);
139 | fit.train();
140 | end = System.nanoTime();
141 | time = end - start;
142 | time /= Math.pow(10, 9);
143 | sum_ga += ef.value(ga.getOptimal());
144 | time_ga += time;
145 | //System.out.println("ga: " + ef.value(ga.getOptimal()));
146 | //System.out.println(time);
147 |
148 | start = System.nanoTime();
149 | // for mimic we use a sort encoding
150 | ef = new TravelingSalesmanSortEvaluationFunction(points);
151 | int[] ranges = new int[N];
152 | Arrays.fill(ranges, N);
153 | odd = new DiscreteUniformDistribution(ranges);
154 | Distribution df = new DiscreteDependencyTree(.1, ranges);
155 | ProbabilisticOptimizationProblem pop = new GenericProbabilisticOptimizationProblem(ef, odd, df);
156 |
157 | MIMIC mimic = new MIMIC(200, 20, pop);
158 | fit = new FixedIterationTrainer(mimic, iter);
159 | fit.train();
160 | //System.out.println(ef.value(mimic.getOptimal()));
161 | end = System.nanoTime();
162 | time = end - start;
163 | time /= Math.pow(10, 9);
164 | sum_mimic += ef.value(mimic.getOptimal());
165 | time_mimic += time;
166 | //System.out.println("Mimic: " + ef.value(mimic.getOptimal()));
167 | //System.out.println(time);
168 | }
169 |
170 | double average_rhc = sum_rhc / testRuns;
171 | double average_sa = sum_sa / testRuns;
172 | double average_ga = sum_ga / testRuns;
173 | double average_mimic = sum_mimic / testRuns;
174 |
175 | double averagetime_rhc = time_rhc / testRuns;
176 | double averagetime_sa = time_sa / testRuns;
177 | double averagetime_ga = time_ga / testRuns;
178 | double averagetime_mimic = time_mimic / testRuns;
179 |
180 | System.out.println("##############");
181 | System.out.println("this is iteration " + iter);
182 | System.out.println("rhc average is " + average_rhc + ", time average is " + averagetime_rhc);
183 | System.out.println("sa average is " + average_sa + ", time average is " + averagetime_sa);
184 | System.out.println("ga average is " + average_ga + ", time average is " + averagetime_ga);
185 | System.out.println("mimic average is " + average_mimic + ", time average is " + averagetime_mimic);
186 |
187 | String final_result = "";
188 | final_result = "rhc" + "," + iter + "," + Double.toString(average_rhc) + "," + Double.toString(averagetime_rhc) + "," +
189 | "sa" + "," + iter + "," + Double.toString(average_sa) + "," + Double.toString(averagetime_sa) + "," +
190 | "ga" + "," + iter + "," + Double.toString(average_ga) + "," + Double.toString(averagetime_ga) + "," +
191 | "mimic" + "," + iter + "," + Double.toString(average_mimic) + "," + Double.toString(averagetime_mimic);
192 |
193 | write_output_to_file("Optimization_Results", "travelingsalesman_results.csv", final_result, true);
194 | }
195 |
196 | /**
197 | int[] population = {10,20,50,100,200,500};
198 | int[] mate = {5,10,25,50,150,350};
199 | int[] mute = {2,5,10,10,20,50};
200 |
201 |
202 | double sum_ga = 0;
203 |
204 |
205 | double time_ga = 0;
206 |
207 |
208 | for (int i = 0; i < population.length; i++) {
209 | for (int j = 0; j < testRuns; j++) {
210 | Random random = new Random();
211 | // create the random points
212 | double[][] points = new double[N][2];
213 | for (int q = 0; q < points.length; q++) {
214 | points[q][0] = random.nextDouble();
215 | points[q][1] = random.nextDouble();
216 | }
217 | // for rhc, sa, and ga we use a p
218 | // ermutation based encoding
219 | TravelingSalesmanEvaluationFunction ef = new TravelingSalesmanRouteEvaluationFunction(points);
220 | Distribution odd = new DiscretePermutationDistribution(N);
221 | NeighborFunction nf = new SwapNeighbor();
222 | MutationFunction mf = new SwapMutation();
223 | CrossoverFunction cf = new TravelingSalesmanCrossOver(ef);
224 | HillClimbingProblem hcp = new GenericHillClimbingProblem(ef, odd, nf);
225 | GeneticAlgorithmProblem gap = new GenericGeneticAlgorithmProblem(ef, odd, mf, cf);
226 |
227 | start = System.nanoTime();
228 | StandardGeneticAlgorithm ga = new StandardGeneticAlgorithm(population[i], mate[i], mute[i], gap);
229 | FixedIterationTrainer fit = new FixedIterationTrainer(ga, 5000);
230 | fit.train();
231 | end = System.nanoTime();
232 | time = end - start;
233 | time /= Math.pow(10, 9);
234 | sum_ga += ef.value(ga.getOptimal());
235 | time_ga += time;
236 | //System.out.println("ga: " + ef.value(ga.getOptimal()));
237 | //System.out.println(time);
238 |
239 |
240 | }
241 |
242 |
243 | double average_ga = sum_ga / testRuns;
244 |
245 | double averagetime_ga = time_ga / testRuns;
246 |
247 |
248 | System.out.println("##############");
249 | System.out.println("ga average is " + average_ga + "," + population[i] + "," + mate[i] + "," + mute[i]+ ", time average is " + averagetime_ga);
250 |
251 | String final_result = "";
252 | final_result = "ga" + "," + population[i] + "," + mate[i] + "," + mute[i] + "," + Double.toString(average_ga) + "," + Double.toString(averagetime_ga);
253 |
254 | write_output_to_file("Optimization_Results", "travelingsalesman_ga_results.csv", final_result, true);
255 | }
256 | **/
257 | }
258 |
259 | }
--------------------------------------------------------------------------------
/Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/Main.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4;
2 |
3 | import java.util.HashMap;
4 | import java.util.List;
5 |
6 | import burlap.behavior.policy.Policy;
7 | import burlap.behavior.policy.PolicyUtils;
8 | import burlap.behavior.singleagent.Episode;
9 | import burlap.behavior.singleagent.auxiliary.StateReachability;
10 | import burlap.behavior.singleagent.auxiliary.performance.LearningAlgorithmExperimenter;
11 | import burlap.behavior.singleagent.auxiliary.performance.PerformanceMetric;
12 | import burlap.behavior.singleagent.auxiliary.performance.TrialMode;
13 | import burlap.behavior.singleagent.auxiliary.valuefunctionvis.ValueFunctionVisualizerGUI;
14 | import burlap.behavior.singleagent.learning.LearningAgent;
15 | import burlap.behavior.singleagent.learning.LearningAgentFactory;
16 | import burlap.behavior.singleagent.learning.tdmethods.QLearning;
17 | import burlap.behavior.singleagent.planning.Planner;
18 | import burlap.behavior.singleagent.planning.stochastic.policyiteration.PolicyIteration;
19 | import burlap.behavior.singleagent.planning.stochastic.valueiteration.ValueIteration;
20 | import burlap.behavior.valuefunction.ValueFunction;
21 | import burlap.domain.singleagent.gridworld.GridWorldDomain;
22 | import burlap.domain.singleagent.gridworld.GridWorldRewardFunction;
23 | import burlap.domain.singleagent.gridworld.state.GridAgent;
24 | import burlap.domain.singleagent.gridworld.state.GridLocation;
25 | import burlap.domain.singleagent.gridworld.state.GridWorldState;
26 | import burlap.mdp.auxiliary.common.ConstantStateGenerator;
27 | import burlap.mdp.auxiliary.common.SinglePFTF;
28 | import burlap.mdp.core.TerminalFunction;
29 | import burlap.mdp.core.oo.propositional.PropositionalFunction;
30 | import burlap.mdp.core.state.State;
31 | import burlap.mdp.singleagent.SADomain;
32 | import burlap.mdp.singleagent.environment.SimulatedEnvironment;
33 | import burlap.mdp.singleagent.oo.OOSADomain;
34 | import burlap.statehashing.HashableStateFactory;
35 | import burlap.statehashing.simple.SimpleHashableStateFactory;
36 | import edu.gatech.cs7641.assignment4.artifacts.Algorithm;
37 | import edu.gatech.cs7641.assignment4.artifacts.Analysis;
38 | import edu.gatech.cs7641.assignment4.artifacts.Hazard;
39 | import edu.gatech.cs7641.assignment4.artifacts.Hazard.HazardType;
40 | import edu.gatech.cs7641.assignment4.artifacts.PlannerFactory;
41 | import edu.gatech.cs7641.assignment4.artifacts.Problem;
42 |
43 | public class Main {
44 |
45 | /*
46 | * Set this constant to the specific problem you want to execute. The code currently has two
47 | * different problems, but you can add more problems and control which one runs by using this
48 | * constant.
49 | */
50 | private static int PROBLEM = 1;
51 |
52 | /*
53 | * This class runs one algorithm at the time. You can set this constant to the specific
54 | * algorithm you want to run.
55 | */
56 | private final static Algorithm algorithm = Algorithm.QLearning;
57 |
58 | /*
59 | * If you set this constant to false, the specific GUI showing the grid, rewards, and policy
60 | * will not be displayed. Honestly, I never set this to false, so I'm not even sure why I added
61 | * the constant, but here it is anyway.
62 | */
63 | private final static boolean SHOW_VISUALIZATION = true;
64 |
65 | /*
66 | * This is a very cool feature of BURLAP that unfortunately only works with learning algorithms
67 | * (so no ValueIteration or PolicyIteration). It is somewhat redundant to the specific analysis
68 | * I implemented for all three algorithms (it computes some of the same stuff), but it shows
69 | * very cool charts and it lets you export all the data to external files.
70 | *
71 | * At the end, I didnt't use this much, but I'm sure some people will love it. Keep in mind that
72 | * by setting this constant to true, you'll be running the QLearning experiment twice (so double
73 | * the time).
74 | */
75 | private static boolean USE_LEARNING_EXPERIMENTER = true;
76 |
77 | public static void main(String[] args) {
78 |
79 | /*
80 | * If you want to set up more than two problems, make sure you change this ternary operator.
81 | */
82 | Problem problem = PROBLEM == 1
83 | ? createProblem1()
84 | : createProblem2();
85 |
86 | GridWorldDomain gridWorldDomain = new GridWorldDomain(problem.getWidth(), problem.getWidth());
87 | gridWorldDomain.setMap(problem.getMatrix());
88 | gridWorldDomain.setProbSucceedTransitionDynamics(0.8);
89 |
90 | /*
91 | * This makes sure that the algorithm finishes as soon as the agent reaches the goal. We
92 | * don't want the agent to run forever, so this is kind of important.
93 | *
94 | * You could set more than one goal if you wish, or you could even set hazards that end the
95 | * game (and penalize the agent with a negative reward). But this is not this code...
96 | */
97 | TerminalFunction terminalFunction = new SinglePFTF(PropositionalFunction.findPF(gridWorldDomain.generatePfs(), GridWorldDomain.PF_AT_LOCATION));
98 |
99 | GridWorldRewardFunction rewardFunction = new GridWorldRewardFunction(problem.getWidth(), problem.getWidth(), problem.getDefaultReward());
100 |
101 | /*
102 | * This sets the reward for the cell representing the goal. Of course, we want this reward
103 | * to be positive and juicy (unless we don't want our agent to reach the end, which will
104 | * probably be mean).
105 | */
106 | rewardFunction.setReward(problem.getGoal().x, problem.getGoal().y, problem.getGoalReward());
107 |
108 | /*
109 | * This sets up all the rewards associated with the different hazards specified on the
110 | * surface of the grid.
111 | */
112 | for (Hazard hazard : problem.getHazards()) {
113 | rewardFunction.setReward(hazard.getLocation().x, hazard.getLocation().y, hazard.getReward());
114 | }
115 |
116 | gridWorldDomain.setTf(terminalFunction);
117 | gridWorldDomain.setRf(rewardFunction);
118 |
119 | OOSADomain domain = gridWorldDomain.generateDomain();
120 |
121 | /*
122 | * This sets up the initial position of the agent, and the goal.
123 | */
124 | GridWorldState initialState = new GridWorldState(new GridAgent(problem.getStart().x, problem.getStart().y), new GridLocation(problem.getGoal().x, problem.getGoal().y, "loc0"));
125 |
126 | SimpleHashableStateFactory hashingFactory = new SimpleHashableStateFactory();
127 |
128 | Analysis analysis = new Analysis();
129 |
130 | /*
131 | * Depending on the specific algorithm that we want to run, I call the magic method and
132 | * specify the corresponding planner.
133 | */
134 | switch (algorithm) {
135 | case ValueIteration:
136 | runAlgorithm(analysis, problem, domain, hashingFactory, initialState, new PlannerFactory() {
137 |
138 | @Override
139 | public Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment) {
140 | return new ValueIteration(domain, 0.25, hashingFactory, 0.001, episodeIndex);
141 | }
142 | }, algorithm);
143 | break;
144 | case PolicyIteration:
145 | runAlgorithm(analysis, problem, domain, hashingFactory, initialState, new PlannerFactory() {
146 |
147 | @Override
148 | public Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment) {
149 |
150 | /*
151 | * For PolicyIteration we need to specify the number of iterations of
152 | * ValueIteration that the algorithm will use internally to compute the
153 | * corresponding values. By default, the code is using the same number of
154 | * iterations specified for the ValueIteration algorithm.
155 | *
156 | * A recommended modification is to change this value to the actual number
157 | * of iterations that it takes ValueIteration to converge. This will
158 | * considerably improve the runtime of the algorithm (assuming that
159 | * ValueIteration converges faster than the number of configured
160 | * iterations).
161 | */
162 | return new PolicyIteration(domain, 0.25, hashingFactory, 0.000001, problem.getNumberOfIterations(Algorithm.ValueIteration), episodeIndex);
163 | }
164 | }, algorithm);
165 | break;
166 | default:
167 | runAlgorithm(analysis, problem, domain, hashingFactory, initialState, new PlannerFactory() {
168 |
169 | @Override
170 | public Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment) {
171 | QLearning agent = new QLearning(domain, 0.25, hashingFactory, 0.3, 0.1);
172 | for (int i = 0; i < episodeIndex; i++) {
173 | agent.runLearningEpisode(simulatedEnvironment);
174 | simulatedEnvironment.resetEnvironment();
175 | }
176 |
177 | agent.initializeForPlanning(1);
178 |
179 | return agent;
180 | }
181 | }, algorithm);
182 | break;
183 | }
184 |
185 | analysis.print();
186 | }
187 |
188 | /**
189 | * Here is where the magic happens. In this method is where I loop through the specific number
190 | * of episodes (iterations) and run the specific algorithm. To keep things nice and clean, I use
191 | * this method to run all three algorithms. The specific details are specified through the
192 | * PlannerFactory interface.
193 | *
194 | * This method collects all the information from the algorithm and packs it in an Analysis
195 | * instance that later gets dumped on the console.
196 | */
197 | private static void runAlgorithm(Analysis analysis, Problem problem, SADomain domain, HashableStateFactory hashingFactory, State initialState, PlannerFactory plannerFactory, Algorithm algorithm) {
198 | ConstantStateGenerator constantStateGenerator = new ConstantStateGenerator(initialState);
199 | SimulatedEnvironment simulatedEnvironment = new SimulatedEnvironment(domain, constantStateGenerator);
200 | Planner planner = null;
201 | Policy policy = null;
202 | for (int episodeIndex = 1; episodeIndex <= problem.getNumberOfIterations(algorithm); episodeIndex++) {
203 | long startTime = System.nanoTime();
204 | planner = plannerFactory.createPlanner(episodeIndex, domain, hashingFactory, simulatedEnvironment);
205 | policy = planner.planFromState(initialState);
206 |
207 | /*
208 | * If we haven't converged, following the policy will lead the agent wandering around
209 | * and it might never reach the goal. To avoid this, we need to set the maximum number
210 | * of steps to take before terminating the policy rollout. I decided to set this maximum
211 | * at the number of grid locations in our map (width * width). This should give the
212 | * agent plenty of room to wander around.
213 | *
214 | * The smaller this number is, the faster the algorithm will run.
215 | */
216 | //int maxNumberOfSteps = problem.getWidth() * problem.getWidth() * problem.getWidth() * problem.getWidth();
217 |
218 | int maxNumberOfSteps = 5000000;
219 |
220 | Episode episode = PolicyUtils.rollout(policy, initialState, domain.getModel(), maxNumberOfSteps);
221 | analysis.add(episodeIndex, episode.rewardSequence, episode.numTimeSteps(), (long) (System.nanoTime() - startTime) / 1000000);
222 | }
223 |
224 | if (algorithm == Algorithm.QLearning && USE_LEARNING_EXPERIMENTER) {
225 | learningExperimenter(problem, (LearningAgent) planner, simulatedEnvironment);
226 | }
227 |
228 | if (SHOW_VISUALIZATION && planner != null && policy != null) {
229 | visualize(problem, (ValueFunction) planner, policy, initialState, domain, hashingFactory, algorithm.getTitle());
230 | }
231 | }
232 |
233 | /**
234 | * Runs a learning experiment and shows some cool charts. Apparently, this is only useful for
235 | * Q-Learning, so I only call this method when Q-Learning is selected and the appropriate flag
236 | * is enabled.
237 | */
238 | private static void learningExperimenter(Problem problem, LearningAgent agent, SimulatedEnvironment simulatedEnvironment) {
239 | LearningAlgorithmExperimenter experimenter = new LearningAlgorithmExperimenter(simulatedEnvironment, 10, problem.getNumberOfIterations(Algorithm.QLearning), new LearningAgentFactory() {
240 |
241 | public String getAgentName() {
242 | return Algorithm.QLearning.getTitle();
243 | }
244 |
245 | public LearningAgent generateAgent() {
246 | return agent;
247 | }
248 | });
249 |
250 | /*
251 | * Try different PerformanceMetric values below to display different charts.
252 | */
253 | experimenter.setUpPlottingConfiguration(500, 250, 2, 1000, TrialMode.MOST_RECENT_AND_AVERAGE, PerformanceMetric.CUMULATIVE_STEPS_PER_EPISODE, PerformanceMetric.AVERAGE_EPISODE_REWARD);
254 | experimenter.startExperiment();
255 | }
256 |
257 | /**
258 | * This method takes care of visualizing the grid, rewards, and specific policy on a nice
259 | * BURLAP-predefined GUI. I found this very useful to understand how the algorithm was working.
260 | */
261 | private static void visualize(Problem map, ValueFunction valueFunction, Policy policy, State initialState, SADomain domain, HashableStateFactory hashingFactory, String title) {
262 | List states = StateReachability.getReachableStates(initialState, domain, hashingFactory);
263 | ValueFunctionVisualizerGUI gui = GridWorldDomain.getGridWorldValueFunctionVisualization(states, map.getWidth(), map.getWidth(), valueFunction, policy);
264 | gui.setTitle(title);
265 | gui.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
266 | gui.initGUI();
267 | }
268 |
269 | private static Problem createProblem1() {
270 | /*
271 | * The surface can be described as follows:
272 | *
273 | * X — The starting point of the agent.
274 | * 0 — Represents a safe cell where the agent can move.
275 | * 1 — Represents a wall. The agent can't move to this cell.
276 | * G — Represents the goal that the agent wants to achieve.
277 | * S — Represents a small hazard. The agent will be penalized.
278 | * M — Represents a medium hazard. The agent will be penalized.
279 | * L — Represents a large hazard. The agent will be penalized.
280 | */
281 | String[] map = new String[] {
282 | "X0001",
283 | "00001",
284 | "0LML0",
285 | "0000S",
286 | "110SG",
287 |
288 | };
289 |
290 | /*
291 | * Make sure to specify the specific number of iterations for each algorithm. If you don't
292 | * do this, I'm still nice and use 100 as the default value, but that wouldn't make sense
293 | * all the time.
294 | */
295 | HashMap numIterationsHashMap = new HashMap();
296 | numIterationsHashMap.put(Algorithm.ValueIteration, 20);
297 | numIterationsHashMap.put(Algorithm.PolicyIteration, 2);
298 | numIterationsHashMap.put(Algorithm.QLearning, 50);
299 |
300 | /*
301 | * These are the specific rewards for each one of the hazards. Here you can be creative and
302 | * play with different values as you see fit.
303 | */
304 | HashMap hazardRewardsHashMap = new HashMap();
305 | hazardRewardsHashMap.put(HazardType.SMALL, 1.0);
306 | hazardRewardsHashMap.put(HazardType.MEDIUM, 2.5);
307 | hazardRewardsHashMap.put(HazardType.LARGE, -50.0);
308 |
309 | /*
310 | * Notice how I specify below the specific default reward for cells with nothing on them (we
311 | * want regular cells to have a small penalty that encourages our agent to find the goal),
312 | * and the reward for the cell representing the goal (something nice and large so the agent
313 | * is happy).
314 | */
315 | return new Problem(map, numIterationsHashMap, -5, 100, hazardRewardsHashMap);
316 | }
317 |
318 | private static Problem createProblem2() {
319 | String[] map = new String[] {
320 | "XSSS0000M1111M00SSSS",
321 | "S0000L00000000001111",
322 | "S00000010000000010M0",
323 | "S0111111000111111000",
324 | "000000M10000000100G0",
325 | "000000010000L0000SSS",
326 | "00111S01000000000111",
327 | "00SS1S010011111001M0",
328 | "00S11S00001111100100",
329 | "00SS1S0L000000000100",
330 | "00001S00000000001100",
331 | "000011111110000L0100",
332 | "010000000M1001000100",
333 | "M1SS000L001001G00000",
334 | "111S0000001M01SSSS00",
335 | "SSSS0011111111111100",
336 | "G0000000000001M0G000",
337 | "1000L00SSSS001000001",
338 | "100000GS110L00000000",
339 | "1111SMSS110000SSSSSS",
340 |
341 | };
342 |
343 | HashMap numIterationsHashMap = new HashMap();
344 | numIterationsHashMap.put(Algorithm.ValueIteration, 6);
345 | numIterationsHashMap.put(Algorithm.PolicyIteration, 5);
346 | numIterationsHashMap.put(Algorithm.QLearning, 100);
347 |
348 | HashMap hazardRewardsHashMap = new HashMap();
349 | hazardRewardsHashMap.put(HazardType.SMALL, 1.0);
350 | hazardRewardsHashMap.put(HazardType.MEDIUM, 2.5);
351 | hazardRewardsHashMap.put(HazardType.LARGE, -50.0);
352 |
353 | return new Problem(map, numIterationsHashMap, -5, 100, hazardRewardsHashMap);
354 | }
355 |
356 | }
357 |
--------------------------------------------------------------------------------
/Assignment 4 Markov Decision Processes/assignment4/src/main/java/edu/gatech/cs7641/assignment4/Main.java:
--------------------------------------------------------------------------------
1 | package edu.gatech.cs7641.assignment4;
2 |
3 | import java.util.HashMap;
4 | import java.util.List;
5 |
6 | import burlap.behavior.policy.Policy;
7 | import burlap.behavior.policy.PolicyUtils;
8 | import burlap.behavior.singleagent.Episode;
9 | import burlap.behavior.singleagent.auxiliary.StateReachability;
10 | import burlap.behavior.singleagent.auxiliary.performance.LearningAlgorithmExperimenter;
11 | import burlap.behavior.singleagent.auxiliary.performance.PerformanceMetric;
12 | import burlap.behavior.singleagent.auxiliary.performance.TrialMode;
13 | import burlap.behavior.singleagent.auxiliary.valuefunctionvis.ValueFunctionVisualizerGUI;
14 | import burlap.behavior.singleagent.learning.LearningAgent;
15 | import burlap.behavior.singleagent.learning.LearningAgentFactory;
16 | import burlap.behavior.singleagent.learning.tdmethods.QLearning;
17 | import burlap.behavior.singleagent.planning.Planner;
18 | import burlap.behavior.singleagent.planning.stochastic.policyiteration.PolicyIteration;
19 | import burlap.behavior.singleagent.planning.stochastic.valueiteration.ValueIteration;
20 | import burlap.behavior.valuefunction.ValueFunction;
21 | import burlap.domain.singleagent.gridworld.GridWorldDomain;
22 | import burlap.domain.singleagent.gridworld.GridWorldRewardFunction;
23 | import burlap.domain.singleagent.gridworld.state.GridAgent;
24 | import burlap.domain.singleagent.gridworld.state.GridLocation;
25 | import burlap.domain.singleagent.gridworld.state.GridWorldState;
26 | import burlap.mdp.auxiliary.common.ConstantStateGenerator;
27 | import burlap.mdp.auxiliary.common.SinglePFTF;
28 | import burlap.mdp.core.TerminalFunction;
29 | import burlap.mdp.core.oo.propositional.PropositionalFunction;
30 | import burlap.mdp.core.state.State;
31 | import burlap.mdp.singleagent.SADomain;
32 | import burlap.mdp.singleagent.environment.SimulatedEnvironment;
33 | import burlap.mdp.singleagent.oo.OOSADomain;
34 | import burlap.statehashing.HashableStateFactory;
35 | import burlap.statehashing.simple.SimpleHashableStateFactory;
36 | import edu.gatech.cs7641.assignment4.artifacts.Algorithm;
37 | import edu.gatech.cs7641.assignment4.artifacts.Analysis;
38 | import edu.gatech.cs7641.assignment4.artifacts.Hazard;
39 | import edu.gatech.cs7641.assignment4.artifacts.Hazard.HazardType;
40 | import edu.gatech.cs7641.assignment4.artifacts.PlannerFactory;
41 | import edu.gatech.cs7641.assignment4.artifacts.Problem;
42 |
43 | public class Main {
44 |
45 | /*
46 | * Set this constant to the specific problem you want to execute. The code currently has two
47 | * different problems, but you can add more problems and control which one runs by using this
48 | * constant.
49 | */
50 | private static int PROBLEM = 1;
51 |
52 | /*
53 | * This class runs one algorithm at the time. You can set this constant to the specific
54 | * algorithm you want to run.
55 | */
56 | private final static Algorithm algorithm = Algorithm.QLearning;
57 |
58 | /*
59 | * If you set this constant to false, the specific GUI showing the grid, rewards, and policy
60 | * will not be displayed. Honestly, I never set this to false, so I'm not even sure why I added
61 | * the constant, but here it is anyway.
62 | */
63 | private final static boolean SHOW_VISUALIZATION = true;
64 |
65 | /*
66 | * This is a very cool feature of BURLAP that unfortunately only works with learning algorithms
67 | * (so no ValueIteration or PolicyIteration). It is somewhat redundant to the specific analysis
68 | * I implemented for all three algorithms (it computes some of the same stuff), but it shows
69 | * very cool charts and it lets you export all the data to external files.
70 | *
71 | * At the end, I didnt't use this much, but I'm sure some people will love it. Keep in mind that
72 | * by setting this constant to true, you'll be running the QLearning experiment twice (so double
73 | * the time).
74 | */
75 | private static boolean USE_LEARNING_EXPERIMENTER = true;
76 |
77 | public static void main(String[] args) {
78 |
79 | /*
80 | * If you want to set up more than two problems, make sure you change this ternary operator.
81 | */
82 | Problem problem = PROBLEM == 1
83 | ? createProblem1()
84 | : createProblem2();
85 |
86 | GridWorldDomain gridWorldDomain = new GridWorldDomain(problem.getWidth(), problem.getWidth());
87 | gridWorldDomain.setMap(problem.getMatrix());
88 | gridWorldDomain.setProbSucceedTransitionDynamics(0.8);
89 |
90 | /*
91 | * This makes sure that the algorithm finishes as soon as the agent reaches the goal. We
92 | * don't want the agent to run forever, so this is kind of important.
93 | *
94 | * You could set more than one goal if you wish, or you could even set hazards that end the
95 | * game (and penalize the agent with a negative reward). But this is not this code...
96 | */
97 | TerminalFunction terminalFunction = new SinglePFTF(PropositionalFunction.findPF(gridWorldDomain.generatePfs(), GridWorldDomain.PF_AT_LOCATION));
98 |
99 | GridWorldRewardFunction rewardFunction = new GridWorldRewardFunction(problem.getWidth(), problem.getWidth(), problem.getDefaultReward());
100 |
101 | /*
102 | * This sets the reward for the cell representing the goal. Of course, we want this reward
103 | * to be positive and juicy (unless we don't want our agent to reach the end, which will
104 | * probably be mean).
105 | */
106 | rewardFunction.setReward(problem.getGoal().x, problem.getGoal().y, problem.getGoalReward());
107 |
108 | /*
109 | * This sets up all the rewards associated with the different hazards specified on the
110 | * surface of the grid.
111 | */
112 | for (Hazard hazard : problem.getHazards()) {
113 | rewardFunction.setReward(hazard.getLocation().x, hazard.getLocation().y, hazard.getReward());
114 | }
115 |
116 | gridWorldDomain.setTf(terminalFunction);
117 | gridWorldDomain.setRf(rewardFunction);
118 |
119 | OOSADomain domain = gridWorldDomain.generateDomain();
120 |
121 | /*
122 | * This sets up the initial position of the agent, and the goal.
123 | */
124 | GridWorldState initialState = new GridWorldState(new GridAgent(problem.getStart().x, problem.getStart().y), new GridLocation(problem.getGoal().x, problem.getGoal().y, "loc0"));
125 |
126 | SimpleHashableStateFactory hashingFactory = new SimpleHashableStateFactory();
127 |
128 | Analysis analysis = new Analysis();
129 |
130 | /*
131 | * Depending on the specific algorithm that we want to run, I call the magic method and
132 | * specify the corresponding planner.
133 | */
134 | switch (algorithm) {
135 | case ValueIteration:
136 | runAlgorithm(analysis, problem, domain, hashingFactory, initialState, new PlannerFactory() {
137 |
138 | @Override
139 | public Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment) {
140 | return new ValueIteration(domain, 0.25, hashingFactory, 0.001, episodeIndex);
141 | }
142 | }, algorithm);
143 | break;
144 | case PolicyIteration:
145 | runAlgorithm(analysis, problem, domain, hashingFactory, initialState, new PlannerFactory() {
146 |
147 | @Override
148 | public Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment) {
149 |
150 | /*
151 | * For PolicyIteration we need to specify the number of iterations of
152 | * ValueIteration that the algorithm will use internally to compute the
153 | * corresponding values. By default, the code is using the same number of
154 | * iterations specified for the ValueIteration algorithm.
155 | *
156 | * A recommended modification is to change this value to the actual number
157 | * of iterations that it takes ValueIteration to converge. This will
158 | * considerably improve the runtime of the algorithm (assuming that
159 | * ValueIteration converges faster than the number of configured
160 | * iterations).
161 | */
162 | return new PolicyIteration(domain, 0.25, hashingFactory, 0.000001, problem.getNumberOfIterations(Algorithm.ValueIteration), episodeIndex);
163 | }
164 | }, algorithm);
165 | break;
166 | default:
167 | runAlgorithm(analysis, problem, domain, hashingFactory, initialState, new PlannerFactory() {
168 |
169 | @Override
170 | public Planner createPlanner(int episodeIndex, SADomain domain, HashableStateFactory hashingFactory, SimulatedEnvironment simulatedEnvironment) {
171 | QLearning agent = new QLearning(domain, 0.25, hashingFactory, 0.3, 0.1);
172 | for (int i = 0; i < episodeIndex; i++) {
173 | agent.runLearningEpisode(simulatedEnvironment);
174 | simulatedEnvironment.resetEnvironment();
175 | }
176 |
177 | agent.initializeForPlanning(1);
178 |
179 | return agent;
180 | }
181 | }, algorithm);
182 | break;
183 | }
184 |
185 | analysis.print();
186 | }
187 |
188 | /**
189 | * Here is where the magic happens. In this method is where I loop through the specific number
190 | * of episodes (iterations) and run the specific algorithm. To keep things nice and clean, I use
191 | * this method to run all three algorithms. The specific details are specified through the
192 | * PlannerFactory interface.
193 | *
194 | * This method collects all the information from the algorithm and packs it in an Analysis
195 | * instance that later gets dumped on the console.
196 | */
197 | private static void runAlgorithm(Analysis analysis, Problem problem, SADomain domain, HashableStateFactory hashingFactory, State initialState, PlannerFactory plannerFactory, Algorithm algorithm) {
198 | ConstantStateGenerator constantStateGenerator = new ConstantStateGenerator(initialState);
199 | SimulatedEnvironment simulatedEnvironment = new SimulatedEnvironment(domain, constantStateGenerator);
200 | Planner planner = null;
201 | Policy policy = null;
202 | for (int episodeIndex = 1; episodeIndex <= problem.getNumberOfIterations(algorithm); episodeIndex++) {
203 | long startTime = System.nanoTime();
204 | planner = plannerFactory.createPlanner(episodeIndex, domain, hashingFactory, simulatedEnvironment);
205 | policy = planner.planFromState(initialState);
206 |
207 | /*
208 | * If we haven't converged, following the policy will lead the agent wandering around
209 | * and it might never reach the goal. To avoid this, we need to set the maximum number
210 | * of steps to take before terminating the policy rollout. I decided to set this maximum
211 | * at the number of grid locations in our map (width * width). This should give the
212 | * agent plenty of room to wander around.
213 | *
214 | * The smaller this number is, the faster the algorithm will run.
215 | */
216 | //int maxNumberOfSteps = problem.getWidth() * problem.getWidth() * problem.getWidth() * problem.getWidth();
217 |
218 | int maxNumberOfSteps = 5000000;
219 |
220 | Episode episode = PolicyUtils.rollout(policy, initialState, domain.getModel(), maxNumberOfSteps);
221 | analysis.add(episodeIndex, episode.rewardSequence, episode.numTimeSteps(), (long) (System.nanoTime() - startTime) / 1000000);
222 | }
223 |
224 | if (algorithm == Algorithm.QLearning && USE_LEARNING_EXPERIMENTER) {
225 | learningExperimenter(problem, (LearningAgent) planner, simulatedEnvironment);
226 | }
227 |
228 | if (SHOW_VISUALIZATION && planner != null && policy != null) {
229 | visualize(problem, (ValueFunction) planner, policy, initialState, domain, hashingFactory, algorithm.getTitle());
230 | }
231 | }
232 |
233 | /**
234 | * Runs a learning experiment and shows some cool charts. Apparently, this is only useful for
235 | * Q-Learning, so I only call this method when Q-Learning is selected and the appropriate flag
236 | * is enabled.
237 | */
238 | private static void learningExperimenter(Problem problem, LearningAgent agent, SimulatedEnvironment simulatedEnvironment) {
239 | LearningAlgorithmExperimenter experimenter = new LearningAlgorithmExperimenter(simulatedEnvironment, 10, problem.getNumberOfIterations(Algorithm.QLearning), new LearningAgentFactory() {
240 |
241 | public String getAgentName() {
242 | return Algorithm.QLearning.getTitle();
243 | }
244 |
245 | public LearningAgent generateAgent() {
246 | return agent;
247 | }
248 | });
249 |
250 | /*
251 | * Try different PerformanceMetric values below to display different charts.
252 | */
253 | experimenter.setUpPlottingConfiguration(500, 250, 2, 1000, TrialMode.MOST_RECENT_AND_AVERAGE, PerformanceMetric.CUMULATIVE_STEPS_PER_EPISODE, PerformanceMetric.AVERAGE_EPISODE_REWARD);
254 | experimenter.startExperiment();
255 | }
256 |
257 | /**
258 | * This method takes care of visualizing the grid, rewards, and specific policy on a nice
259 | * BURLAP-predefined GUI. I found this very useful to understand how the algorithm was working.
260 | */
261 | private static void visualize(Problem map, ValueFunction valueFunction, Policy policy, State initialState, SADomain domain, HashableStateFactory hashingFactory, String title) {
262 | List states = StateReachability.getReachableStates(initialState, domain, hashingFactory);
263 | ValueFunctionVisualizerGUI gui = GridWorldDomain.getGridWorldValueFunctionVisualization(states, map.getWidth(), map.getWidth(), valueFunction, policy);
264 | gui.setTitle(title);
265 | gui.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
266 | gui.initGUI();
267 | }
268 |
269 | private static Problem createProblem1() {
270 | /*
271 | * The surface can be described as follows:
272 | *
273 | * X — The starting point of the agent.
274 | * 0 — Represents a safe cell where the agent can move.
275 | * 1 — Represents a wall. The agent can't move to this cell.
276 | * G — Represents the goal that the agent wants to achieve.
277 | * S — Represents a small hazard. The agent will be penalized.
278 | * M — Represents a medium hazard. The agent will be penalized.
279 | * L — Represents a large hazard. The agent will be penalized.
280 | */
281 | String[] map = new String[] {
282 | "X0001",
283 | "00001",
284 | "0LML0",
285 | "0000S",
286 | "110SG",
287 |
288 | };
289 |
290 | /*
291 | * Make sure to specify the specific number of iterations for each algorithm. If you don't
292 | * do this, I'm still nice and use 100 as the default value, but that wouldn't make sense
293 | * all the time.
294 | */
295 | HashMap numIterationsHashMap = new HashMap();
296 | numIterationsHashMap.put(Algorithm.ValueIteration, 20);
297 | numIterationsHashMap.put(Algorithm.PolicyIteration, 2);
298 | numIterationsHashMap.put(Algorithm.QLearning, 50);
299 |
300 | /*
301 | * These are the specific rewards for each one of the hazards. Here you can be creative and
302 | * play with different values as you see fit.
303 | */
304 | HashMap hazardRewardsHashMap = new HashMap();
305 | hazardRewardsHashMap.put(HazardType.SMALL, 1.0);
306 | hazardRewardsHashMap.put(HazardType.MEDIUM, 2.5);
307 | hazardRewardsHashMap.put(HazardType.LARGE, -50.0);
308 |
309 | /*
310 | * Notice how I specify below the specific default reward for cells with nothing on them (we
311 | * want regular cells to have a small penalty that encourages our agent to find the goal),
312 | * and the reward for the cell representing the goal (something nice and large so the agent
313 | * is happy).
314 | */
315 | return new Problem(map, numIterationsHashMap, -5, 100, hazardRewardsHashMap);
316 | }
317 |
318 | private static Problem createProblem2() {
319 | String[] map = new String[] {
320 | "XSSS0000M1111M00SSSS",
321 | "S0000L00000000001111",
322 | "S00000010000000010M0",
323 | "S0111111000111111000",
324 | "000000M10000000100G0",
325 | "000000010000L0000SSS",
326 | "00111S01000000000111",
327 | "00SS1S010011111001M0",
328 | "00S11S00001111100100",
329 | "00SS1S0L000000000100",
330 | "00001S00000000001100",
331 | "000011111110000L0100",
332 | "010000000M1001000100",
333 | "M1SS000L001001G00000",
334 | "111S0000001M01SSSS00",
335 | "SSSS0011111111111100",
336 | "G0000000000001M0G000",
337 | "1000L00SSSS001000001",
338 | "100000GS110L00000000",
339 | "1111SMSS110000SSSSSS",
340 |
341 | };
342 |
343 | HashMap numIterationsHashMap = new HashMap();
344 | numIterationsHashMap.put(Algorithm.ValueIteration, 6);
345 | numIterationsHashMap.put(Algorithm.PolicyIteration, 5);
346 | numIterationsHashMap.put(Algorithm.QLearning, 100);
347 |
348 | HashMap hazardRewardsHashMap = new HashMap();
349 | hazardRewardsHashMap.put(HazardType.SMALL, 1.0);
350 | hazardRewardsHashMap.put(HazardType.MEDIUM, 2.5);
351 | hazardRewardsHashMap.put(HazardType.LARGE, -50.0);
352 |
353 | return new Problem(map, numIterationsHashMap, -5, 100, hazardRewardsHashMap);
354 | }
355 |
356 | }
357 |
--------------------------------------------------------------------------------
/Assignment 1 Supervised Learning/CS 7641 HW1 Code.py:
--------------------------------------------------------------------------------
1 |
2 | # coding: utf-8
3 |
4 | # # CS 7641 HW1 Code - Supervised Learning
5 |
6 | # This file will provide analysis for 5 different classification algorithms for two datasets.
7 | #
8 | # Datasets: Phishing Websites, Bank Marketing.
9 | #
10 | # Classification Algorithms: Decision Tree, Neural Network, Boosting, SVM, KNN.
11 | #
12 | # There will be two outputs for each model: a plot of learning curve and a plot of model complexity. Both plots will be generated after hyperparameter tuning is performed.
13 |
14 | # # 1. Data Load and Preprocessing
15 |
16 | # First we load the data! Please save the datasets to your local machine and change the current directory to a file where you have the data stored.
17 |
18 | # In[19]:
19 |
20 |
21 | import os
22 | import pandas as pd
23 | import numpy as np
24 | import random
25 |
26 | get_ipython().run_line_magic('matplotlib', 'inline')
27 | import matplotlib.pyplot as plt
28 | plt.style.use('seaborn-whitegrid')
29 |
30 | os.chdir(r"C:\...") #change this to your current working directory
31 |
32 |
33 | # ### Load the Phishing Website Data
34 |
35 | # In[20]:
36 |
37 |
38 | ## Download the Phishing Data from OpenML https://www.openml.org/d/4534
39 |
40 | df_phish = pd.read_csv('PhishingWebsitesData.csv').astype('category')
41 | print("Data has",len(df_phish),"rows and", len(df_phish.columns),"columns.")
42 | if df_phish.isnull().values.any():
43 | print("Warning: Missing Data")
44 | #df_phish.head()
45 | #df_phish.describe(include='all')
46 |
47 |
48 | # Now that the phishing data is loaded, we need to do some preprocessing. Several of the columns are categorical with the levels {-1,0,1} and the rest are all binary with levels {-1,1}. For the 3-level columns we will use one-hot encoding to create additional features with level {0,1}. Finally, we will edit the binary features so that the new levels are all {0,1}. We will have more features now, but they will all be binary.
49 |
50 | # In[21]:
51 |
52 |
53 | col_1hot = ['URL_Length','having_Sub_Domain','SSLfinal_State','URL_of_Anchor','Links_in_tags','SFH','web_traffic','Links_pointing_to_page']
54 | df_1hot = df_phish[col_1hot]
55 | df_1hot = pd.get_dummies(df_1hot)
56 | df_others = df_phish.drop(col_1hot,axis=1)
57 | df_phish = pd.concat([df_1hot,df_others],axis=1)
58 | df_phish = df_phish.replace(-1,0).astype('category')
59 | column_order = list(df_phish)
60 | column_order.insert(0, column_order.pop(column_order.index('Result')))
61 | df_phish = df_phish.loc[:, column_order] #move the target variable 'Result' to the front
62 | df_phish.describe(include='all')
63 |
64 |
65 | # We now have a file with no missing data in the format [y, X] where all features are binary {0,1}. The phishing data is ready to go! Now we move on to loading the Bank Marketing data.
66 |
67 | # ### Load the Bank Marketing Data
68 |
69 | # In[7]:
70 |
71 |
72 | ## Load the Bank Marketing Data from OpenML https://www.openml.org/d/1461
73 |
74 | df_bank = pd.read_csv('BankMarketingData.csv')
75 | print("Data has",len(df_bank),"rows and", len(df_bank.columns),"columns.")
76 | if df_bank.isnull().values.any():
77 | print("Warning: Missing Data")
78 | #df_bank.head()
79 | #df_bank.describe(include='all')
80 |
81 |
82 | # This dataset needs some preprocessing love too. We will convert all categorical columns using one hot encoding. Additionally, we will standardize all of the numeric features and we will convert the target variable from {no,yes} to {0,1}. It should be noted that the feature 'pdays' is numeric but contains values that are '999' if the customer was not called before. It may be worth while to create a new feature that defines whether or not {0,1} a customer had been called before. In the current state the '999' values may be outliers. Finally we will standardize all numeric columns.
83 |
84 | # In[8]:
85 |
86 |
87 | col_1hot = ['job','marital','education','default','housing','loan','contact','month','day_of_week','poutcome']
88 | df_1hot = df_bank[col_1hot]
89 | df_1hot = pd.get_dummies(df_1hot).astype('category')
90 | df_others = df_bank.drop(col_1hot,axis=1)
91 | df_bank = pd.concat([df_others,df_1hot],axis=1)
92 | column_order = list(df_bank)
93 | column_order.insert(0, column_order.pop(column_order.index('y')))
94 | df_bank = df_bank.loc[:, column_order]
95 | df_bank['y'].replace("no",0,inplace=True)
96 | df_bank['y'].replace("yes",1,inplace=True)
97 | df_bank['y'] = df_bank['y'].astype('category')
98 |
99 | numericcols = ['age','duration','campaign','pdays','previous','emp.var.rate','cons.price.idx','cons.conf.idx','euribor3m','nr.employed']
100 | df_num = df_bank[numericcols]
101 | df_stand =(df_num-df_num.min())/(df_num.max()-df_num.min())
102 | df_bank_categorical = df_bank.drop(numericcols,axis=1)
103 | df_bank = pd.concat([df_bank_categorical,df_stand],axis=1)
104 | df_bank.describe(include='all')
105 |
106 |
107 | # Now we have successfully loaded and processed both datasets. We are ready to start the ML!
108 |
109 | # # 2. Helper Functions
110 |
111 | # ### Data Loading and Function Prep
112 |
113 | # Before we get into the algorithms, let's define some helper functions that will be used across all of the models and both datasets. We will define a function to load the data (not really necessary in a Jupyter notebook, but good if this is exported as a .py for later use). We will also define a function that plots the learning curve (training and cross validation score as a function of training examples) of an estimator (classification model). Finally, we define functions to output final model scores using an untouched test dataset.
114 |
115 | # In[9]:
116 |
117 |
118 | import matplotlib.pyplot as plt
119 | from sklearn.model_selection import cross_validate, train_test_split, GridSearchCV
120 | from sklearn.metrics import roc_auc_score, accuracy_score, f1_score, precision_score, recall_score, confusion_matrix
121 | import itertools
122 | import timeit
123 |
124 | plt.rcParams['xtick.labelsize'] = 12
125 | plt.rcParams['ytick.labelsize'] = 12
126 | plt.rcParams['axes.titlesize'] = 12
127 | plt.rcParams['font.size'] = 12
128 |
129 | def import_data():
130 |
131 | X1 = np.array(df_phish.values[:,1:-1],dtype='int64')
132 | Y1 = np.array(df_phish.values[:,0],dtype='int64')
133 | X2 = np.array(df_bank.values[:,1:-1],dtype='int64')
134 | Y2 = np.array(df_bank.values[:,0],dtype='int64')
135 | return X1, Y1, X2, Y2
136 |
137 |
138 | def plot_learning_curve(clf, X, y, title="Insert Title"):
139 |
140 | n = len(y)
141 | train_mean = []; train_std = [] #model performance score (f1)
142 | cv_mean = []; cv_std = [] #model performance score (f1)
143 | fit_mean = []; fit_std = [] #model fit/training time
144 | pred_mean = []; pred_std = [] #model test/prediction times
145 | train_sizes=(np.linspace(.05, 1.0, 20)*n).astype('int')
146 |
147 | for i in train_sizes:
148 | idx = np.random.randint(X.shape[0], size=i)
149 | X_subset = X[idx,:]
150 | y_subset = y[idx]
151 | scores = cross_validate(clf, X_subset, y_subset, cv=10, scoring='f1', n_jobs=-1, return_train_score=True)
152 |
153 | train_mean.append(np.mean(scores['train_score'])); train_std.append(np.std(scores['train_score']))
154 | cv_mean.append(np.mean(scores['test_score'])); cv_std.append(np.std(scores['test_score']))
155 | fit_mean.append(np.mean(scores['fit_time'])); fit_std.append(np.std(scores['fit_time']))
156 | pred_mean.append(np.mean(scores['score_time'])); pred_std.append(np.std(scores['score_time']))
157 |
158 | train_mean = np.array(train_mean); train_std = np.array(train_std)
159 | cv_mean = np.array(cv_mean); cv_std = np.array(cv_std)
160 | fit_mean = np.array(fit_mean); fit_std = np.array(fit_std)
161 | pred_mean = np.array(pred_mean); pred_std = np.array(pred_std)
162 |
163 | plot_LC(train_sizes, train_mean, train_std, cv_mean, cv_std, title)
164 | plot_times(train_sizes, fit_mean, fit_std, pred_mean, pred_std, title)
165 |
166 | return train_sizes, train_mean, fit_mean, pred_mean
167 |
168 |
169 | def plot_LC(train_sizes, train_mean, train_std, cv_mean, cv_std, title):
170 |
171 | plt.figure()
172 | plt.title("Learning Curve: "+ title)
173 | plt.xlabel("Training Examples")
174 | plt.ylabel("Model F1 Score")
175 | plt.fill_between(train_sizes, train_mean - 2*train_std, train_mean + 2*train_std, alpha=0.1, color="b")
176 | plt.fill_between(train_sizes, cv_mean - 2*cv_std, cv_mean + 2*cv_std, alpha=0.1, color="r")
177 | plt.plot(train_sizes, train_mean, 'o-', color="b", label="Training Score")
178 | plt.plot(train_sizes, cv_mean, 'o-', color="r", label="Cross-Validation Score")
179 | plt.legend(loc="best")
180 | plt.show()
181 |
182 |
183 | def plot_times(train_sizes, fit_mean, fit_std, pred_mean, pred_std, title):
184 |
185 | plt.figure()
186 | plt.title("Modeling Time: "+ title)
187 | plt.xlabel("Training Examples")
188 | plt.ylabel("Training Time (s)")
189 | plt.fill_between(train_sizes, fit_mean - 2*fit_std, fit_mean + 2*fit_std, alpha=0.1, color="b")
190 | plt.fill_between(train_sizes, pred_mean - 2*pred_std, pred_mean + 2*pred_std, alpha=0.1, color="r")
191 | plt.plot(train_sizes, fit_mean, 'o-', color="b", label="Training Time (s)")
192 | plt.plot(train_sizes, pred_std, 'o-', color="r", label="Prediction Time (s)")
193 | plt.legend(loc="best")
194 | plt.show()
195 |
196 |
197 | def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion Matrix', cmap=plt.cm.Blues):
198 |
199 | plt.imshow(cm, interpolation='nearest', cmap=cmap)
200 | plt.title(title)
201 | plt.colorbar()
202 | tick_marks = np.arange(len(classes))
203 | plt.xticks(tick_marks, classes)
204 | plt.yticks(tick_marks, classes)
205 |
206 | fmt = '.2f' if normalize else 'd'
207 | thresh = cm.max() / 2.
208 | for i, j in itertools.product(range(2), range(2)):
209 | plt.text(j, i, format(cm[i, j], fmt),
210 | horizontalalignment="center",
211 | color="white" if cm[i, j] > thresh else "black")
212 |
213 | plt.tight_layout()
214 | plt.ylabel('True Label')
215 | plt.xlabel('Predicted Label')
216 |
217 |
218 | def final_classifier_evaluation(clf,X_train, X_test, y_train, y_test):
219 |
220 | start_time = timeit.default_timer()
221 | clf.fit(X_train, y_train)
222 | end_time = timeit.default_timer()
223 | training_time = end_time - start_time
224 |
225 | start_time = timeit.default_timer()
226 | y_pred = clf.predict(X_test)
227 | end_time = timeit.default_timer()
228 | pred_time = end_time - start_time
229 |
230 | auc = roc_auc_score(y_test, y_pred)
231 | f1 = f1_score(y_test,y_pred)
232 | accuracy = accuracy_score(y_test,y_pred)
233 | precision = precision_score(y_test,y_pred)
234 | recall = recall_score(y_test,y_pred)
235 | cm = confusion_matrix(y_test,y_pred)
236 |
237 | print("Model Evaluation Metrics Using Untouched Test Dataset")
238 | print("*****************************************************")
239 | print("Model Training Time (s): "+"{:.5f}".format(training_time))
240 | print("Model Prediction Time (s): "+"{:.5f}\n".format(pred_time))
241 | print("F1 Score: "+"{:.2f}".format(f1))
242 | print("Accuracy: "+"{:.2f}".format(accuracy)+" AUC: "+"{:.2f}".format(auc))
243 | print("Precision: "+"{:.2f}".format(precision)+" Recall: "+"{:.2f}".format(recall))
244 | print("*****************************************************")
245 | plt.figure()
246 | plot_confusion_matrix(cm, classes=["0","1"], title='Confusion Matrix')
247 | plt.show()
248 |
249 |
250 | # # 3. The Fun Part: Machine Learning!
251 |
252 | # # Neural Network Classifier
253 |
254 | # This section will build a forward-feed neural network which computes weights via backpropagation (a multilayer perceptron). The main hyperparameter will be number of hidden nodes in a network defined by a single hidden layer, while others that could be searched over in grid search are activation function, and learning rate.
255 |
256 | # In[10]:
257 |
258 |
259 | from sklearn.neural_network import MLPClassifier
260 |
261 | def hyperNN(X_train, y_train, X_test, y_test, title):
262 |
263 | f1_test = []
264 | f1_train = []
265 | hlist = np.linspace(1,150,30).astype('int')
266 | for i in hlist:
267 | clf = MLPClassifier(hidden_layer_sizes=(i,), solver='adam', activation='logistic',
268 | learning_rate_init=0.05, random_state=100)
269 | clf.fit(X_train, y_train)
270 | y_pred_test = clf.predict(X_test)
271 | y_pred_train = clf.predict(X_train)
272 | f1_test.append(f1_score(y_test, y_pred_test))
273 | f1_train.append(f1_score(y_train, y_pred_train))
274 |
275 | plt.plot(hlist, f1_test, 'o-', color='r', label='Test F1 Score')
276 | plt.plot(hlist, f1_train, 'o-', color = 'b', label='Train F1 Score')
277 | plt.ylabel('Model F1 Score')
278 | plt.xlabel('No. Hidden Units')
279 |
280 | plt.title(title)
281 | plt.legend(loc='best')
282 | plt.tight_layout()
283 | plt.show()
284 |
285 |
286 | def NNGridSearchCV(X_train, y_train):
287 | #parameters to search:
288 | #number of hidden units
289 | #learning_rate
290 | h_units = [5, 10, 20, 30, 40, 50, 75, 100]
291 | learning_rates = [0.01, 0.05, .1]
292 | param_grid = {'hidden_layer_sizes': h_units, 'learning_rate_init': learning_rates}
293 |
294 | net = GridSearchCV(estimator = MLPClassifier(solver='adam',activation='logistic',random_state=100),
295 | param_grid=param_grid, cv=10)
296 | net.fit(X_train, y_train)
297 | print("Per Hyperparameter tuning, best parameters are:")
298 | print(net.best_params_)
299 | return net.best_params_['hidden_layer_sizes'], net.best_params_['learning_rate_init']
300 |
301 |
302 | # In[10]:
303 |
304 |
305 | phishX,phishY,bankX,bankY = import_data()
306 |
307 | X_train, X_test, y_train, y_test = train_test_split(np.array(phishX),np.array(phishY), test_size=0.20)
308 | hyperNN(X_train, y_train, X_test, y_test,title="Model Complexity Curve for NN (Phishing Data)\nHyperparameter : No. Hidden Units")
309 | h_units, learn_rate = NNGridSearchCV(X_train, y_train)
310 | estimator_phish = MLPClassifier(hidden_layer_sizes=(h_units,), solver='adam', activation='logistic',
311 | learning_rate_init=learn_rate, random_state=100)
312 | train_samp_phish, NN_train_score_phish, NN_fit_time_phish, NN_pred_time_phish = plot_learning_curve(estimator_phish, X_train, y_train,title="Neural Net Phishing Data")
313 | final_classifier_evaluation(estimator_phish, X_train, X_test, y_train, y_test)
314 |
315 | X_train, X_test, y_train, y_test = train_test_split(np.array(bankX),np.array(bankY), test_size=0.20)
316 | hyperNN(X_train, y_train, X_test, y_test,title="Model Complexity Curve for NN (Banking Data)\nHyperparameter : No. Hidden Units")
317 | h_units, learn_rate = NNGridSearchCV(X_train, y_train)
318 | estimator_bank = MLPClassifier(hidden_layer_sizes=(h_units,), solver='adam', activation='logistic',
319 | learning_rate_init=learn_rate, random_state=100)
320 | train_samp_bank, NN_train_score_bank, NN_fit_time_bank, NN_pred_time_bank = plot_learning_curve(estimator_bank, X_train, y_train,title="Neural Net Banking Data")
321 | final_classifier_evaluation(estimator_bank, X_train, X_test, y_train, y_test)
322 |
323 |
324 | # The final section for neural network will plot the loss curve for each dataset over the iterations.
325 |
326 | # In[17]:
327 |
328 |
329 | phishX,phishY,bankX,bankY = import_data()
330 |
331 | X_train, X_test, y_train, y_test = train_test_split(np.array(phishX),np.array(phishY), test_size=0.20)
332 | estimator_phish = MLPClassifier(hidden_layer_sizes=(50,), solver='adam', activation='logistic',
333 | learning_rate_init=0.05, random_state=100)
334 | estimator_phish.fit(X_train, y_train)
335 | a = estimator_phish.loss_curve_
336 |
337 | X_train, X_test, y_train, y_test = train_test_split(np.array(bankX),np.array(bankY), test_size=0.20)
338 | estimator_bank = MLPClassifier(hidden_layer_sizes=(5,), solver='adam', activation='logistic',
339 | learning_rate_init=0.01, random_state=100)
340 | estimator_bank.fit(X_train, y_train)
341 | b = estimator_bank.loss_curve_
342 |
343 | plt.figure()
344 | plt.title("Loss Curve")
345 | plt.xlabel("Number of Iterations")
346 | plt.ylabel("Loss")
347 | plt.plot(a, 'o-', color="b", label="Phishing Data")
348 | plt.plot(b, 'o-', color="r", label="Banking Data")
349 | plt.legend(loc="best")
350 | plt.show()
351 |
352 |
353 | # This section will create a surface plot for the Phishing data to see where the local and global maxima occur for different numbers of hidden units for a network with 2 hidden layers.
354 |
355 | # In[11]:
356 |
357 |
358 | # from mpl_toolkits.mplot3d import Axes3D
359 |
360 | # def runNeural(XTrain,YTrain,XTest,YTest,learn_rate,sz1,sz2,testit=True,times=1):
361 | # for mm in range(1,times+1):
362 | # #clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
363 | # clf = MLPClassifier(learning_rate_init=learn_rate,activation='logistic', hidden_layer_sizes=(sz1, sz2), random_state=100)
364 | # clf.fit(XTrain, YTrain)
365 | # if testit:
366 | # mmm = clf.predict(XTest)
367 | # return((sum(mmm == YTest) / len(mmm))/times)
368 | # return(0)
369 |
370 | # X_train, X_test, y_train, y_test = train_test_split(np.array(bankX),np.array(bankY), test_size=0.20)
371 |
372 | # trackertot2 = [[0]*5]*5
373 | # nspot = 10
374 | # trackertot = np.zeros((nspot,nspot))
375 | # xtot = []
376 | # ytot = []
377 | # maxtrack = 0
378 | # x_vals = np.linspace(1,50,nspot).astype('int')
379 | # y_vals = np.linspace(1,50,nspot).astype('int')
380 | # for x in range(1,nspot+1):
381 | # xi = x_vals[x-1]
382 | # for y in range(1,nspot+1):
383 | # yi = y_vals[y-1]
384 | # tracker = (runNeural(X_train,y_train,X_test,y_test,0.05,xi,yi))
385 | # trackertot[x-1][y-1]=tracker
386 | # if(tracker>maxtrack):
387 | # maxtrack = tracker
388 |
389 | # X, Y = np.meshgrid(x_vals, y_vals)
390 | # fig = plt.figure()
391 | # # Add an axes
392 | # ax = fig.add_subplot(111,projection='3d')
393 | # # plot the surface
394 | # ax.plot_surface(X, Y, np.array(trackertot), alpha=0.2)
395 |
396 | # plt.show()
397 | # plt.clf()
398 | # plt.cla()
399 | # plt.close()
400 |
401 |
402 | # # Support Vector Machine Classifier
403 |
404 | # This section will build a support vector machine classifier. The primary hyperparameter will be kernel function: linear, polynomial, rbf (radial basis function), and sigmoid. We will also explore the penalty term 'C' and the kernel coefficient 'gamma'.
405 |
406 | # In[22]:
407 |
408 |
409 | from sklearn.svm import SVC
410 |
411 | def hyperSVM(X_train, y_train, X_test, y_test, title):
412 |
413 | f1_test = []
414 | f1_train = []
415 | kernel_func = ['linear','poly','rbf','sigmoid']
416 | for i in kernel_func:
417 | if i == 'poly':
418 | for j in [2,3,4,5,6,7,8]:
419 | clf = SVC(kernel=i, degree=j,random_state=100)
420 | clf.fit(X_train, y_train)
421 | y_pred_test = clf.predict(X_test)
422 | y_pred_train = clf.predict(X_train)
423 | f1_test.append(f1_score(y_test, y_pred_test))
424 | f1_train.append(f1_score(y_train, y_pred_train))
425 | else:
426 | clf = SVC(kernel=i, random_state=100)
427 | clf.fit(X_train, y_train)
428 | y_pred_test = clf.predict(X_test)
429 | y_pred_train = clf.predict(X_train)
430 | f1_test.append(f1_score(y_test, y_pred_test))
431 | f1_train.append(f1_score(y_train, y_pred_train))
432 |
433 | xvals = ['linear','poly2','poly3','poly4','poly5','poly6','poly7','poly8','rbf','sigmoid']
434 | plt.plot(xvals, f1_test, 'o-', color='r', label='Test F1 Score')
435 | plt.plot(xvals, f1_train, 'o-', color = 'b', label='Train F1 Score')
436 | plt.ylabel('Model F1 Score')
437 | plt.xlabel('Kernel Function')
438 |
439 | plt.title(title)
440 | plt.legend(loc='best')
441 | plt.tight_layout()
442 | plt.show()
443 |
444 | def SVMGridSearchCV(X_train, y_train):
445 | #parameters to search:
446 | #penalty parameter, C
447 | #
448 | Cs = [1e-4, 1e-3, 1e-2, 1e01, 1]
449 | gammas = [1,10,100]
450 | param_grid = {'C': Cs, 'gamma': gammas}
451 |
452 | clf = GridSearchCV(estimator = SVC(kernel='rbf',random_state=100),
453 | param_grid=param_grid, cv=10)
454 | clf.fit(X_train, y_train)
455 | print("Per Hyperparameter tuning, best parameters are:")
456 | print(clf.best_params_)
457 | return clf.best_params_['C'], clf.best_params_['gamma']
458 |
459 |
460 | # In[23]:
461 |
462 |
463 | phishX,phishY,bankX,bankY = import_data()
464 |
465 | X_train, X_test, y_train, y_test = train_test_split(np.array(phishX),np.array(phishY), test_size=0.20)
466 | hyperSVM(X_train, y_train, X_test, y_test,title="Model Complexity Curve for SVM (Phishing Data)\nHyperparameter : Kernel Function")
467 | C_val, gamma_val = SVMGridSearchCV(X_train, y_train)
468 | estimator_phish = SVC(C=C_val, gamma=gamma_val, kernel='rbf', random_state=100)
469 | train_samp_phish, SVM_train_score_phish, SVM_fit_time_phish, SVM_pred_time_phish = plot_learning_curve(estimator_phish, X_train, y_train,title="SVM Phishing Data")
470 | final_classifier_evaluation(estimator_phish, X_train, X_test, y_train, y_test)
471 |
472 | X_train, X_test, y_train, y_test = train_test_split(np.array(bankX),np.array(bankY), test_size=0.20)
473 | hyperSVM(X_train, y_train, X_test, y_test,title="Model Complexity Curve for SVM (Banking Data)\nHyperparameter : Kernel Function")
474 | C_val, gamma_val = SVMGridSearchCV(X_train, y_train)
475 | estimator_bank = SVC(C=C_val, gamma=gamma_val, kernel='rbf', random_state=100)
476 | estimator_bank = SVC(kernel='rbf', random_state=100)
477 | train_samp_bank, SVM_train_score_bank, SVM_fit_time_bank, SVM_pred_time_bank = plot_learning_curve(estimator_bank, X_train, y_train,title="SVM Banking Data")
478 | final_classifier_evaluation(estimator_bank, X_train, X_test, y_train, y_test)
479 |
480 |
481 | # # KNN Classifier
482 |
483 | # This section will build a classifier using K-nearest neighbors. The hyperparameter will be n_neighbors. One could easily add another hyperparameter is the distance metric, but for simplicity, and for the sake of running the code faster, I won't explore this. I will use the standard euclidean distance for all models. The Model Complexity curve will show F1 score as a function of number of neighbors.
484 |
485 | # In[24]:
486 |
487 |
488 | from sklearn.neighbors import KNeighborsClassifier as kNN
489 |
490 | def hyperKNN(X_train, y_train, X_test, y_test, title):
491 |
492 | f1_test = []
493 | f1_train = []
494 | klist = np.linspace(1,250,25).astype('int')
495 | for i in klist:
496 | clf = kNN(n_neighbors=i,n_jobs=-1)
497 | clf.fit(X_train,y_train)
498 | y_pred_test = clf.predict(X_test)
499 | y_pred_train = clf.predict(X_train)
500 | f1_test.append(f1_score(y_test, y_pred_test))
501 | f1_train.append(f1_score(y_train, y_pred_train))
502 |
503 | plt.plot(klist, f1_test, 'o-', color='r', label='Test F1 Score')
504 | plt.plot(klist, f1_train, 'o-', color = 'b', label='Train F1 Score')
505 | plt.ylabel('Model F1 Score')
506 | plt.xlabel('No. Neighbors')
507 |
508 | plt.title(title)
509 | plt.legend(loc='best')
510 | plt.tight_layout()
511 | plt.show()
512 |
513 |
514 |
515 | # In[15]:
516 |
517 |
518 | phishX,phishY,bankX,bankY = import_data()
519 |
520 | X_train, X_test, y_train, y_test = train_test_split(np.array(phishX),np.array(phishY), test_size=0.20)
521 | hyperKNN(X_train, y_train, X_test, y_test,title="Model Complexity Curve for kNN (Phishing Data)\nHyperparameter : No. Neighbors")
522 | estimator_phish = kNN(n_neighbors=20, n_jobs=-1)
523 | train_samp_phish, kNN_train_score_phish, kNN_fit_time_phish, kNN_pred_time_phish = plot_learning_curve(estimator_phish, X_train, y_train,title="kNN Phishing Data")
524 | final_classifier_evaluation(estimator_phish, X_train, X_test, y_train, y_test)
525 |
526 | X_train, X_test, y_train, y_test = train_test_split(np.array(bankX),np.array(bankY), test_size=0.20)
527 | hyperKNN(X_train, y_train, X_test, y_test,title="Model Complexity Curve for kNN (Banking Data)\nHyperparameter : No. Neighbors")
528 | estimator_bank = kNN(n_neighbors=10, n_jobs=-1)
529 | train_samp_bank, kNN_train_score_bank, kNN_fit_time_bank, kNN_pred_time_bank = plot_learning_curve(estimator_bank, X_train, y_train,title="kNN Banking Data")
530 | final_classifier_evaluation(estimator_bank, X_train, X_test, y_train, y_test)
531 |
532 |
533 | # # Decision Tree Classifier
534 |
535 | # This section will build a Decision Tree Classifier using information gain (based on entropy) to determine the best feature split per the ID3 algorithm. The model will be pre-pruned by limiting tree depth using the hyperparameter 'max_depth' and by ensuring that each leaf (a terminal node on the tree) has at least 'min_samples_leaf'.
536 |
537 | # In[16]:
538 |
539 |
540 | from sklearn.tree import DecisionTreeClassifier
541 |
542 | def hyperTree(X_train, y_train, X_test, y_test, title):
543 |
544 | f1_test = []
545 | f1_train = []
546 | max_depth = list(range(1,31))
547 | for i in max_depth:
548 | clf = DecisionTreeClassifier(max_depth=i, random_state=100, min_samples_leaf=1, criterion='entropy')
549 | clf.fit(X_train, y_train)
550 | y_pred_test = clf.predict(X_test)
551 | y_pred_train = clf.predict(X_train)
552 | f1_test.append(f1_score(y_test, y_pred_test))
553 | f1_train.append(f1_score(y_train, y_pred_train))
554 |
555 | plt.plot(max_depth, f1_test, 'o-', color='r', label='Test F1 Score')
556 | plt.plot(max_depth, f1_train, 'o-', color = 'b', label='Train F1 Score')
557 | plt.ylabel('Model F1 Score')
558 | plt.xlabel('Max Tree Depth')
559 |
560 | plt.title(title)
561 | plt.legend(loc='best')
562 | plt.tight_layout()
563 | plt.show()
564 |
565 |
566 | def TreeGridSearchCV(start_leaf_n, end_leaf_n, X_train, y_train):
567 | #parameters to search:
568 | #20 values of min_samples leaf from 0.5% sample to 5% of the training data
569 | #20 values of max_depth from 1, 20
570 | param_grid = {'min_samples_leaf':np.linspace(start_leaf_n,end_leaf_n,20).round().astype('int'), 'max_depth':np.arange(1,20)}
571 |
572 | tree = GridSearchCV(estimator = DecisionTreeClassifier(), param_grid=param_grid, cv=10)
573 | tree.fit(X_train, y_train)
574 | print("Per Hyperparameter tuning, best parameters are:")
575 | print(tree.best_params_)
576 | return tree.best_params_['max_depth'], tree.best_params_['min_samples_leaf']
577 |
578 |
579 | # In[17]:
580 |
581 |
582 | phishX,phishY,bankX,bankY = import_data()
583 |
584 | X_train, X_test, y_train, y_test = train_test_split(np.array(phishX),np.array(phishY), test_size=0.20)
585 | hyperTree(X_train, y_train, X_test, y_test,title="Model Complexity Curve for Decision Tree (Phishing Data)\nHyperparameter : Tree Max Depth")
586 | start_leaf_n = round(0.005*len(X_train))
587 | end_leaf_n = round(0.05*len(X_train)) #leaf nodes of size [0.5%, 5% will be tested]
588 | max_depth, min_samples_leaf = TreeGridSearchCV(start_leaf_n,end_leaf_n,X_train,y_train)
589 | estimator_phish = DecisionTreeClassifier(max_depth=max_depth, min_samples_leaf=min_samples_leaf, random_state=100, criterion='entropy')
590 | train_samp_phish, DT_train_score_phish, DT_fit_time_phish, DT_pred_time_phish = plot_learning_curve(estimator_phish, X_train, y_train,title="Decision Tree Phishing Data")
591 | final_classifier_evaluation(estimator_phish, X_train, X_test, y_train, y_test)
592 |
593 | X_train, X_test, y_train, y_test = train_test_split(np.array(bankX),np.array(bankY), test_size=0.20)
594 | hyperTree(X_train, y_train, X_test, y_test,title="Model Complexity Curve for Decision Tree (Banking Data)\nHyperparameter : Tree Max Depth")
595 | start_leaf_n = round(0.005*len(X_train))
596 | end_leaf_n = round(0.05*len(X_train)) #leaf nodes of size [0.5%, 5% will be tested]
597 | max_depth, min_samples_leaf = TreeGridSearchCV(start_leaf_n,end_leaf_n,X_train,y_train)
598 | estimator_bank = DecisionTreeClassifier(max_depth=max_depth, min_samples_leaf=min_samples_leaf, random_state=100, criterion='entropy')
599 | train_samp_bank, DT_train_score_bank, DT_fit_time_bank, DT_pred_time_bank = plot_learning_curve(estimator_bank, X_train, y_train,title="Decision Tree Banking Data")
600 | final_classifier_evaluation(estimator_bank, X_train, X_test, y_train, y_test)
601 |
602 |
603 | # # Boosted Decision Tree Classifier
604 |
605 | # This section will implement a boosted version of the earlier decision tree. We will still keep the pruning based on max_depth and min_samples_leaf, but the cutoff thresholds will be more aggressive (lower) since the power of boosting is to combine multiple weak learners. We also introduce the hyperparameter of n_estimators and learning rate which will determine the contribution of each tree classifier.
606 |
607 | # In[18]:
608 |
609 |
610 | from sklearn.ensemble import GradientBoostingClassifier
611 |
612 | def hyperBoost(X_train, y_train, X_test, y_test, max_depth, min_samples_leaf, title):
613 |
614 | f1_test = []
615 | f1_train = []
616 | n_estimators = np.linspace(1,250,40).astype('int')
617 | for i in n_estimators:
618 | clf = GradientBoostingClassifier(n_estimators=i, max_depth=int(max_depth/2),
619 | min_samples_leaf=int(min_samples_leaf/2), random_state=100,)
620 | clf.fit(X_train, y_train)
621 | y_pred_test = clf.predict(X_test)
622 | y_pred_train = clf.predict(X_train)
623 | f1_test.append(f1_score(y_test, y_pred_test))
624 | f1_train.append(f1_score(y_train, y_pred_train))
625 |
626 | plt.plot(n_estimators, f1_test, 'o-', color='r', label='Test F1 Score')
627 | plt.plot(n_estimators, f1_train, 'o-', color = 'b', label='Train F1 Score')
628 | plt.ylabel('Model F1 Score')
629 | plt.xlabel('No. Estimators')
630 |
631 | plt.title(title)
632 | plt.legend(loc='best')
633 | plt.tight_layout()
634 | plt.show()
635 |
636 | def BoostedGridSearchCV(start_leaf_n, end_leaf_n, X_train, y_train):
637 | #parameters to search:
638 | #n_estimators, learning_rate, max_depth, min_samples_leaf
639 | param_grid = {'min_samples_leaf': np.linspace(start_leaf_n,end_leaf_n,3).round().astype('int'),
640 | 'max_depth': np.arange(1,4),
641 | 'n_estimators': np.linspace(10,100,3).round().astype('int'),
642 | 'learning_rate': np.linspace(.001,.1,3)}
643 |
644 | boost = GridSearchCV(estimator = GradientBoostingClassifier(), param_grid=param_grid, cv=10)
645 | boost.fit(X_train, y_train)
646 | print("Per Hyperparameter tuning, best parameters are:")
647 | print(boost.best_params_)
648 | return boost.best_params_['max_depth'], boost.best_params_['min_samples_leaf'], boost.best_params_['n_estimators'], boost.best_params_['learning_rate']
649 |
650 |
651 | # In[19]:
652 |
653 |
654 | phishX,phishY,bankX,bankY = import_data()
655 |
656 | X_train, X_test, y_train, y_test = train_test_split(np.array(phishX),np.array(phishY), test_size=0.20)
657 | hyperBoost(X_train, y_train, X_test, y_test, 3, 50, title="Model Complexity Curve for Boosted Tree (Phishing Data)\nHyperparameter : No. Estimators")
658 | start_leaf_n = round(0.005*len(X_train))
659 | end_leaf_n = round(0.05*len(X_train)) #leaf nodes of size [0.5%, 5% will be tested]
660 | max_depth, min_samples_leaf, n_est, learn_rate = BoostedGridSearchCV(start_leaf_n,end_leaf_n,X_train,y_train)
661 | estimator_phish = GradientBoostingClassifier(max_depth=max_depth, min_samples_leaf=min_samples_leaf,
662 | n_estimators=n_est, learning_rate=learn_rate, random_state=100)
663 | train_samp_phish, BT_train_score_phish, BT_fit_time_phish, BT_pred_time_phish = plot_learning_curve(estimator_phish, X_train, y_train,title="Boosted Tree Phishing Data")
664 | final_classifier_evaluation(estimator_phish, X_train, X_test, y_train, y_test)
665 |
666 | X_train, X_test, y_train, y_test = train_test_split(np.array(bankX),np.array(bankY), test_size=0.20)
667 | hyperBoost(X_train, y_train, X_test, y_test, 3, 50, title="Model Complexity Curve for Boosted Tree (Banking Data)\nHyperparameter : No. Estimators")
668 | start_leaf_n = round(0.005*len(X_train))
669 | end_leaf_n = round(0.05*len(X_train)) #leaf nodes of size [0.5%, 5% will be tested]
670 | max_depth, min_samples_leaf, n_est, learn_rate = BoostedGridSearchCV(start_leaf_n,end_leaf_n,X_train,y_train)
671 | estimator_bank = GradientBoostingClassifier(max_depth=max_depth, min_samples_leaf=min_samples_leaf,
672 | n_estimators=n_est, learning_rate=learn_rate, random_state=100)
673 | train_samp_bank, BT_train_score_bank, BT_fit_time_bank, BT_pred_time_bank = plot_learning_curve(estimator_bank, X_train, y_train,title="Boosted Tree Banking Data")
674 | final_classifier_evaluation(estimator_bank, X_train, X_test, y_train, y_test)
675 |
676 |
677 | # # 4. Model Comparison Plots
678 |
679 | # Finally, let's define and call a function that will plot training times and learning rates for the 5 different algorithms so that we can compare across the classifiers for the same dataset.
680 |
681 | # In[23]:
682 |
683 |
684 | def compare_fit_time(n,NNtime, SMVtime, kNNtime, DTtime, BTtime, title):
685 |
686 | plt.figure()
687 | plt.title("Model Training Times: " + title)
688 | plt.xlabel("Training Examples")
689 | plt.ylabel("Model Training Time (s)")
690 | plt.plot(n, NNtime, '-', color="b", label="Neural Network")
691 | plt.plot(n, SMVtime, '-', color="r", label="SVM")
692 | plt.plot(n, kNNtime, '-', color="g", label="kNN")
693 | plt.plot(n, DTtime, '-', color="m", label="Decision Tree")
694 | plt.plot(n, BTtime, '-', color="k", label="Boosted Tree")
695 | plt.legend(loc="best")
696 | plt.show()
697 |
698 | def compare_pred_time(n,NNpred, SMVpred, kNNpred, DTpred, BTpred, title):
699 |
700 | plt.figure()
701 | plt.title("Model Prediction Times: " + title)
702 | plt.xlabel("Training Examples")
703 | plt.ylabel("Model Prediction Time (s)")
704 | plt.plot(n, NNpred, '-', color="b", label="Neural Network")
705 | plt.plot(n, SMVpred, '-', color="r", label="SVM")
706 | plt.plot(n, kNNpred, '-', color="g", label="kNN")
707 | plt.plot(n, DTpred, '-', color="m", label="Decision Tree")
708 | plt.plot(n, BTpred, '-', color="k", label="Boosted Tree")
709 | plt.legend(loc="best")
710 | plt.show()
711 |
712 |
713 | def compare_learn_time(n,NNlearn, SMVlearn, kNNlearn, DTlearn, BTlearn, title):
714 |
715 | plt.figure()
716 | plt.title("Model Learning Rates: " + title)
717 | plt.xlabel("Training Examples")
718 | plt.ylabel("Model F1 Score")
719 | plt.plot(n, NNlearn, '-', color="b", label="Neural Network")
720 | plt.plot(n, SMVlearn, '-', color="r", label="SVM")
721 | plt.plot(n, kNNlearn, '-', color="g", label="kNN")
722 | plt.plot(n, DTlearn, '-', color="m", label="Decision Tree")
723 | plt.plot(n, BTlearn, '-', color="k", label="Boosted Tree")
724 | plt.legend(loc="best")
725 | plt.show()
726 |
727 |
728 | # In[24]:
729 |
730 |
731 | compare_fit_time(train_samp_phish, NN_fit_time_phish, SVM_fit_time_phish, kNN_fit_time_phish,
732 | DT_fit_time_phish, BT_fit_time_phish, 'Phishing Dataset')
733 | compare_pred_time(train_samp_phish, NN_pred_time_phish, SVM_pred_time_phish, kNN_pred_time_phish,
734 | DT_pred_time_phish, BT_pred_time_phish, 'Phishing Dataset')
735 | compare_learn_time(train_samp_phish, NN_train_score_phish, SVM_train_score_phish, kNN_train_score_phish,
736 | DT_train_score_phish, BT_train_score_phish, 'Phishing Dataset')
737 |
738 |
739 |
740 | compare_fit_time(train_samp_bank, NN_fit_time_bank, SVM_fit_time_bank, kNN_fit_time_bank,
741 | DT_fit_time_bank, BT_fit_time_bank, 'Banking Dataset')
742 | compare_pred_time(train_samp_bank, NN_pred_time_bank, SVM_pred_time_bank, kNN_pred_time_bank,
743 | DT_pred_time_bank, BT_pred_time_bank, 'Banking Dataset')
744 | compare_learn_time(train_samp_bank, NN_train_score_bank, SVM_train_score_bank, kNN_train_score_bank,
745 | DT_train_score_bank, BT_train_score_bank, 'Banking Dataset')
746 |
747 |
--------------------------------------------------------------------------------