knapsackMultiExample.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from macop.callbacks.UCBCheckpoint import UCBCheckpoint
  18. if not os.path.exists('data'):
  19. os.makedirs('data')
  20. # logging configuration
  21. logging.basicConfig(format='%(asctime)s %(message)s', filename='data/exampleMOEAD.log', level=logging.DEBUG)
  22. random.seed(42)
  23. elements_score1 = [ random.randint(1, 100) for _ in range(500) ]
  24. elements_score2 = [ random.randint(1, 200) for _ in range(500) ]
  25. elements_weight = [ random.randint(90, 100) for _ in range(500) ]
  26. def knapsackWeight(_solution):
  27. weight_sum = 0
  28. for index, elem in enumerate(_solution.data):
  29. weight_sum += elements_weight[index] * elem
  30. return weight_sum
  31. # default validator
  32. def validator(_solution):
  33. if knapsackWeight(_solution) <= 15000:
  34. return True
  35. else:
  36. False
  37. # define init random solution
  38. def init():
  39. return BinarySolution([], 200).random(validator)
  40. def evaluator1(_solution):
  41. fitness = 0
  42. for index, elem in enumerate(_solution.data):
  43. fitness += (elements_score1[index] * elem)
  44. return fitness
  45. def evaluator2(_solution):
  46. fitness = 0
  47. for index, elem in enumerate(_solution.data):
  48. fitness += (elements_score2[index] * elem)
  49. return fitness
  50. mo_checkpoint_path = "data/checkpointsMOEAD.csv"
  51. pf_checkpoint_path = "data/pfMOEAD.csv"
  52. ucb_checkpoint_path = "data/UCBPolicyMOEAD.csv"
  53. def main():
  54. operators = [SimpleBinaryMutation(), SimpleMutation(), SimpleCrossover(), RandomSplitCrossover()]
  55. policy = UCBPolicy(operators, _C=100, _exp_rate=0.2)
  56. # pass list of evaluators
  57. algo = MOEAD(20, 5, init, [evaluator1, evaluator2, evaluator2, evaluator2], operators, policy, validator, _maximise=True)
  58. algo.addCallback(MultiCheckpoint(_every=5, _filepath=mo_checkpoint_path))
  59. algo.addCallback(ParetoCheckpoint(_every=5, _filepath=pf_checkpoint_path))
  60. algo.addCallback(UCBCheckpoint(_every=5, _filepath=ucb_checkpoint_path))
  61. paretoFront = algo.run(10000)
  62. print("Pareto front is composed of", len(paretoFront), "solutions")
  63. if __name__ == "__main__":
  64. main()