Collections.sort(population); // Método de ordenamiento



Similar documents
System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

J a v a Quiz (Unit 3, Test 0 Practice)

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Topic 11 Scanner object, conditional execution

Building Java Programs

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Sample CSE8A midterm Multiple Choice (circle one)

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

ISSN: ISO 9001:2008 Certified International Journal of Engineering Science and Innovative Technology (IJESIT) Volume 2, Issue 3, May 2013

Part I. Multiple Choice Questions (2 points each):

CS170 Lab 11 Abstract Data Types & Objects

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Building Java Programs

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

2009 Tutorial (DB4O and Visual Studio 2008 Express)

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Building Java Programs

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Software Development with UML and Java 2 SDJ I2, Spring 2010

Introduction to Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

This explanations are for absolute beginners. Skilled programmers should (and probably will) use more effective approach.

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

Chapter 2 Introduction to Java programming

Building Java Programs

AP Computer Science Java Subset

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Contents. 9-1 Copyright (c) N. Afshartous

(Eng. Hayam Reda Seireg) Sheet Java

Java Programming Fundamentals

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

The following program is aiming to extract from a simple text file an analysis of the content such as:

1 Hour, Closed Notes, Browser open to Java API docs is OK

Object Oriented Software Design

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Java Programming Language

Arrays in Java. Working with Arrays

Adding HTML5 to your Android applications. Martin Gunnarsson & Pär Sikö

JAVA ARRAY EXAMPLE PDF

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Programmierpraktikum

public void creditaccount(string accountnumber, float amount) { this.accounts.get(accountnumber).credit(amount); }

Inheritance, overloading and overriding

Example of a Java program

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CS114: Introduction to Java

6 Creating the Animation

Using ilove SharePoint Web Services Workflow Action

Java CPD (I) Frans Coenen Department of Computer Science

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

>

Files and input/output streams

JAVA - MULTITHREADING

Statements and Control Flow

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

3 length + 23 size = 2

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

java Features Version April 19, 2013 by Thorsten Kracht

Introduction to Object-Oriented Programming

Loops and ArrayLists

Java: overview by example

Konzepte objektorientierter Programmierung

CS 111 Classes I 1. Software Organization View to this point:

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Building Java Programs

1.204 Lecture 10. Greedy algorithms: Job scheduling. Greedy method

13 File Output and Input

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

CSE 1020 Introduction to Computer Science I A sample nal exam

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Unit 6. Loop statements

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

JAVA Program For Processing SMS Messages

CSE 2221 Midterm Exam #1 SAMPLE

Introduction To Genetic Algorithms

Comp 248 Introduction to Programming

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

CS 1302 Ch 19, Binary I/O

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Event-Driven Programming

Continuous Integration Part 2

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

CS 121 Intro to Programming:Java - Lecture 11 Announcements

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Introducing Variance into the Java Programming Language DRAFT

Chapter 5. Recursion. Data Structures and Algorithms in Java

Week 1: Review of Java Programming Basics

AP Computer Science Java Mr. Clausen Program 9A, 9B

Primitive data types in Java

Transcription:

import java.util.collections; import java.util.linkedlist; import java.util.random; public class GeneticAlgorithms static long BEGIN; static final boolean _DEBUG = true; LinkedList<Candidate> population = new LinkedList<Candidate>(); final Random rand = new Random(); final int populationsize = 10; final int parentusepercent = 10; public GeneticAlgorithms() for (int i = 0; i < populationsize; i++) Candidate c = new Candidate(); c.random(); population.add(c); Collections.sort(population); // Método de ordenamiento System.out.println("Inicializa población ordenada");

print(); void print() System.out.println("-- print"); for (Candidate c : population) System.out.println(c); /** * Estrategia de seleccion: Tournament method * Estrategia de reemplazo: elitism 10% * and steady state find 4 random in population not same let 2 fight, and 2 * fight the winners makes 2 children */ void producenextgen() LinkedList<Candidate> newpopulation = new LinkedList<Candidate>(); while (newpopulation.size() < populationsize * (1.0 - (parentusepercent / 100.0))) int size = population.size(); int i = rand.nextint(size); int j, k, l;

j = k = l = i; while (j == i) j = rand.nextint(size); while (k == i k == j) k = rand.nextint(size); while (l == i l == j k == l) l = rand.nextint(size); Candidate c1 = population.get(i); Candidate c2 = population.get(j); Candidate c3 = population.get(k); Candidate c4 = population.get(l); int f1 = c1.fitness(); int f2 = c2.fitness(); int f3 = c3.fitness(); int f4 = c4.fitness(); Candidate w1, w2; if (f1 > f2) w1 = c1; else w1 = c2;

if (f3 > f4) w2 = c3; else w2 = c4; Candidate child1, child2; // Method one-point crossover random pivot // int pivot = rand.nextint(candidate.size-2) + 1; // cut interval // is 1.. size-1 // child1 = newchild(w1,w2,pivot); // child2 = newchild(w2,w1,pivot); // Method uniform crossover Candidate[] childs = newchilds(w1, w2); child1 = childs[0]; child2 = childs[1]; double mutatepercent = 0.01; boolean m1 = rand.nextfloat() <= mutatepercent; boolean m2 = rand.nextfloat() <= mutatepercent; if (m1) mutate(child1); if (m2) mutate(child2);

boolean ischild1good = child1.fitness() >= w1.fitness(); boolean ischild2good = child2.fitness() >= w2.fitness(); newpopulation.add(ischild1good? child1 : w1); newpopulation.add(ischild2good? child2 : w2); // add top percent parent int j = (int) (populationsize * parentusepercent / 100.0); for (int i = 0; i < j; i++) newpopulation.add(population.get(i)); population = newpopulation; Collections.sort(population); // Cruza a partir de un pivote Candidate newchild(candidate c1, Candidate c2, int pivot) Candidate child = new Candidate(); for (int i = 0; i < pivot; i++) child.genotype[i] = c1.genotype[i];

for (int j = pivot; j < Candidate.SIZE; j++) child.genotype[j] = c2.genotype[j]; return child; // Cruza uniforme Candidate[] newchilds(candidate c1, Candidate c2) Candidate child1 = new Candidate(); Candidate child2 = new Candidate(); for (int i = 0; i < Candidate.SIZE; i++) boolean b = rand.nextfloat() >= 0.5; if (b) child1.genotype[i] = c1.genotype[i]; child2.genotype[i] = c2.genotype[i]; else child1.genotype[i] = c2.genotype[i]; child2.genotype[i] = c1.genotype[i];

return new Candidate[] child1, child2 ; void mutate(candidate c) int i = rand.nextint(candidate.size); c.genotype[i] =!c.genotype[i]; // flip public static void main(string[] args) BEGIN = System.currentTimeMillis(); GeneticAlgorithms ga = new GeneticAlgorithms(); ga.run(); long END = System.currentTimeMillis(); System.out.println("Tiempo: " + (END - BEGIN) / 1000.0 + " sec."); void run() final int maxsteps = 50000; int count = 0;

while (count < maxsteps) count++; producenextgen(); System.out.println("\nResultado"); print(); import java.util.random; public class Candidate implements Comparable<Candidate> public static final int SIZE = 10; public boolean[] genotype; final Random rand = new Random(); public Candidate() genotype = new boolean[size]; void random()

for (int i = 0; i < genotype.length; i++) genotype[i] = 0.5 > rand.nextfloat(); private String gene() StringBuilder sb = new StringBuilder(); for (int i = 0; i < genotype.length; i++) sb.append(genotype[i] == true? 1 : 0); return sb.tostring(); int fitness() int sum = 0; for (int i = 0; i < genotype.length; i++) if (genotype[i]) sum++; return sum;

public int compareto(candidate o) int f1 = this.fitness(); int f2 = o.fitness(); if (f1 < f2) return 1; else if (f1 > f2) return -1; else return 0; @Override public String tostring() return "gene=" + gene() + " fit=" + fitness();