Friday, March 24, 2023
No Result
View All Result
Get the latest A.I News on A.I. Pulses
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing
No Result
View All Result
Get the latest A.I News on A.I. Pulses
No Result
View All Result

Genetic Programming in Python: The Knapsack Drawback

January 25, 2023
140 10
Home Data science
Share on FacebookShare on Twitter


Picture created with DALL•E
 

On this article, we are going to have a look at the knapsack drawback, a basic in laptop science. We are going to clarify why it’s tough to resolve utilizing conventional computational strategies, and the way genetic programming will help discover a “adequate” answer. Afterwards, we are going to have a look at a Python implementation of simply such an answer to check out for ourselves.

 

 The knapsack drawback can be utilized for instance the problem of fixing advanced computational issues. In its easiest type, one is given a knapsack of a sure capability, a set of things with their sizes and values, and requested to maximise the worth of the objects positioned within the knapsack with out exceeding the capability. The knapsack drawback may be formulated in some ways, however it’s usually thought of to be a tough drawback to resolve when using conventional algorithms.

The issue of the knapsack drawback lies in the truth that it’s an NP-complete drawback, which implies that there isn’t any identified answer that may assure a globally optimum answer. Subsequently, the issue is intractable and can’t be shortly solved utilizing conventional strategies. The perfect identified algorithms for fixing the knapsack drawback contain utilizing brute drive search or heuristics, which might have prolonged run occasions, and which can not assure an optimum answer.

 

 Genetic programming, nevertheless, can present an alternate technique for locating an answer to the knapsack drawback. Genetic programming is a way that makes use of evolutionary algorithms to seek for options to advanced issues. By utilizing genetic programming, it’s potential to shortly discover a answer that’s “adequate” for the given drawback. It will also be used to optimize and enhance upon options.

In genetic programming, a set of potential options (or preliminary technology) are randomly generated, after which evaluated primarily based on a set of standards. These options that greatest match the factors are then chosen, and genetic mutations are utilized to create new answer variants (or subsequent generations). This new technology of variants is then evaluated and the method is repeated till a passable answer is discovered. The method is repeated till an optimum, or greatest “adequate”, answer is discovered.

The benefit of utilizing genetic programming to resolve the knapsack drawback is {that a} adequate answer may be discovered shortly with out having to exhaustively search by way of all potential options. This makes it a way more environment friendly method than conventional algorithms, and permits for a a lot quicker answer to be discovered.

 

 Now that we’ve got an concept of what the knapsack drawback is, what genetic programming is, and why we might use the latter to try to clear up the previous, let’s take a look at a Python implementation of what we’ve got described above.

We are going to undergo the essential capabilities one after the other, after which have a look at the holistic program afterwards.

This system is applied in Python, utilizing no third get together libraries.

 

Generate random inhabitants

 This perform generates a random inhabitants of a given measurement. It makes use of a for loop to iterate by way of the given measurement, and for every iteration it creates a chromosome. This chromosome is an inventory of 0s and 1s, which is generated utilizing the random.alternative() perform. The chromosome is then appended to the inhabitants record. Lastly, the perform prints out a message and returns the inhabitants record. This perform is beneficial for making a inhabitants of people for genetic algorithms.

def generate_population(measurement):
inhabitants = []
for _ in vary(measurement):
genes = [0, 1]
chromosome = []
for _ in vary(len(objects)):
chromosome.append(random.alternative(genes))
inhabitants.append(chromosome)
print(“Generated a random inhabitants of measurement”, measurement)
return inhabitants

 

Calculate chromosome health

 This perform is used to calculate the health of a chromosome. It takes a chromosome as an argument and iterates by way of it. If the worth of the chromosome at a given index is 1, it provides the corresponding merchandise’s weight and worth to the entire weight and complete worth respectively. If the entire weight exceeds the utmost weight, the health is ready to 0. In any other case, the health is ready to the entire worth. This perform is utilized in genetic algorithms to find out the health of a given chromosome.

def calculate_fitness(chromosome):
total_weight = 0
total_value = 0
for i in vary(len(chromosome)):
if chromosome[i] == 1:
total_weight += objects[i][0]
total_value += objects[i][1]
if total_weight > max_weight:
return 0
else:
return total_value

 

Choose chromosomes

 This perform is used to pick two chromosomes from a inhabitants for crossover. It first calculates the health values of every chromosome within the inhabitants utilizing the calculate_fitness() perform. It then normalizes the health values by dividing every worth by the sum of all health values. Lastly, it makes use of the random.selections() perform to randomly choose two chromosomes from the inhabitants primarily based on the normalized health values. The 2 chosen chromosomes are then returned because the mum or dad chromosomes for crossover.

def select_chromosomes(inhabitants):
fitness_values = []
for chromosome in inhabitants:
fitness_values.append(calculate_fitness(chromosome))

fitness_values = [float(i)/sum(fitness_values) for i in fitness_values]

parent1 = random.selections(inhabitants, weights=fitness_values, ok=1)[0]
parent2 = random.selections(inhabitants, weights=fitness_values, ok=1)[0]

print(“Chosen two chromosomes for crossover”)
return parent1, parent2

 

Carry out crossover

 This perform performs crossover between two chromosomes. It takes two mum or dad chromosomes as enter and randomly selects a crossover level. It then creates two little one chromosomes by combining the 2 mum or dad chromosomes on the crossover level. The primary little one chromosome is created by taking the primary a part of the primary mum or dad chromosome and the second a part of the second mum or dad chromosome. The second little one chromosome is created by taking the primary a part of the second mum or dad chromosome and the second a part of the primary mum or dad chromosome. Lastly, the 2 little one chromosomes are returned because the output.

def crossover(parent1, parent2):
crossover_point = random.randint(0, len(objects)-1)
child1 = parent1[0:crossover_point] + parent2[crossover_point:]
child2 = parent2[0:crossover_point] + parent1[crossover_point:]

print(“Carried out crossover between two chromosomes”)
return child1, child2

 

Carry out mutation

 This perform performs a mutation on a chromosome. It takes in a chromosome as an argument and makes use of the random module to generate a random quantity between 0 and the size of the chromosome. If the worth on the mutation level is 0, it’s modified to 1, and whether it is 1, it’s modified to 0. The perform then prints a message and returns the mutated chromosome. This perform can be utilized to simulate genetic mutations in a inhabitants of organisms.

def mutate(chromosome):
mutation_point = random.randint(0, len(objects)-1)
if chromosome[mutation_point] == 0:
chromosome[mutation_point] = 1
else:
chromosome[mutation_point] = 0
print(“Carried out mutation on a chromosome”)
return chromosome

 

Get greatest chromosome

 This perform takes in a inhabitants of chromosomes and returns the most effective chromosome from the inhabitants. It does this by first creating an inventory of health values for every chromosome within the inhabitants. It then finds the utmost health worth and its corresponding index within the record. Lastly, it returns the chromosome on the index of the utmost health worth. This perform is beneficial for locating the most effective chromosome from a inhabitants of chromosomes with a purpose to use it for additional operations.

def get_best(inhabitants):
fitness_values = []
for chromosome in inhabitants:
fitness_values.append(calculate_fitness(chromosome))

max_value = max(fitness_values)
max_index = fitness_values.index(max_value)
return inhabitants[max_index]

 

The management loop

 This code is performing an evolutionary algorithm to evolve a inhabitants of chromosomes. It begins by looping by way of the required variety of generations. For every technology, two chromosomes are chosen from the inhabitants, after which crossover is carried out on them to generate two new chromosomes. Then, the 2 new chromosomes are subjected to mutation with a given likelihood. The brand new chromosomes are individually topic to a random genetic mutation if the randomly generated likelihood is above the predetermined threshold. Lastly, the previous inhabitants is changed with the brand new inhabitants, which consists of the 2 new chromosomes and the remaining chromosomes from the previous inhabitants.

for _ in vary(generations):
# choose two chromosomes for crossover
parent1, parent2 = select_chromosomes(inhabitants)

# carry out crossover to generate two new chromosomes
child1, child2 = crossover(parent1, parent2)

# carry out mutation on the 2 new chromosomes
if random.uniform(0, 1) < mutation_probability:
child1 = mutate(child1)
if random.uniform(0, 1) < mutation_probability:
child2 = mutate(child2)

# change the previous inhabitants with the brand new inhabitants
inhabitants = [child1, child2] + inhabitants[2:]

 

 If we take the above capabilities and management loop, add an inventory of merchandise together with a number of parameters and a few output to the console, we get the next full Python implementation.

Word that all the parameters are hardcoded for simplicity; nevertheless, with little hassle one may as a substitute settle for command line arguments or solicit person enter for any of them, together with the quantity, worth, and weight of obtainable objects.

import random

# perform to generate a random inhabitants
def generate_population(measurement):
inhabitants = []
for _ in vary(measurement):
genes = [0, 1]
chromosome = []
for _ in vary(len(objects)):
chromosome.append(random.alternative(genes))
inhabitants.append(chromosome)
print(“Generated a random inhabitants of measurement”, measurement)
return inhabitants

# perform to calculate the health of a chromosome
def calculate_fitness(chromosome):
total_weight = 0
total_value = 0
for i in vary(len(chromosome)):
if chromosome[i] == 1:
total_weight += objects[i][0]
total_value += objects[i][1]
if total_weight > max_weight:
return 0
else:
return total_value

# perform to pick two chromosomes for crossover
def select_chromosomes(inhabitants):
fitness_values = []
for chromosome in inhabitants:
fitness_values.append(calculate_fitness(chromosome))

fitness_values = [float(i)/sum(fitness_values) for i in fitness_values]

parent1 = random.selections(inhabitants, weights=fitness_values, ok=1)[0]
parent2 = random.selections(inhabitants, weights=fitness_values, ok=1)[0]

print(“Chosen two chromosomes for crossover”)
return parent1, parent2

# perform to carry out crossover between two chromosomes
def crossover(parent1, parent2):
crossover_point = random.randint(0, len(objects)-1)
child1 = parent1[0:crossover_point] + parent2[crossover_point:]
child2 = parent2[0:crossover_point] + parent1[crossover_point:]

print(“Carried out crossover between two chromosomes”)
return child1, child2

# perform to carry out mutation on a chromosome
def mutate(chromosome):
mutation_point = random.randint(0, len(objects)-1)
if chromosome[mutation_point] == 0:
chromosome[mutation_point] = 1
else:
chromosome[mutation_point] = 0
print(“Carried out mutation on a chromosome”)
return chromosome

# perform to get the most effective chromosome from the inhabitants
def get_best(inhabitants):
fitness_values = []
for chromosome in inhabitants:
fitness_values.append(calculate_fitness(chromosome))

max_value = max(fitness_values)
max_index = fitness_values.index(max_value)
return inhabitants[max_index]


# objects that may be put within the knapsack
objects = [
[1, 2],
[2, 4],
[3, 4],
[4, 5],
[5, 7],
[6, 9]
]

# print out there objects
print(“Accessible objects:n”, objects)

# parameters for genetic algorithm
max_weight = 10
population_size = 10
mutation_probability = 0.2
generations = 10

print(“nGenetic algorithm parameters:”)
print(“Max weight:”, max_weight)
print(“Inhabitants:”, population_size)
print(“Mutation likelihood:”, mutation_probability)
print(“Generations:”, generations, “n”)
print(“Performing genetic evolution:”)

# generate a random inhabitants
inhabitants = generate_population(population_size)

# evolve the inhabitants for specified variety of generations
for _ in vary(generations):
# choose two chromosomes for crossover
parent1, parent2 = select_chromosomes(inhabitants)

# carry out crossover to generate two new chromosomes
child1, child2 = crossover(parent1, parent2)

# carry out mutation on the 2 new chromosomes
if random.uniform(0, 1) < mutation_probability:
child1 = mutate(child1)
if random.uniform(0, 1) < mutation_probability:
child2 = mutate(child2)

# change the previous inhabitants with the brand new inhabitants
inhabitants = [child1, child2] + inhabitants[2:]

# get the most effective chromosome from the inhabitants
greatest = get_best(inhabitants)

# get the burden and worth of the most effective answer
total_weight = 0
total_value = 0
for i in vary(len(greatest)):
if greatest[i] == 1:
total_weight += objects[i][0]
total_value += objects[i][1]

# print the most effective answer
print(“nThe greatest answer:”)
print(“Weight:”, total_weight)
print(“Worth:”, total_value)

 

Save the above code to the file knapsack_ga.py, after which run it by typing python knapsack_ga.py.

A pattern output is as follows:

Accessible objects:
[[1, 2], [2, 4], [3, 4], [4, 5], [5, 7], [6, 9]]

Genetic algorithm parameters:
Max weight: 10
Inhabitants: 10
Mutation likelihood: 0.2
Generations: 10

Performing genetic evolution:
Generated a random inhabitants of measurement 10
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Carried out mutation on a chromosome
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Carried out mutation on a chromosome
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Carried out mutation on a chromosome
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Chosen two chromosomes for crossover
Carried out crossover between two chromosomes
Carried out mutation on a chromosome

The perfect answer:
Weight: 10
Worth: 14

 

And there you go. You now know tips on how to use genetic programming to resolve the knapsack drawback. With just a little ingenuity, the above script could possibly be modified to resolve all types of computationally advanced issues for his or her greatest “adequate” options.

Thanks for studying!

 Parts of this text have been plotted and/or written with the help of GPT-3.

  Matthew Mayo (@mattmayo13) is a Information Scientist and the Editor-in-Chief of KDnuggets, the seminal on-line Information Science and Machine Studying useful resource. His pursuits lie in pure language processing, algorithm design and optimization, unsupervised studying, neural networks, and automatic approaches to machine studying. Matthew holds a Grasp’s diploma in laptop science and a graduate diploma in knowledge mining. He may be reached at editor1 at kdnuggets[dot]com. 



Source link

Tags: GeneticKnapsackProblemProgrammingPython
Next Post

4 methods you won't notice superior analytics is altering the world

5 Methods to Take care of the Lack of Knowledge in Machine Studying

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent News

Optimize Knowledge Warehouse Storage with Views and Tables | by Madison Schott | Mar, 2023

March 24, 2023

Bard Makes use of Gmail Information | Is AI Coaching With Private Information Moral?

March 24, 2023

Key Methods to Develop AI Software program Value-Successfully

March 24, 2023

Visible language maps for robotic navigation – Google AI Weblog

March 24, 2023

Unlock Your Potential with This FREE DevOps Crash Course

March 24, 2023

High 15 YouTube Channels to Degree Up Your Machine Studying Expertise

March 23, 2023

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
A.I. Pulses

Get The Latest A.I. News on A.I.Pulses.com.
Machine learning, Computer Vision, A.I. Startups, Robotics News and more.

Categories

  • A.I News
  • A.I. Startups
  • Computer Vision
  • Data science
  • Machine learning
  • Natural Language Processing
  • Robotics
No Result
View All Result

Recent News

  • Optimize Knowledge Warehouse Storage with Views and Tables | by Madison Schott | Mar, 2023
  • Bard Makes use of Gmail Information | Is AI Coaching With Private Information Moral?
  • Key Methods to Develop AI Software program Value-Successfully
  • Home
  • DMCA
  • Disclaimer
  • Cookie Privacy Policy
  • Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

No Result
View All Result
  • Home
  • A.I News
  • Computer Vision
  • Machine learning
  • A.I. Startups
  • Robotics
  • Data science
  • Natural Language Processing

Copyright © 2022 A.I. Pulses.
A.I. Pulses is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In