buenas tardes, me gustaría ver si alguien me puede ayudar a pasar este código de python a java, mi profesor de la uni nos lo pidio de tarea pero no conocemos nada de java, si es neceserio puedo pagar por paypal
from random import randint
import numpy
import random
import itertools
import time
import sys
import tkinter
from tkinter.ttk import *
start_time = time.clock()
winningsMatrix = numpy.matrix([
[0.00,0.00,0.00,0.00],
[0.28,0.25,0.15,0.20],
[0.45,0.41,0.25,0.33],
[0.65,0.55,0.40,0.42],
[0.78,0.65,0.50,0.48],
[0.90,0.75,0.62,0.53],
[1.02,0.80,0.73,0.56],
[1.13,0.85,0.82,0.58],
[1.23,0.88,0.90,0.60],
[1.32,0.90,0.96,0.60],
[1.38,0.90,1.00,0.60]])
def create_initial_population(numberofIndividuals):
InitialPopulation = list()
i = 0
while(i < numberofIndividuals):
individual = [randint(0,10) for r in range(0,4)]
summing = individual[0] + individual[1] + individual[2] + individual[3]
if summing == 10 :
InitialPopulation.append(individual)
i = i + 1
return InitialPopulation
def compute_aptitude( individual ) :
sumInversion = individual[0] + individual[1] + individual[2] + individual[3]
#winnings = (((individual[0] * winningsMatrix[individual[0] - 1 ,0]) / 100) + individual[0]) + (((individual[1] * winningsMatrix[individual[1] - 1,1]) / 100) + individual[1]) + (((individual[2] * winningsMatrix[individual[2] - 1,2]) / 100) + individual[2]) + (((individual[3] * winningsMatrix[individual[3] - 1,3]) / 100) + individual[3])
winnings = (1000000 * winningsMatrix[individual[0] ,0]) + (1000000 * winningsMatrix[individual[1] ,1]) + (1000000 * winningsMatrix[individual[2] ,2]) + (1000000 * winningsMatrix[individual[3] ,3])
aptitude = winnings / ((500 * abs(sumInversion - 10)) + 1)
return aptitude
def save_aptitude(population):
aptitude = list()
for ind in population:
aptitude.append(compute_aptitude(ind))
return aptitude
def make_selection(population):
selection = list()
for i in range(0, len(population)) :
firstElement= random.choice(population)
secondElement = random.choice(population)
if compute_aptitude(firstElement) > compute_aptitude(secondElement):
selection.append(firstElement)
else :
selection.append(secondElement)
return selection
def one_point_cross(parent1, parent2, crossPoint) :
lefParent1 = parent1[0:crossPoint]
rigthParent1 = parent1[crossPoint: len(parent1)]
lefParent2 = parent2[0: crossPoint]
rigthParent2 = parent2[crossPoint: len(parent2)]
children1 = lefParent1 + rigthParent2
children2 = lefParent2 + rigthParent1
return children1, children2
def cross_parents(population, crossProbability) :
newPopulation = list()
crossPoint = randint(0,3)
for i in range(0, len(population)) :
parent1 = random.choice(population)
parent2 = random.choice(population)
probability = random.uniform(0.0,1.0)
if (probability >= crossProbability) :
children = one_point_cross(parent1,parent2, crossPoint)
newPopulation.append(children[0])
newPopulation.append(children[1])
else :
newPopulation.append(parent1)
newPopulation.append(parent2)
return newPopulation
def mutation(individual, mutationPoint1, mutationPoint2):
m1 = individual[mutationPoint1]
m2 = individual[mutationPoint2]
individual[mutationPoint1] = m2
individual[mutationPoint2] = m1
return individual
def make_mutation(population, mutationProbability) :
newPopulation = list()
mut1 = randint(0,3)
mut2 = randint(0,3)
for ind in population:
probability = random.uniform(0.0, 1.0)
if mutationProbability >= probability :
newPopulation.append(mutation(ind, mut1, mut2))
else :
newPopulation.append(ind)
return newPopulation
def getBest(population) :
aptitude = save_aptitude(population)
bestInd = max(aptitude)
bestIndividual = None
for i in range(0,len(aptitude)):
if aptitude == bestInd:
bestIndividual = population
return bestIndividual
def main(generationsNumber, crossProbability, mutationProbability, numberofIndividuals):
bestIndividuals = list()
population = create_initial_population(numberofIndividuals)
for i in range(0, generationsNumber) :
selection = make_selection(population)
cross = cross_parents(selection, crossProbability)
mutate = make_mutation(cross, mutationProbability)
bestIndividuals.append(getBest(mutate))
for i in range(0, len(bestIndividuals)):
print("Best Individual of Generation {}: {}".format( i + 1, bestIndividuals[i] ))
best = getBest(bestIndividuals)
print("DISTRIBUCIÓN DE LA INVERSIÓN CON MAYORES GANANCIAS:")
print("CIUDAD 1: {} MILLONES, GANANCIA: ${}".format(best[0], ((winningsMatrix[best[0],0])*1000000)))
print("CIUDAD 2: {} MILLONES, GANANCIA: ${}".format(best[1], ((winningsMatrix[best[1],1])*1000000)))
print("CIUDAD 3: {} MILLONES, GANANCIA: ${}".format(best[2], ((winningsMatrix[best[2],2])*1000000)))
print("CIUDAD 4: {} MILLONES, GANANCIA: ${}".format(best[3], ((winningsMatrix[best[3],3])*1000000)))
print("GANANCIA TOTAL: ${}00".format(compute_aptitude(best)))
print ("Time to reach the final state: {} seconds".format( time.clock() - start_time))
main(20,0.8,0.01,50)
#root = tkinter.Tk() #Create main window
#root.title("ALGORITMO GENÉTICO")
#root.config (bg = "white") #Set background color
#root.geometry ("400x400") #Set size of window
#individuos = Label(text='INDIVIDUOS DE LA POBLACIÓN').pack(padx=10,pady=10)
#x = Entry(root, width=10).pack(padx=10,pady=10)
#generaciones = Label(text='NÚMERO DE GENERACIONES').pack(padx=10,pady=10)
#y = Entry(root, width=10).pack(padx=10,pady=10)
#probCruza = Label(text='PROBABILIDAD DE CRUZA').pack(padx=10,pady=10)
#z = Entry(root, width=10).pack(padx=10,pady=10)
#probMuta = Label(text='PROBABILIDAD DE MUTACIÓN').pack(padx=10,pady=10)
#a = Entry(root, width=10).pack(padx=10,pady=10)
#buttonCapture = tkinter.ttk.Button(root, text = 'EJECUTAR', command = "") #Create button and assign it a function to execute
#buttonCapture.pack(padx=10,pady=10) #Execute function when click in the button
#root.mainloop()