knapsackMultiExample.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # main imports
  2. import logging
  3. import os
  4. import random
  5. # module imports
  6. from macop.solutions.BinarySolution import BinarySolution
  7. from macop.evaluators.EvaluatorExample import evaluatorExample
  8. from macop.operators.mutators.SimpleMutation import SimpleMutation
  9. from macop.operators.mutators.SimpleBinaryMutation import SimpleBinaryMutation
  10. from macop.operators.crossovers.SimpleCrossover import SimpleCrossover
  11. from macop.operators.crossovers.RandomSplitCrossover import RandomSplitCrossover
  12. from macop.operators.policies.RandomPolicy import RandomPolicy
  13. from macop.operators.policies.UCBPolicy import UCBPolicy
  14. from macop.algorithms.multi.MOEAD import MOEAD
  15. from macop.callbacks.MultiCheckpoint import MultiCheckpoint
  16. from macop.callbacks.ParetoCheckpoint import ParetoCheckpoint
  17. if not os.path.exists('data'):
  18. os.makedirs('data')
  19. # logging configuration
  20. logging.basicConfig(format='%(asctime)s %(message)s', filename='data/exampleMOEAD.log', level=logging.DEBUG)
  21. random.seed(42)
  22. elements_score1 = [ random.randint(1, 100) for _ in range(500) ]
  23. elements_score2 = [ random.randint(1, 200) for _ in range(500) ]
  24. elements_weight = [ random.randint(90, 100) for _ in range(500) ]
  25. def knapsackWeight(_solution):
  26. weight_sum = 0
  27. for index, elem in enumerate(_solution.data):
  28. weight_sum += elements_weight[index] * elem
  29. return weight_sum
  30. # default validator
  31. def validator(_solution):
  32. if knapsackWeight(_solution) <= 15000:
  33. return True
  34. else:
  35. False
  36. # define init random solution
  37. def init():
  38. return BinarySolution([], 200).random(validator)
  39. def evaluator1(_solution):
  40. fitness = 0
  41. for index, elem in enumerate(_solution.data):
  42. fitness += (elements_score1[index] * elem)
  43. return fitness
  44. def evaluator2(_solution):
  45. fitness = 0
  46. for index, elem in enumerate(_solution.data):
  47. fitness += (elements_score2[index] * elem)
  48. return fitness
  49. mo_checkpoint_path = "data/checkpointsMOEAD.csv"
  50. pf_checkpoint_path = "data/pfMOEAD.csv"
  51. def main():
  52. operators = [SimpleBinaryMutation(), SimpleMutation(), SimpleCrossover(), RandomSplitCrossover()]
  53. policy = RandomPolicy(operators)
  54. # pass list of evaluators
  55. algo = MOEAD(20, 5, init, [evaluator1, evaluator2, evaluator2, evaluator2], operators, policy, validator, _maximise=True)
  56. print(algo.weights)
  57. algo.addCallback(MultiCheckpoint(_every=5, _filepath=mo_checkpoint_path))
  58. algo.addCallback(ParetoCheckpoint(_every=5, _filepath=pf_checkpoint_path))
  59. paretoFront = algo.run(10000)
  60. print("Pareto front is composed of", len(paretoFront), "solutions")
  61. if __name__ == "__main__":
  62. main()