SlideShare a Scribd company logo
1 of 83
Download to read offline
Inteligência Artificial
Aula 4
Nauber Gois
SIMULATED ANNEALING
MODELAGEM DO PROBLEMA
MODELAGEM DO PROBLEMA
public class SimulatedAnnealing {
private static Travel travel = new Travel(10);
public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double
coolingRate) {
System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " +
numberOfIterations + " and colling rate: " + coolingRate);
double t = startingTemperature;
travel.generateInitialTravel();
double bestDistance = travel.getDistance();
System.out.println("Initial distance of travel: " + bestDistance);
Travel bestSolution = travel;
Travel currentSolution = bestSolution;
for (int i = 0; i < numberOfIterations; i++) {
if (t > 0.1) {
currentSolution.swapCities();
double currentDistance = currentSolution.getDistance();
if (currentDistance < bestDistance) {
bestDistance = currentDistance;
} else if (Math.exp((bestDistance - currentDistance) / t) < Math.random()) {
currentSolution.revertSwap();
}
t *= coolingRate;
} else {
continue;
}
if (i % 100 == 0) {
System.out.println("Iteration #" + i);
}
}
return bestDistance;
}
Main APP
Algoritmos Genéticos
@Data
public class Individual {
protected int defaultGeneLength = 64;
private byte[] genes = new
byte[defaultGeneLength];
private int fitness = 0;
public Individual() {
for (int i = 0; i < genes.length; i++) {
byte gene = (byte)
Math.round(Math.random());
genes[i] = gene;
}
}
protected byte getSingleGene(int index) {
return genes[index];
}
protected void setSingleGene(int index, byte value) {
genes[index] = value;
fitness = 0;
}
public int getFitness() {
if (fitness == 0) {
fitness =
SimpleGeneticAlgorithm.getFitness(this);
}
return fitness;
}
@Override
public String toString() {
String geneString = "";
for (int i = 0; i < genes.length; i++) {
geneString += getSingleGene(i);
}
return geneString;
}
public class Population {
private List<Individual> individuals;
public Population(int size, boolean createNew) {
individuals = new ArrayList<>();
if (createNew) {
createNewPopulation(size);
}
}
protected Individual getIndividual(int index) {
return individuals.get(index);
}
protected Individual getFittest() {
Individual fittest = individuals.get(0);
for (int i = 0; i < individuals.size(); i++) {
if (fittest.getFitness() <= getIndividual(i).getFitness()) {
fittest = getIndividual(i);
}
}
return fittest;
}
private void createNewPopulation(int size) {
for (int i = 0; i < size; i++) {
Individual newIndividual = new Individual();
individuals.add(i, newIndividual);
}
}
}
public class SimpleGeneticAlgorithm {
private static final double uniformRate = 0.5;
private static final double mutationRate = 0.025;
private static final int tournamentSize = 5;
private static final boolean elitism = true;
private static byte[] solution = new byte[64];
public boolean runAlgorithm(int populationSize, String solution) {
if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
throw new RuntimeException("The solution needs to have " +
SimpleGeneticAlgorithm.solution.length + " bytes");
}
setSolution(solution);
Population myPop = new Population(populationSize, true);
int generationCount = 1;
while (myPop.getFittest().getFitness() < getMaxFitness()) {
System.out.println("Generation: " + generationCount + " Correct
genes found: " + myPop.getFittest().getFitness());
myPop = evolvePopulation(myPop);
generationCount++;
}
System.out.println("Solution found!");
System.out.println("Generation: " + generationCount);
System.out.println("Genes: ");
System.out.println(myPop.getFittest());
return true;
}
public Population evolvePopulation(Population pop) {
int elitismOffset;
Population newPopulation = new Population(pop.getIndividuals().size(), false);
if (elitism) {
newPopulation.getIndividuals().add(0, pop.getFittest());
elitismOffset = 1;
} else {
elitismOffset = 0;
}
for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) {
Individual indiv1 = tournamentSelection(pop);
Individual indiv2 = tournamentSelection(pop);
Individual newIndiv = crossover(indiv1, indiv2);
newPopulation.getIndividuals().add(i, newIndiv);
}
for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) {
mutate(newPopulation.getIndividual(i));
}
return newPopulation;
}
private Individual crossover(Individual indiv1, Individual
indiv2) {
Individual newSol = new Individual();
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
if (Math.random() <= uniformRate) {
newSol.setSingleGene(i, indiv1.getSingleGene(i));
} else {
newSol.setSingleGene(i, indiv2.getSingleGene(i));
}
}
return newSol;
}
private void mutate(Individual indiv) {
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
if (Math.random() <= mutationRate) {
byte gene = (byte) Math.round(Math.random());
indiv.setSingleGene(i, gene);
}
}
}
private Individual tournamentSelection(Population pop) {
Population tournament = new Population(tournamentSize, false);
for (int i = 0; i < tournamentSize; i++) {
int randomId = (int) (Math.random() *
pop.getIndividuals().size());
tournament.getIndividuals().add(i, pop.getIndividual(randomId));
}
Individual fittest = tournament.getFittest();
return fittest;
}
protected static int getFitness(Individual individual) {
int fitness = 0;
for (int i = 0; i < individual.getDefaultGeneLength() && i <
solution.length; i++) {
if (individual.getSingleGene(i) == solution[i]) {
fitness++;
}
}
return fitness;
}
protected int getMaxFitness() {
int maxFitness = solution.length;
return maxFitness;
}
protected void setSolution(String newSolution) {
solution = new byte[newSolution.length()];
for (int i = 0; i < newSolution.length(); i++) {
String character = newSolution.substring(i, i + 1);
if (character.contains("0") || character.contains("1")) {
solution[i] = Byte.parseByte(character);
} else {
solution[i] = 0;
}
}
}
Agentes Inteligente
Requisitos para um agente inteligente
public class BookBuyerAgent extends Agent {
protected void setup() {
// Printout a welcome message
System.out.println(“Hello! Buyer-agent
“+getAID().getName()+” is ready.”);
}
}
SUGESTÃO
Próxima Aula Ant Colony

More Related Content

What's hot

C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscationguest9006ab
 
Simulator customizing & testing for Xcode 9
Simulator customizing & testing for Xcode 9Simulator customizing & testing for Xcode 9
Simulator customizing & testing for Xcode 9Bongwon Lee
 
Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記kentaro watanabe
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFabio Collini
 
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180Mahmoud Samir Fayed
 
Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Evgeny Borisov
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)MongoSF
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding HorrorsMark Baker
 
The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189Mahmoud Samir Fayed
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionIban Martinez
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212Mahmoud Samir Fayed
 
Scala meetup
Scala meetupScala meetup
Scala meetup扬 明
 
Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]
Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]
Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]MongoDB
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptLaurence Svekis ✔
 
Nullcon HackIM 2012 Solutions
Nullcon HackIM 2012 SolutionsNullcon HackIM 2012 Solutions
Nullcon HackIM 2012 SolutionsNilanjan De
 
Data analysis and visualization with mongo db [mongodb world 2016]
Data analysis and visualization with mongo db [mongodb world 2016]Data analysis and visualization with mongo db [mongodb world 2016]
Data analysis and visualization with mongo db [mongodb world 2016]Alexander Hendorf
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackVic Metcalfe
 

What's hot (20)

C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Simulator customizing & testing for Xcode 9
Simulator customizing & testing for Xcode 9Simulator customizing & testing for Xcode 9
Simulator customizing & testing for Xcode 9
 
Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記Beautiful Development ブレイクスルー体験記
Beautiful Development ブレイクスルー体験記
 
HCE tutorial
HCE tutorialHCE tutorial
HCE tutorial
 
Python speleology
Python speleologyPython speleology
Python speleology
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
 
The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180The Ring programming language version 1.5.1 book - Part 51 of 180
The Ring programming language version 1.5.1 book - Part 51 of 180
 
Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2Groovy puzzlers jug-moscow-part 2
Groovy puzzlers jug-moscow-part 2
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.6 book - Part 46 of 189
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introduction
 
The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212The Ring programming language version 1.10 book - Part 71 of 212
The Ring programming language version 1.10 book - Part 71 of 212
 
Scala meetup
Scala meetupScala meetup
Scala meetup
 
Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]
Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]
Aggregation Pipeline Power++: MongoDB 4.2 파이프 라인 쿼리, 업데이트 및 구체화된 뷰 소개 [MongoDB]
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScript
 
Nullcon HackIM 2012 Solutions
Nullcon HackIM 2012 SolutionsNullcon HackIM 2012 Solutions
Nullcon HackIM 2012 Solutions
 
Data analysis and visualization with mongo db [mongodb world 2016]
Data analysis and visualization with mongo db [mongodb world 2016]Data analysis and visualization with mongo db [mongodb world 2016]
Data analysis and visualization with mongo db [mongodb world 2016]
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
 
3 1-1
3 1-13 1-1
3 1-1
 

Viewers also liked

Sistemas infgerencial 1
Sistemas infgerencial 1Sistemas infgerencial 1
Sistemas infgerencial 1Nauber Gois
 
Seguranca informacao 1
Seguranca informacao 1Seguranca informacao 1
Seguranca informacao 1Nauber Gois
 
Sistemas operacionais 3
Sistemas operacionais 3Sistemas operacionais 3
Sistemas operacionais 3Nauber Gois
 
Sistemas operacionais 4
Sistemas operacionais 4Sistemas operacionais 4
Sistemas operacionais 4Nauber Gois
 
Sistemas infgerencial3
Sistemas infgerencial3Sistemas infgerencial3
Sistemas infgerencial3Nauber Gois
 
Sistema infgerenciais 2
Sistema infgerenciais 2Sistema infgerenciais 2
Sistema infgerenciais 2Nauber Gois
 
Sistemas operacionais 5
Sistemas operacionais 5Sistemas operacionais 5
Sistemas operacionais 5Nauber Gois
 
Sistema infgerencial5
Sistema infgerencial5Sistema infgerencial5
Sistema infgerencial5Nauber Gois
 
Inteligencia artificial5
Inteligencia artificial5Inteligencia artificial5
Inteligencia artificial5Nauber Gois
 
Sist infgerencial4
Sist infgerencial4Sist infgerencial4
Sist infgerencial4Nauber Gois
 
Inteligencia artificial 8
Inteligencia artificial 8Inteligencia artificial 8
Inteligencia artificial 8Nauber Gois
 
Inteligencia artificial 2
Inteligencia artificial 2Inteligencia artificial 2
Inteligencia artificial 2Nauber Gois
 
Sistemas operacionais1
Sistemas operacionais1Sistemas operacionais1
Sistemas operacionais1Nauber Gois
 
Invasaocom exploits
Invasaocom exploitsInvasaocom exploits
Invasaocom exploitsNauber Gois
 
Inteligencia artificial 1
Inteligencia artificial 1Inteligencia artificial 1
Inteligencia artificial 1Nauber Gois
 
Inteligencia artificial 3
Inteligencia artificial 3Inteligencia artificial 3
Inteligencia artificial 3Nauber Gois
 
Testes nao funcionais 1
Testes nao funcionais 1Testes nao funcionais 1
Testes nao funcionais 1Nauber Gois
 
Sistemas operacionais 2
Sistemas operacionais 2Sistemas operacionais 2
Sistemas operacionais 2Nauber Gois
 
Sist infgerenciais 8
Sist infgerenciais 8Sist infgerenciais 8
Sist infgerenciais 8Nauber Gois
 

Viewers also liked (20)

Sistemas infgerencial 1
Sistemas infgerencial 1Sistemas infgerencial 1
Sistemas infgerencial 1
 
Seguranca informacao 1
Seguranca informacao 1Seguranca informacao 1
Seguranca informacao 1
 
Sistemas operacionais 3
Sistemas operacionais 3Sistemas operacionais 3
Sistemas operacionais 3
 
Sistemas operacionais 4
Sistemas operacionais 4Sistemas operacionais 4
Sistemas operacionais 4
 
Sistemas infgerencial3
Sistemas infgerencial3Sistemas infgerencial3
Sistemas infgerencial3
 
Sistema infgerenciais 2
Sistema infgerenciais 2Sistema infgerenciais 2
Sistema infgerenciais 2
 
Sistemas operacionais 5
Sistemas operacionais 5Sistemas operacionais 5
Sistemas operacionais 5
 
Sistema infgerencial5
Sistema infgerencial5Sistema infgerencial5
Sistema infgerencial5
 
Inteligencia artificial5
Inteligencia artificial5Inteligencia artificial5
Inteligencia artificial5
 
Sist infgerencial4
Sist infgerencial4Sist infgerencial4
Sist infgerencial4
 
Inteligencia artificial 8
Inteligencia artificial 8Inteligencia artificial 8
Inteligencia artificial 8
 
Inteligencia artificial 2
Inteligencia artificial 2Inteligencia artificial 2
Inteligencia artificial 2
 
Sistemas operacionais1
Sistemas operacionais1Sistemas operacionais1
Sistemas operacionais1
 
Invasaocom exploits
Invasaocom exploitsInvasaocom exploits
Invasaocom exploits
 
Inteligencia artificial 1
Inteligencia artificial 1Inteligencia artificial 1
Inteligencia artificial 1
 
Inteligencia artificial 3
Inteligencia artificial 3Inteligencia artificial 3
Inteligencia artificial 3
 
Testes nao funcionais 1
Testes nao funcionais 1Testes nao funcionais 1
Testes nao funcionais 1
 
Sistemas operacionais 2
Sistemas operacionais 2Sistemas operacionais 2
Sistemas operacionais 2
 
Sist infgerenciais 8
Sist infgerenciais 8Sist infgerenciais 8
Sist infgerenciais 8
 
Data science
Data scienceData science
Data science
 

Similar to AI Simulated Annealing Genetic Algorithms Book Buyer Agent

かとうの Kotlin 講座 こってり版
かとうの Kotlin 講座 こってり版かとうの Kotlin 講座 こってり版
かとうの Kotlin 講座 こってり版Yutaka Kato
 
import java-util-Random- public class GA { int PopulationSize- int i.pdf
import java-util-Random-   public class GA { int PopulationSize- int i.pdfimport java-util-Random-   public class GA { int PopulationSize- int i.pdf
import java-util-Random- public class GA { int PopulationSize- int i.pdfasarudheen07
 
So Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfSo Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfarihantgiftgallery
 
Empathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible codeEmpathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible codeMario Gleichmann
 
Kotlin : Happy Development
Kotlin : Happy DevelopmentKotlin : Happy Development
Kotlin : Happy DevelopmentMd Sazzad Islam
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxwhitneyleman54422
 
About java
About javaAbout java
About javaJay Xu
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfakshpatil4
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFabio Collini
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdfaravlitraders2012
 
Kotlin killed Java stars
Kotlin killed Java starsKotlin killed Java stars
Kotlin killed Java starsMatteo Bonifazi
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfABHISHEKREADYMADESKO
 
import java.util.LinkedList;import java.util.Scanner;public cla.pdf
import java.util.LinkedList;import java.util.Scanner;public cla.pdfimport java.util.LinkedList;import java.util.Scanner;public cla.pdf
import java.util.LinkedList;import java.util.Scanner;public cla.pdffakhrifoam
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlinThijs Suijten
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodecamp Romania
 
Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012Anton Arhipov
 

Similar to AI Simulated Annealing Genetic Algorithms Book Buyer Agent (20)

かとうの Kotlin 講座 こってり版
かとうの Kotlin 講座 こってり版かとうの Kotlin 講座 こってり版
かとうの Kotlin 講座 こってり版
 
import java-util-Random- public class GA { int PopulationSize- int i.pdf
import java-util-Random-   public class GA { int PopulationSize- int i.pdfimport java-util-Random-   public class GA { int PopulationSize- int i.pdf
import java-util-Random- public class GA { int PopulationSize- int i.pdf
 
So Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdfSo Far I have these two classes but I need help with my persontest c.pdf
So Far I have these two classes but I need help with my persontest c.pdf
 
Empathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible codeEmpathic Programming - How to write comprehensible code
Empathic Programming - How to write comprehensible code
 
Kotlin : Happy Development
Kotlin : Happy DevelopmentKotlin : Happy Development
Kotlin : Happy Development
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
 
About java
About javaAbout java
About java
 
can do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdfcan do this in java please thanks in advance The code that y.pdf
can do this in java please thanks in advance The code that y.pdf
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+k
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 
Benefits of Kotlin
Benefits of KotlinBenefits of Kotlin
Benefits of Kotlin
 
Kotlin killed Java stars
Kotlin killed Java starsKotlin killed Java stars
Kotlin killed Java stars
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdf
 
import java.util.LinkedList;import java.util.Scanner;public cla.pdf
import java.util.LinkedList;import java.util.Scanner;public cla.pdfimport java.util.LinkedList;import java.util.Scanner;public cla.pdf
import java.util.LinkedList;import java.util.Scanner;public cla.pdf
 
Property Based Testing
Property Based TestingProperty Based Testing
Property Based Testing
 
Elegant objects
Elegant objectsElegant objects
Elegant objects
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
 
Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012Mastering Java Bytecode With ASM - 33rd degree, 2012
Mastering Java Bytecode With ASM - 33rd degree, 2012
 
662305 10
662305 10662305 10
662305 10
 

More from Nauber Gois

Inteligencia artificial 13
Inteligencia artificial 13Inteligencia artificial 13
Inteligencia artificial 13Nauber Gois
 
Sistemas operacionais 14
Sistemas operacionais 14Sistemas operacionais 14
Sistemas operacionais 14Nauber Gois
 
Sistemas operacionais 13
Sistemas operacionais 13Sistemas operacionais 13
Sistemas operacionais 13Nauber Gois
 
Inteligencia artificial 12
Inteligencia artificial 12Inteligencia artificial 12
Inteligencia artificial 12Nauber Gois
 
Sistemas operacionais 12
Sistemas operacionais 12Sistemas operacionais 12
Sistemas operacionais 12Nauber Gois
 
Sistemas operacionais 11
Sistemas operacionais 11Sistemas operacionais 11
Sistemas operacionais 11Nauber Gois
 
Sistemas operacionais 10
Sistemas operacionais 10Sistemas operacionais 10
Sistemas operacionais 10Nauber Gois
 
Inteligencia artificial 11
Inteligencia artificial 11Inteligencia artificial 11
Inteligencia artificial 11Nauber Gois
 
Sistemas operacional 9
Sistemas operacional 9Sistemas operacional 9
Sistemas operacional 9Nauber Gois
 
Inteligencia artificial 10
Inteligencia artificial 10Inteligencia artificial 10
Inteligencia artificial 10Nauber Gois
 
Sistemas operacionais 8
Sistemas operacionais 8Sistemas operacionais 8
Sistemas operacionais 8Nauber Gois
 
Inteligencia artificial 9
Inteligencia artificial 9Inteligencia artificial 9
Inteligencia artificial 9Nauber Gois
 
Sist operacionais 7
Sist operacionais 7Sist operacionais 7
Sist operacionais 7Nauber Gois
 
Inteligencia artifical 7
Inteligencia artifical 7Inteligencia artifical 7
Inteligencia artifical 7Nauber Gois
 
Ssit informacoesgerenciais 5
Ssit informacoesgerenciais 5Ssit informacoesgerenciais 5
Ssit informacoesgerenciais 5Nauber Gois
 
Sistemas operacionais 6
Sistemas operacionais 6Sistemas operacionais 6
Sistemas operacionais 6Nauber Gois
 
Inteligencia artifical 6
Inteligencia artifical 6Inteligencia artifical 6
Inteligencia artifical 6Nauber Gois
 

More from Nauber Gois (19)

Ai health
Ai health Ai health
Ai health
 
Inteligencia artificial 13
Inteligencia artificial 13Inteligencia artificial 13
Inteligencia artificial 13
 
Sistemas operacionais 14
Sistemas operacionais 14Sistemas operacionais 14
Sistemas operacionais 14
 
Sistemas operacionais 13
Sistemas operacionais 13Sistemas operacionais 13
Sistemas operacionais 13
 
Inteligencia artificial 12
Inteligencia artificial 12Inteligencia artificial 12
Inteligencia artificial 12
 
Sistemas operacionais 12
Sistemas operacionais 12Sistemas operacionais 12
Sistemas operacionais 12
 
Sistemas operacionais 11
Sistemas operacionais 11Sistemas operacionais 11
Sistemas operacionais 11
 
Sistemas operacionais 10
Sistemas operacionais 10Sistemas operacionais 10
Sistemas operacionais 10
 
Inteligencia artificial 11
Inteligencia artificial 11Inteligencia artificial 11
Inteligencia artificial 11
 
Sistemas operacional 9
Sistemas operacional 9Sistemas operacional 9
Sistemas operacional 9
 
Inteligencia artificial 10
Inteligencia artificial 10Inteligencia artificial 10
Inteligencia artificial 10
 
Sistemas operacionais 8
Sistemas operacionais 8Sistemas operacionais 8
Sistemas operacionais 8
 
Inteligencia artificial 9
Inteligencia artificial 9Inteligencia artificial 9
Inteligencia artificial 9
 
Sist operacionais 7
Sist operacionais 7Sist operacionais 7
Sist operacionais 7
 
Inteligencia artifical 7
Inteligencia artifical 7Inteligencia artifical 7
Inteligencia artifical 7
 
Beefataque
BeefataqueBeefataque
Beefataque
 
Ssit informacoesgerenciais 5
Ssit informacoesgerenciais 5Ssit informacoesgerenciais 5
Ssit informacoesgerenciais 5
 
Sistemas operacionais 6
Sistemas operacionais 6Sistemas operacionais 6
Sistemas operacionais 6
 
Inteligencia artifical 6
Inteligencia artifical 6Inteligencia artifical 6
Inteligencia artifical 6
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

AI Simulated Annealing Genetic Algorithms Book Buyer Agent

  • 2.
  • 6. public class SimulatedAnnealing { private static Travel travel = new Travel(10);
  • 7. public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) { System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " + numberOfIterations + " and colling rate: " + coolingRate); double t = startingTemperature; travel.generateInitialTravel(); double bestDistance = travel.getDistance(); System.out.println("Initial distance of travel: " + bestDistance); Travel bestSolution = travel; Travel currentSolution = bestSolution; for (int i = 0; i < numberOfIterations; i++) { if (t > 0.1) { currentSolution.swapCities(); double currentDistance = currentSolution.getDistance(); if (currentDistance < bestDistance) { bestDistance = currentDistance; } else if (Math.exp((bestDistance - currentDistance) / t) < Math.random()) { currentSolution.revertSwap(); } t *= coolingRate; } else { continue; } if (i % 100 == 0) { System.out.println("Iteration #" + i); } } return bestDistance; }
  • 8.
  • 9.
  • 12.
  • 13. @Data public class Individual { protected int defaultGeneLength = 64; private byte[] genes = new byte[defaultGeneLength]; private int fitness = 0;
  • 14. public Individual() { for (int i = 0; i < genes.length; i++) { byte gene = (byte) Math.round(Math.random()); genes[i] = gene; } }
  • 15. protected byte getSingleGene(int index) { return genes[index]; } protected void setSingleGene(int index, byte value) { genes[index] = value; fitness = 0; } public int getFitness() { if (fitness == 0) { fitness = SimpleGeneticAlgorithm.getFitness(this); } return fitness; }
  • 16. @Override public String toString() { String geneString = ""; for (int i = 0; i < genes.length; i++) { geneString += getSingleGene(i); } return geneString; }
  • 17. public class Population { private List<Individual> individuals; public Population(int size, boolean createNew) { individuals = new ArrayList<>(); if (createNew) { createNewPopulation(size); } } protected Individual getIndividual(int index) { return individuals.get(index); } protected Individual getFittest() { Individual fittest = individuals.get(0); for (int i = 0; i < individuals.size(); i++) { if (fittest.getFitness() <= getIndividual(i).getFitness()) { fittest = getIndividual(i); } } return fittest; }
  • 18. private void createNewPopulation(int size) { for (int i = 0; i < size; i++) { Individual newIndividual = new Individual(); individuals.add(i, newIndividual); } } }
  • 19. public class SimpleGeneticAlgorithm { private static final double uniformRate = 0.5; private static final double mutationRate = 0.025; private static final int tournamentSize = 5; private static final boolean elitism = true; private static byte[] solution = new byte[64];
  • 20. public boolean runAlgorithm(int populationSize, String solution) { if (solution.length() != SimpleGeneticAlgorithm.solution.length) { throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes"); } setSolution(solution); Population myPop = new Population(populationSize, true); int generationCount = 1; while (myPop.getFittest().getFitness() < getMaxFitness()) { System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness()); myPop = evolvePopulation(myPop); generationCount++; } System.out.println("Solution found!"); System.out.println("Generation: " + generationCount); System.out.println("Genes: "); System.out.println(myPop.getFittest()); return true; }
  • 21. public Population evolvePopulation(Population pop) { int elitismOffset; Population newPopulation = new Population(pop.getIndividuals().size(), false); if (elitism) { newPopulation.getIndividuals().add(0, pop.getFittest()); elitismOffset = 1; } else { elitismOffset = 0; } for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) { Individual indiv1 = tournamentSelection(pop); Individual indiv2 = tournamentSelection(pop); Individual newIndiv = crossover(indiv1, indiv2); newPopulation.getIndividuals().add(i, newIndiv); } for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) { mutate(newPopulation.getIndividual(i)); } return newPopulation; }
  • 22. private Individual crossover(Individual indiv1, Individual indiv2) { Individual newSol = new Individual(); for (int i = 0; i < newSol.getDefaultGeneLength(); i++) { if (Math.random() <= uniformRate) { newSol.setSingleGene(i, indiv1.getSingleGene(i)); } else { newSol.setSingleGene(i, indiv2.getSingleGene(i)); } } return newSol; } private void mutate(Individual indiv) { for (int i = 0; i < indiv.getDefaultGeneLength(); i++) { if (Math.random() <= mutationRate) { byte gene = (byte) Math.round(Math.random()); indiv.setSingleGene(i, gene); } } }
  • 23.
  • 24. private Individual tournamentSelection(Population pop) { Population tournament = new Population(tournamentSize, false); for (int i = 0; i < tournamentSize; i++) { int randomId = (int) (Math.random() * pop.getIndividuals().size()); tournament.getIndividuals().add(i, pop.getIndividual(randomId)); } Individual fittest = tournament.getFittest(); return fittest; } protected static int getFitness(Individual individual) { int fitness = 0; for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) { if (individual.getSingleGene(i) == solution[i]) { fitness++; } } return fitness; }
  • 25. protected int getMaxFitness() { int maxFitness = solution.length; return maxFitness; } protected void setSolution(String newSolution) { solution = new byte[newSolution.length()]; for (int i = 0; i < newSolution.length(); i++) { String character = newSolution.substring(i, i + 1); if (character.contains("0") || character.contains("1")) { solution[i] = Byte.parseByte(character); } else { solution[i] = 0; } } }
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 80. Requisitos para um agente inteligente
  • 81. public class BookBuyerAgent extends Agent { protected void setup() { // Printout a welcome message System.out.println(“Hello! Buyer-agent “+getAID().getName()+” is ready.”); } }