mainExample.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # main imports
  2. import logging
  3. # module imports
  4. # Note: you need to import from folder dependency name
  5. # examples: `from optimization.solutions.BinarySolution import BinarySolution`
  6. from optimization.algorithms.IteratedLocalSearch import IteratedLocalSearch as ILS
  7. from optimization.solutions.BinarySolution import BinarySolution
  8. from optimization.evaluators.EvaluatorExample import evaluatorExample
  9. from optimization.operators.mutators.SimpleMutation import SimpleMutation
  10. from optimization.operators.mutators.SimpleBinaryMutation import SimpleBinaryMutation
  11. from optimization.operators.crossovers.SimpleCrossover import SimpleCrossover
  12. from optimization.operators.policies.RandomPolicy import RandomPolicy
  13. # logging configuration
  14. logging.basicConfig(format='%(asctime)s %(message)s', filename='example.log', level=logging.DEBUG)
  15. # default validator
  16. def validator(solution):
  17. return True
  18. # define init random solution
  19. def init():
  20. return BinarySolution([], 30).random(validator)
  21. def main():
  22. operators = [SimpleBinaryMutation(), SimpleMutation(), SimpleCrossover()]
  23. policy = RandomPolicy(operators)
  24. algo = ILS(init, evaluatorExample, operators, policy, validator, True)
  25. bestSol = algo.run(1000)
  26. print("Found ", bestSol)
  27. if __name__ == "__main__":
  28. main()