samedi 7 mars 2020

Python random task generator for Genetic Algorithm : Is there a way to randomly generate values for multiple variables with some criteria/constraints?

The task generator should be implemented as a function: generate(n, w, s, output file)

The generator for a given number of items is to randomly generate weights and sizes of items. The weight wi, size si and price ci of the i-th item must meet the following Criteria: 1 < wi < 10*w/n ; 1 < si < 10*s/n ; 1 < ci < n

This is what i have done for the first part of the problem:

n = random.randint(10,20)
w = random.randint(100,200)
s = random.randint(100,200)

def generator(n, w, s):
        yield n
        yield w
        yield s

a = list(generator(n, w, s))

listOne = [a]
with open('output_file.csv', 'w') as f:
    rows = [['n','w','s']] + listOne
    writer = csv.writer(f)
    writer.writerows(rows)

Since i am a very bad programmer but an average coder : how (do i/would you) (approach/figure out) the second part of my problem which is to generate wi si and ci with the generator, what (steps do you take/procedure you would follow) to structure the code step by step (also any tips for approaching these kind of problems in general are welcome).Additionally please let me know if i can improve my approach to first part in a more concise way.




Aucun commentaire:

Enregistrer un commentaire