1

I explain, I am trying to develop a program to optimize a system based on the parameters it receives. My program will have to vary these parameters to try to find the best possible combination.

here is a code to simplify my problem:

parameters=[["toto1","toto2","toto3"],["tutu1","tutu2","tutu3"],["titi1","titi2","titi3"],["tata1","tata2","tata3"]]

def MySysteme(param1,param2,param3,param4):
    result=0

    for i in range(0,len(param1)):
        result+=ord(param1[i])

    for i in range(0,len(param2)):
        result+=ord(param1[i])

    for i in range(0,len(param3)):
        result+=ord(param1[i])

    for i in range(0,len(param4)):
        result+=ord(param1[i])

    return result

print(MySysteme(parameters[0][0],parameters[1][2],parameters[2][2],parameters[3][0]))
print(MySysteme(parameters[1][0],parameters[2][2],parameters[3][2],parameters[0][0]))
print(MySysteme(parameters[3][1],parameters[1][2],parameters[2][2],parameters[0][0]))


#how to find the highest value?

I try to (try) find the highest number, without testing all the parameters naively. hence the use of a genetic algorithm. 1 parameter is a list contained in the list parameters, the contents of the list is a varariante of my parameter

knowing that in my function / my system, one should not have 2 times the same parameter, for example this should not happen: print (MySystem (parameters [1] [0], parameters [1] [0])) or this print (MySystem (parameters [2] [1], parameters [2] [0]))

on the other hand the number of parameters is included in 1 and 4 (there can be 1,2,3 or 4 parameters)

To solve my problem here is the data that I consider: Individual: it is a variant of parameter which carries a name ("toto1", "tata3", "toto2 = 12" ... etc.) Population: set of the variants of the parameters fitness : it is the result of the function according to the parameters a circuit: a set of parameters

but unlike the commercial traveler, I have no starting data => that is to say that I do not have GPS coordinates. and it is at this level that I am stuck for the resolution of my problem.

can anyone help me?

edit:

I have been looking some examples of how I could find the points at which a function achieves its maxium using a genetic algorithm approach in Python. I looked at this tutorial https://lethain.com/genetic-algorithms-cool-name-damn-simple/

my objective is to found the smaller number to "mySysteme" function i set a new code : je re-explique mon probleme plus simplement. J’ai mets un code plus complet, plus clair avec un algo génétique.

from random import randint, random
from operator import add
from functools import reduce

parameters=[["toto123","toto27","toto3000"],["tu","tut","tutu378694245"],["t","choicezaert","titi3=78965"],["blabla","2","conjoncture_is_enable"]]

def individual(length, min, max):
    return [ randint(min,max) for x in range(length) ]

def population(count, length, min, max):
    return [ individual(length, min, max) for x in range(count) ]

def fitness(individual, target):
    sum = reduce(add, individual, 0)
    return abs(target-sum)

def grade(pop, target):
    individu_number_parameters=randint(1, len(parameters)-1)
    for j in range(0,individu_number_parameters):
        position=randint(1, len(parameters)-1)
        parameter=parameters[position]
        if isinstance(parameter, list):
            parameter=parameters[position][randint(1, len(parameters[position])-1)]
    result=0

    for i in range(0,len(parameter)):
        result+=ord(parameter[i])
    return result

def evolve(pop, target, retain=0.2, random_select=0.05, mutate=0.01):
    graded = [ (fitness(x, target), x) for x in pop]
    graded = [ x[1] for x in sorted(graded)]
    retain_length = int(len(graded)*retain)
    parents = graded[:retain_length]
    for individual in graded[retain_length:]:
        if random_select > random():
            parents.append(individual)
    for individual in parents:
        if mutate > random():
            pos_to_mutate = randint(0, len(individual)-1)
            individual[pos_to_mutate] = randint(
                min(individual), max(individual))
    parents_length = len(parents)
    desired_length = len(pop) - parents_length
    children = []
    while len(children) < desired_length:
        male = randint(0, parents_length-1)
        female = randint(0, parents_length-1)
        if male != female:
            male = parents[male]
            female = parents[female]
            half = int(len(male) / 2)
            child = male[:half] + female[half:]
            children.append(child)
    parents.extend(children)
    return parents


target = 0
p_count = 100
i_length = 6
i_min = 0
i_max = 100
p = population(p_count, i_length, i_min, i_max)
fitness_history = [grade(p, target),]

for i in range(1000):
    p = evolve(p, target)
    fitness_history.append(grade(p, target))

for datum in fitness_history:
   print(datum)
print(len(fitness_history))

I updated with new code. My ask : i want that my program found smaller number

user7454761
  • 53
  • 1
  • 7

0 Answers0