setup.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from setuptools import setup
  2. import distutils.command.check
  3. class TestCommand(distutils.command.check.check):
  4. """Custom test command."""
  5. def run(self):
  6. # run tests using doctest
  7. import doctest
  8. # folders
  9. # from macop.algorithms import Algorithm
  10. print("==============================")
  11. print("Run test program...")
  12. from macop.solutions.BinarySolution import BinarySolution
  13. from macop.evaluators.EvaluatorExample import evaluatorExample
  14. from macop.operators.mutators.SimpleMutation import SimpleMutation
  15. from macop.operators.mutators.SimpleBinaryMutation import SimpleBinaryMutation
  16. from macop.operators.crossovers.SimpleCrossover import SimpleCrossover
  17. from macop.operators.crossovers.RandomSplitCrossover import RandomSplitCrossover
  18. from macop.operators.policies.RandomPolicy import RandomPolicy
  19. from macop.operators.policies.UCBPolicy import UCBPolicy
  20. from macop.algorithms.mono.IteratedLocalSearch import IteratedLocalSearch as ILS
  21. from macop.callbacks.BasicCheckpoint import BasicCheckpoint
  22. import random
  23. random.seed(42)
  24. elements_score = [ random.randint(1, 20) for _ in range(30) ]
  25. elements_weight = [ random.randint(2, 5) for _ in range(30) ]
  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) <= 80:
  34. return True
  35. else:
  36. False
  37. # define init random solution
  38. def init():
  39. return BinarySolution([], 30).random(validator)
  40. def evaluator(solution):
  41. fitness = 0
  42. for index, elem in enumerate(solution._data):
  43. fitness += (elements_score[index] * elem)
  44. return fitness
  45. operators = [SimpleBinaryMutation(), SimpleMutation(), SimpleCrossover(), RandomSplitCrossover()]
  46. policy = UCBPolicy(operators)
  47. # callback = BasicCheckpoint(_every=5, _filepath=filepath)
  48. algo = ILS(init, evaluator, operators, policy, validator, maximise=True)
  49. # add callback into callback list
  50. # algo.addCallback(callback)
  51. algo.run(200)
  52. print("==============================")
  53. print("Run test using doctest...")
  54. # pass test using doctest
  55. distutils.command.check.check.run(self)
  56. setup(
  57. name='macop',
  58. version='1.0.5',
  59. description='Minimalist And Customisable Optimisation Package',
  60. long_description=open('README.md').read(),
  61. long_description_content_type='text/markdown',
  62. classifiers=[
  63. 'Development Status :: 4 - Beta',
  64. 'License :: OSI Approved :: MIT License',
  65. 'Programming Language :: Python :: 3.7',
  66. 'Topic :: Scientific/Engineering',
  67. 'Topic :: Utilities'
  68. ],
  69. url='https://github.com/prise-3d/macop',
  70. author='Jérôme BUISINE',
  71. author_email='jerome.buisine@univ-littoral.fr',
  72. license='MIT',
  73. packages=['macop',
  74. 'macop.algorithms',
  75. 'macop.algorithms.mono',
  76. 'macop.algorithms.multi',
  77. 'macop.callbacks',
  78. 'macop.evaluators',
  79. 'macop.operators',
  80. 'macop.operators.mutators',
  81. 'macop.operators.crossovers',
  82. 'macop.operators.policies',
  83. 'macop.solutions',
  84. 'macop.utils'],
  85. install_requires=[
  86. 'numpy',
  87. ],
  88. cmdclass={
  89. 'test': TestCommand,
  90. },
  91. zip_safe=False)