Output

Code in python
import numpy as np
class Genetic(object):
def __init__(self, f, pop_size = 100, n_variables = 2):
self.f = f
self.minim = 0
self.maxim = 31
self.pop_size = pop_size
self.n_variables = n_variables
self.population = self.initializePopulation()
self.evaluatePopulation()
def initializePopulation(self):
return [np.random.randint(self.minim, self.maxim,
size=(self.n_variables))
for i in range(self.pop_size)]
def evaluatePopulation(self):
return [self.f(i[0], i[1]) for i in self.population]
#return [(i[0]-4)**2 + i[1]**2 for i in self.population]
def nextGen(self):
results = self.evaluatePopulation()
children = [self.population[np.argmin(results)]]
while len(children) < self.pop_size:
# Tournament selection
randA, randB = np.random.randint(0, self.pop_size), \
np.random.randint(0, self.pop_size)
if results[randA] < results[randB]: p1 =
self.population[randA]
else: p1 = self.population[randB]
randA, randB = np.random.randint(0, self.pop_size), \
np.random.randint(0, self.pop_size)
if results[randA] < results[randB]: p2 =
self.population[randA]
else: p2 = self.population[randB]
signs = []
for i in zip(p1, p2):
if i[0] < 0 and i[1] < 0: signs.append(-1)
elif i[0] >= 0 and i[1] >= 0: signs.append(1)
else: signs.append(np.random.choice([-1,1]))
# Convert values to binary
p1 = [format(abs(i), '010b') for i in p1]
p2 = [format(abs(i), '010b') for i in p2]
# Recombination
child = []
for i, j in zip(p1, p2):
for k, l in zip(i, j):
if k == l: child.append(k)
else: child.append(str(np.random.randint(min(k, l),
max(k,l))))
child = ''.join(child)
g1 = child[0:len(child)//2]
g2 = child[len(child)//2:len(child)]
children.append(np.asarray([signs[0]*int(g1, 2),
signs[1]*int(g2, 2)]))
self.population = children
def run(self):
ix = 0
while ix < 1000:
ix += 1
self.nextGen()
return self.population[0]
f = lambda x, y: (x-10) + y**2
gen = Genetic(f)
minim = gen.run()
print('Minimum found X =', minim[0], ', Y =', minim[1])
Program a Genetic Algorithm to find an integer number between 0 and 31 that minimizes the...
How to design an algorithm using recursion to find number of subarray contains all 0 in an array which only contains 0 or 1? For example, we have array 00, so we return 1, if we have an array 0100, we return 2. Assume the size of array >=1. How to find the recurrence relation of the algorithm? And how to express the recurrence relation as a function of n.
School Method for Integer Addition and Karatsuba Algorithm for Integer Multiplication Integer Division Your program takes one line as input. The input line contains three integers separated by spaces. Let the three integers be I1, I2, and B. I1 and I2 are both nonnegative integers up to 100 digits long (there are no leading 0s, except when the value itself is 0). B is I1 and I2's base (B is from 2 to 10).1 Your program should output the sum...
1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...
Description For this program, you are going to convert decimal (integer) numbers into their octal number (integer) equivalents. Make sure that you create a new Project and Java class file for this assignment. Your Repl.It file should be named “Main.java”. You can read about octal-to-decimal number conversions from wikepedia or another website Instructions The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its...
Write a program to reverse an integer number by using a function
called reverse. The program reads in an integer number and prints
its reverse order. Note that the function receives the number via a
pointer, and uses the pointer to write the reverse number on the
main function.
The function MUST be used AS
IS:
void reverse(int *n)
Language in C
Bonus Problem: Number Reverser reverse c Write a program to reverse an integer number by using a function...
A random number generator produces a random integer between 0 and 9 inclusive. Part 1 of 3 Find the probability that the integer is a number less than or equal to 7. Then find the probability that the integer is less than 7. Explain the difference or similarity in your answers.
Write a program that (in the main program) prompts the user for the (integer) model number of their car. The main function should then call the function check defective which should • take in the model number of the car • check if the model number is 119, 179, 221, 780, or anything between 189 and 195, inclusive, which indicates the car is defective • print a message indicating whether or not the car is defective Note: this is should...
C program--write a program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or * to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or * to quit : 371 3**3 + 7**3 + 1**3 is = 371...
Design a Java program that asks the user to enter an integer number n and then generates an array of that many random points (x, y) with x- and y-coordinates in the range between 0 and 100. After this the program must ask the user to enter two diagonal points (x_1, y_1) and (x_2, y_2) of a rectangle with sides parallel to the coordinate axis (see the image below, where possible pairs of diagonal points are shown in red color)....
C program--write a program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or * to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or * to quit : 371 3**3 + 7**3 + 1**3 is = 371...