documentations.rst 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. ===================
  2. A tour of Macop
  3. ===================
  4. .. image:: _static/logo_macop.png
  5. :width: 300 px
  6. :align: center
  7. This documentation will allow a user who wishes to use the **Macop** optimisation package to understand both how it works and offers examples of how to implement specific needs.
  8. It will gradually take up the major ideas developed within **Macop** to allow for quick development. You can navigate directly via the menu available below to access a specific part of the documentation.
  9. Introduction
  10. ================
  11. `Macop` is a python package for solving discrete optimisation problems in nature. Continuous optimisation is also applicable but not yet developed. The objective is to allow a user to exploit the basic structure proposed by this package to solve a problem specific to him. The interest is that he can quickly abstract himself from the complications related to the way of evaluating, comparing, saving the progress of the search for good solutions but rather concentrate if necessary on his own algorithm. Indeed, `Macop` offers the following main and basic features:
  12. - **solutions:** representation of the solution;
  13. - **validator:** such as constraint programmig, a `validator` is function which is used for validate or not a solution data state;
  14. - **evaluator:** stores problem instance data and implement a `compute` method in order to evaluate a solution;
  15. - **operators:** mutators, crossovers update of solution;
  16. - **policies:** the way you choose the available operators (might be using reinforcement learning);
  17. - **algorithms:** generic and implemented optimisation research algorithms;
  18. - **callbacks:** callbacks to automatically keep track of the search space advancement.
  19. .. image:: _static/documentation/macop_behaviour.png
  20. :width: 50 %
  21. :align: center
  22. Based on all of these generic and/or implemented functionalities, the user will be able to quickly develop a solution to his problem while retaining the possibility of remaining in control of his development by overloading existing functionalities if necessary.
  23. Problem instance
  24. ===================
  25. In this tutorial, we introduce the way of using **Macop** and running your algorithm quickly using the well known `knapsack` problem.
  26. Problem definition
  27. ~~~~~~~~~~~~~~~~~~~~~~
  28. The **knapsack problem** is a problem in combinatorial optimisation: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
  29. The image below provides an illustration of the problem:
  30. .. image:: _static/documentation/knapsack_problem.png
  31. :width: 40 %
  32. :align: center
  33. In this problem, we try to optimise the value associated with the objects we wish to put in our backpack while respecting the capacity of the bag (weight constraint).
  34. .. warning::
  35. It is a combinatorial and therefore discrete problem. **Macop** decomposes its package into two parts, which is related to discrete optimisation on the one hand, and continuous optimisation on the other hand. This will be detailed later.
  36. Problem implementation
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. During the whole tutorial, the example used is based on the previous illustration with:
  39. .. image:: _static/documentation/project_knapsack_problem.png
  40. :width: 85 %
  41. :align: center
  42. Hence, we now define our problem in Python:
  43. - worth value of each objects
  44. - weight associated to each of these objects
  45. .. code-block:: python
  46. """
  47. Problem instance definition
  48. """
  49. elements_score = [ 4, 2, 10, 1, 2 ] # worth of each object
  50. elements_weight = [ 12, 1, 4, 1, 2 ] # weight of each object
  51. Once we have defined the instance of our problem, we will need to define the representation of a solution to that problem.
  52. Let's define the ``SimpleBinaryCrossover`` operator, allows to randomly change a binary value of our current solution.
  53. Solutions
  54. =============
  55. Representing a solution to a specific problem is very important in an optimisation process. In this example, we will always use the **knapsack problem** as a basis.
  56. In a first step, the management of the solutions by the macop package will be presented. Then a specific implementation for the current problem will be detailed.
  57. Generic Solution
  58. ~~~~~~~~~~~~~~~~~~~~~~~~~
  59. Inside macop.solutions.base_ module of `Macop`, the ``Solution`` class is available. It's an abstract solution class structure which:
  60. - stores the solution data representation into its ``data`` attribute
  61. - get ``size`` (shape) of specific data representation
  62. - stores the ``score`` of the solution once a solution is evaluated
  63. Some specific methods are available:
  64. .. code-block:: python
  65. class Solution():
  66. def __init__(self, data, size):
  67. """
  68. Abstract solution class constructor
  69. """
  70. ...
  71. def isValid(self, validator):
  72. """
  73. Use of custom function which checks if a solution is valid or not
  74. """
  75. ...
  76. def evaluate(self, evaluator):
  77. """
  78. Evaluate solution using specific `evaluator`
  79. """
  80. ...
  81. def fitness(self):
  82. """
  83. Returns fitness score
  84. """
  85. ...
  86. @staticmethod
  87. def random(size, validator=None):
  88. """
  89. initialise solution using random data with validator or not
  90. """
  91. ...
  92. def clone(self):
  93. """
  94. Clone the current solution and its data, but without keeping evaluated `_score`
  95. """
  96. ...
  97. From these basic methods, it is possible to manage a representation of a solution to our problem.
  98. Allowing to initialise it randomly or not (using constructor or ``random`` method), to evaluate it (``evaluate`` method) and to check some constraints of validation of the solution (``isValid`` method).
  99. .. note::
  100. Only one of these methods needs specification if we create our own type of solution. This is the ``random`` method, which depends on the need of the problem.
  101. We will now see how to define a type of solution specific to our problem.
  102. Solution representation for knapsack
  103. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  104. We will now use the abstract ``Solution`` type available in the macop.solutions.base_ module in order to define our own solution.
  105. First of all, let's look at the representation of our knapsack problem. **How to represent the solution?**
  106. Knapsack solution
  107. ************************
  108. A valid solution can be shown below where the sum of the object weights is 15 and the sum of the selected objects values is 8 (its fitness):
  109. .. image:: _static/documentation/project_knapsack_solution.png
  110. :width: 85 %
  111. :align: center
  112. Its representation can be translate as a **binary array** with value:
  113. .. code-block::
  114. [1, 1, 0, 0, 1]
  115. where selected objects have **1** as value otherwise **0**.
  116. Binary Solution
  117. **********************
  118. We will now define our own type of solution by inheriting from macop.solutions.base.Solution_, which we will call ``BinarySolution``.
  119. First we will define our new class as inheriting functionality from ``Solution`` (such as child class).
  120. We will also have to implement the ``random`` method to create a new random solution.
  121. .. code-block:: python
  122. """
  123. modules imports
  124. """
  125. from macop.solutions.base import Solution
  126. import numpy as np
  127. class BinarySolution(Solution):
  128. @staticmethod
  129. def random(size, validator=None):
  130. # create binary array of specific size using numpy random module
  131. data = np.random.randint(2, size=size)
  132. # initialise new solution using constructor
  133. solution = BinarySolution(data, size)
  134. # check if validator is set
  135. if not validator:
  136. return solution
  137. # try to generate solution until solution validity (if validator is provided)
  138. while not validator(solution):
  139. data = np.random.randint(2, size=size)
  140. solution = BinarySolution(data, size)
  141. return solution
  142. .. note::
  143. The current developed ``BinarySolution`` is available into macop.solutions.discrete.BinarySolution_ in **Macop**.
  144. Using this new Solution representation, we can now generate solution randomly:
  145. .. code-block:: python
  146. solution = BinarySolution.random(5)
  147. In the next part, we will see how to verify that a solution meets certain modeling constraints of the problem.
  148. Validate a solution
  149. ======================
  150. When an optimisation problem requires respecting certain constraints, Macop allows you to quickly verify that a solution is valid.
  151. It is based on a defined function taking a solution as input and returning the validity criterion (true or false).
  152. Validator definition
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~
  154. An invalid solution can be shown below where the sum of the object weights is greater than 15:
  155. .. image:: _static/documentation/project_knapsack_invalid.png
  156. :width: 85 %
  157. :align: center
  158. In fact, **[1, 0, 1, 0, 0]** is an invalid solution as we have a weight of **16** which violates the knapsack capacity constraint.
  159. To avoid taking into account invalid solutions, we can define our function which will validate or not a solution based on our problem instance:
  160. .. code-block:: python
  161. """
  162. Problem instance definition
  163. """
  164. elements_score = [ 4, 2, 10, 1, 2 ] # worth of each object
  165. elements_weight = [ 12, 1, 4, 1, 2 ] # weight of each object
  166. """
  167. Validator function definition
  168. """
  169. def validator(solution):
  170. weight_sum = 0
  171. for i, w in enumerate(elements_weight):
  172. # add weight if current object is set to 1
  173. weight_sum += w * solution.getData()[i]
  174. # validation condition
  175. return weight_sum <= 15
  176. Use of validator
  177. ~~~~~~~~~~~~~~~~~~~~~
  178. We can now generate solutions randomly by passing our validation function as a parameter:
  179. .. code-block:: python
  180. """
  181. Problem instance definition
  182. """
  183. ...
  184. """
  185. Validator function definition
  186. """
  187. ...
  188. # ensure valid solution
  189. solution = BinarySolution.random(5, validator)
  190. .. caution::
  191. If the search space for valid solutions is very small compared to the overall search space, this can involve a considerable time for validating the solution and therefore obtaining a solution.
  192. The validation of a solution is therefore now possible. In the next part we will focus on the evaluation of a solution.
  193. Use of evaluators
  194. ====================
  195. Now that it is possible to generate a solution randomly or not. It is important to know the value associated with this solution. We will then speak of evaluation of the solution. With the score associated with it, the `fitness`.
  196. Generic evaluator
  197. ~~~~~~~~~~~~~~~~~~~~~~
  198. As for the management of solutions, a generic evaluator class macop.evaluators.base.Evaluator_ is developed within **Macop**:
  199. Abstract Evaluator class is used for computing fitness score associated to a solution. To evaluate all the solutions, this class:
  200. - stores into its ``_data`` dictionary attritute required measures when computing a solution
  201. - has a ``compute`` abstract method enable to compute and associate a score to a given solution
  202. - stores into its ``_algo`` attritute the current algorithm to use (we will talk about algorithm later)
  203. .. code-block: python
  204. class Evaluator():
  205. """
  206. Abstract Evaluator class which enables to compute solution using specific `_data`
  207. """
  208. def __init__(self, data):
  209. self._data = data
  210. @abstractmethod
  211. def compute(self, solution):
  212. """
  213. Apply the computation of fitness from solution
  214. """
  215. pass
  216. def setAlgo(self, algo):
  217. """
  218. Keep into evaluator reference of the whole algorithm
  219. """
  220. self._algo = algo
  221. We must therefore now create our own evaluator based on the proposed structure.
  222. Custom evaluator
  223. ~~~~~~~~~~~~~~~~~~~~~
  224. To create our own evaluator, we need both:
  225. - data useful for evaluating a solution
  226. - calculate the score (fitness) associated with the state of the solution from these data. Hence, implement specific ``compute`` method.
  227. We will define the ``KnapsackEvaluator`` class, which will therefore allow us to evaluate solutions to our current problem.
  228. .. code-block:: python
  229. """
  230. modules imports
  231. """
  232. from macop.evaluators.base import Evaluator
  233. class KnapsackEvaluator(Evaluator):
  234. def compute(solution):
  235. # `_data` contains worths array values of objects
  236. fitness = 0
  237. for index, elem in enumerate(solution.getData()):
  238. fitness += self._data['worths'][index] * elem
  239. return fitness
  240. It is now possible to initialise our new evaluator with specific data of our problem instance:
  241. .. code-block:: python
  242. """
  243. Problem instance definition
  244. """
  245. elements_score = [ 4, 2, 10, 1, 2 ] # worth of each object
  246. elements_weight = [ 12, 1, 4, 1, 2 ] # weight of each object
  247. """
  248. Evaluator problem instance
  249. """
  250. evaluator = KnapsackEvaluator(data={'worths': elements_score})
  251. # using defined BinarySolution
  252. solution = BinarySolution.random(5)
  253. # obtaining current solution score
  254. solution_fitness = solution.evaluate(evaluator)
  255. # score is also stored into solution
  256. solution_fitness = solution.fitness()
  257. .. note::
  258. The current developed ``KnapsackEvaluator`` is available into macop.evaluators.mono.KnapsackEvaluator_ in **Macop**.
  259. In the next part we will see how to modify our current solution with the use of modification operator.
  260. Apply operators to solution
  261. ==============================
  262. Applying an operator to a solution consists of modifying the current state of the solution in order to obtain a new one. The goal is to find a better solution in the search space.
  263. Operators definition
  264. ~~~~~~~~~~~~~~~~~~~~~~~~~
  265. In the discrete optimisation literature, we can categorise operators into two sections:
  266. - **mutators**: modification of one or more elements of a solution from its current state.
  267. - **crossovers**: Inspired by Darwin's theory of evolution, we are going here from two solutions to generate a so-called offspring solution composed of the fusion of the data of the parent solutions.
  268. Inside **Macop**, operators are also decomposed into these two categories. Inside macop.operators.base_, generic class ``Operator`` enables to manage any kind of operator.
  269. .. code-block:: python
  270. class Operator():
  271. """
  272. Abstract Operator class which enables to update solution applying operator (computation)
  273. """
  274. @abstractmethod
  275. def __init__(self):
  276. pass
  277. @abstractmethod
  278. def apply(self, solution):
  279. """
  280. Apply the current operator transformation
  281. """
  282. pass
  283. def setAlgo(self, algo):
  284. """
  285. Keep into operator reference of the whole algorithm
  286. """
  287. self._algo = algo
  288. Like the evaluator, the operator keeps **track of the algorithm** (using ``setAlgo`` method) to which he will be linked. This will allow better management of the way in which the operator must take into account the state of current data relating to the evolution of research.
  289. ``Mutation`` and ``Crossover`` classes inherite from ``Operator``. An ``apply`` function is required for any new operator.
  290. .. code-block:: python
  291. class Mutation(Operator):
  292. """Abstract Mutation extend from Operator
  293. Attributes:
  294. kind: {KindOperator} -- specify the kind of operator
  295. """
  296. def __init__(self):
  297. self._kind = KindOperator.MUTATOR
  298. def apply(self, solution):
  299. raise NotImplementedError
  300. class Crossover(Operator):
  301. """Abstract crossover extend from Operator
  302. Attributes:
  303. kind: {KindOperator} -- specify the kind of operator
  304. """
  305. def __init__(self):
  306. self._kind = KindOperator.CROSSOVER
  307. def apply(self, solution1, solution2):
  308. raise NotImplementedError
  309. We will now detail these categories of operators and suggest some relative to our problem.
  310. Mutator operator
  311. ~~~~~~~~~~~~~~~~~~~~~
  312. As detailed, the mutation operator consists in having a minimum impact on the current state of our solution. Here is an example of a modification that could be done for our problem.
  313. .. image:: _static/documentation/project_knapsack_mutator.png
  314. :width: 90 %
  315. :align: center
  316. In this example we change a bit value randomly and obtain a new solution from our search space.
  317. .. warning::
  318. Applying an operator can conduct to a new but invalid solution from the search space.
  319. The modification applied here is just a bit swapped. Let's define the ``SimpleBinaryMutation`` operator, allows to randomly change a binary value of our current solution.
  320. .. code-block:: python
  321. """
  322. modules imports
  323. """
  324. from macop.operators.discrete.base import Mutation
  325. class SimpleBinaryMutation(Mutation):
  326. def apply(self, solution):
  327. # obtain targeted cell using solution size
  328. size = solution._size
  329. cell = random.randint(0, size - 1)
  330. # copy of solution
  331. copy_solution = solution.clone()
  332. # swicth values
  333. if copy_solution.getData()[cell]:
  334. copy_solution.getData()[cell] = 0
  335. else:
  336. copy_solution.getData()[cell] = 1
  337. # return the new obtained solution
  338. return copy_solution
  339. We can now instanciate our new operator in order to obtain a new solution:
  340. .. code-block:: python
  341. """
  342. BinaryMutator instance
  343. """
  344. mutator = SimpleBinaryMutation()
  345. # using defined BinarySolution
  346. solution = BinarySolution.random(5)
  347. # obtaining new solution using operator
  348. new_solution = mutator.apply(solution)
  349. .. note::
  350. The developed ``SimpleBinaryMutation`` is available into macop.operators.discrete.mutators.SimpleBinaryMutation_ in **Macop**.
  351. Crossover operator
  352. ~~~~~~~~~~~~~~~~~~~~~~~
  353. Inspired by Darwin's theory of evolution, crossover starts from two solutions to generate a so-called offspring solution composed of the fusion of the data of the parent solutions.
  354. .. image:: _static/documentation/project_knapsack_crossover.png
  355. :width: 95%
  356. :align: center
  357. In this example we merge two solutions with a specific splitting criterion in order to obtain an offspring.
  358. We will now implement the SimpleCrossover crossover operator, which will merge data from two solutions.
  359. The first half of solution 1 will be saved and added to the second half of solution 2 to generate the new solution (offspring).
  360. .. code-block:: python
  361. """
  362. modules imports
  363. """
  364. from macop.operators.discrete.base import Crossover
  365. class SimpleCrossover(Crossover):
  366. def apply(self, solution1, solution2):
  367. size = solution1._size
  368. # default split index used
  369. splitIndex = int(size / 2)
  370. # copy data of solution 1
  371. firstData = solution1._data.copy()
  372. # copy of solution 2
  373. copy_solution = solution2.clone()
  374. copy_solution.getData()[splitIndex:] = firstData[splitIndex:]
  375. return copy_solution
  376. We can now use the crossover operator created to generate new solutions. Here is an example of use:
  377. .. code-block:: python
  378. """
  379. SimpleCrossover instance
  380. """
  381. crossover = SimpleCrossover()
  382. # using defined BinarySolution
  383. solution1 = BinarySolution.random(5)
  384. solution2 = BinarySolution.random(5)
  385. # obtaining new solution using crossover
  386. offspring = crossover.apply(solution1, solution2)
  387. .. tip::
  388. The developed ``SimpleCrossover`` is available into macop.operators.discrete.crossovers.SimpleCrossover_ in **Macop**.
  389. However, the choice of halves of the merged data is made randomly.
  390. Next part introduce the ``policy`` feature of **Macop** which enables to choose the next operator to apply during the search process based on specific criterion.
  391. Operator choices
  392. ===================
  393. The ``policy`` feature of **Macop** enables to choose the next operator to apply during the search process of the algorithm based on specific criterion.
  394. Why using policy ?
  395. ~~~~~~~~~~~~~~~~~~~~~~~
  396. Sometimes the nature of the problem and its instance can strongly influence the search results when using mutation operators or crossovers.
  397. Automated operator choice strategies have also been developed in the literature, notably based on reinforcement learning.
  398. The operator choice problem can be seen as the desire to find the best solution generation operator at the next evaluation that will be the most conducive to precisely improving the solution.
  399. .. image:: _static/documentation/operators_choice.png
  400. :width: 45 %
  401. :align: center
  402. .. note::
  403. An implementation using reinforcement learning has been developed as an example in the macop.policies.reinforcement_ module.
  404. However, it will not be detailed here. You can refer to the API documentation for more details.
  405. Custom policy
  406. ~~~~~~~~~~~~~~~~~~
  407. In our case, we are not going to exploit a complex enough implementation of a ``policy``. Simply, we will use a random choice of operator.
  408. First, let's take a look of the ``policy`` abstract class available in macop.policies.base_:
  409. .. code-block:: python
  410. class Policy():
  411. def __init__(self, operators):
  412. self._operators = operators
  413. @abstractmethod
  414. def select(self):
  415. """
  416. Select specific operator
  417. """
  418. pass
  419. def apply(self, solution):
  420. """
  421. Apply specific operator to create new solution, compute its fitness and return it
  422. """
  423. ...
  424. def setAlgo(self, algo):
  425. """
  426. Keep into policy reference of the whole algorithm
  427. """
  428. ...
  429. ``Policy`` instance will have of ``_operators`` attributs in order to keep track of possible operators when selecting one.
  430. Here, in our implementation we only need to specify the ``select`` abstract method. The ``apply`` method will select the next operator and return the new solution.
  431. .. code-block:: python
  432. """
  433. module imports
  434. """
  435. from macop.policies.base import Policy
  436. class RandomPolicy(Policy):
  437. def select(self):
  438. """
  439. Select specific operator
  440. """
  441. # choose operator randomly
  442. index = random.randint(0, len(self._operators) - 1)
  443. return self._operators[index]
  444. We can now use this operator choice policy to update our current solution:
  445. .. code-block:: python
  446. """
  447. Operators instances
  448. """
  449. mutator = SimpleMutation()
  450. crossover = SimpleCrossover()
  451. """
  452. RandomPolicy instance
  453. """
  454. policy = RandomPolicy([mutator, crossover])
  455. """
  456. Current solutions instance
  457. """
  458. solution1 = BinarySolution.random(5)
  459. solution2 = BinarySolution.random(5)
  460. # pass two solutions in parameters in case of selected crossover operator
  461. new_solution = policy.apply(solution1, solution2)
  462. .. caution::
  463. By default if ``solution2`` parameter is not provided into ``policy.apply`` method for crossover, the best solution known is used from the algorithm linked to the ``policy``.
  464. Updating solutions is therefore now possible with our policy. It is high time to dive into the process of optimizing solutions and digging into our research space.
  465. Optimisation process
  466. =======================
  467. Let us now tackle the interesting part concerning the search for optimum solutions in our research space.
  468. Find local and global optima
  469. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  470. Overall, in an optimization process, we will seek to find the best, or the best solutions that minimize or maximize our objective function (fitness score obtained) in order to respond to our problem.
  471. .. image:: _static/documentation/search_space.png
  472. :width: 95 %
  473. :align: center
  474. Sometimes, the search space can be very simple. A local search can provide access to the global optimum as shown in figure (a) above.
  475. In other cases, the search space is more complex. It may be necessary to explore more rather than exploit in order to get out of a convex zone and not find the global optimum but only a local opmatime solution.
  476. This problem is illustrated in figure (b).
  477. Abstract algorithm class
  478. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  479. An abstract class is proposed within Macop to generalize the management of an algorithm and therefore of a heuristic.
  480. It is located in the macop.algorithms.base_ module.
  481. We will pay attention to the different methods of which she is composed. This class enables to manage some common usages of operation research algorithms:
  482. - initialization function of solution
  483. - validator function to check if solution is valid or not (based on some criteria)
  484. - evaluation function to give fitness score to a solution
  485. - operators used in order to update solution during search process
  486. - policy process applied when choosing next operator to apply
  487. - callbacks function in order to do some relative stuff every number of evaluation or reload algorithm state
  488. - parent algorithm associated to this new algorithm instance (hierarchy management)
  489. She is composed of few default attributes:
  490. - initialiser: {function} -- basic function strategy to initialise solution
  491. - evaluator: {Evaluator} -- evaluator instance in order to obtained fitness (mono or multiple objectives)
  492. - operators: {[Operator]} -- list of operator to use when launching algorithm
  493. - policy: {Policy} -- Policy instance strategy to select operators
  494. - validator: {function} -- basic function to check if solution is valid or not under some constraints
  495. - maximise: {bool} -- specify kind of optimisation problem
  496. - verbose: {bool} -- verbose or not information about the algorithm
  497. - currentSolution: {Solution} -- current solution managed for current evaluation comparison
  498. - bestSolution: {Solution} -- best solution found so far during running algorithm
  499. - callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initialising algorithm
  500. - parent: {Algorithm} -- parent algorithm reference in case of inner Algorithm instance (optional)
  501. .. code-block:: python
  502. class Algorithm():
  503. def __init__(self,
  504. initialiser,
  505. evaluator,
  506. operators,
  507. policy,
  508. validator,
  509. maximise=True,
  510. parent=None,
  511. verbose=True):
  512. ...
  513. def addCallback(self, callback):
  514. """
  515. Add new callback to algorithm specifying usefull parameters
  516. """
  517. ...
  518. def resume(self):
  519. """
  520. Resume algorithm using Callback instances
  521. """
  522. ...
  523. def getParent(self):
  524. """
  525. Recursively find the main parent algorithm attached of the current algorithm
  526. """
  527. ...
  528. def setParent(self, parent):
  529. """
  530. Set parent algorithm to current algorithm
  531. """
  532. ...
  533. def initRun(self):
  534. """
  535. initialise the current solution and best solution using the `initialiser` function
  536. """
  537. ...
  538. def increaseEvaluation(self):
  539. """
  540. Increase number of evaluation once a solution is evaluated for each dependant algorithm (parents hierarchy)
  541. """
  542. ...
  543. def getGlobalEvaluation(self):
  544. """
  545. Get the global number of evaluation (if inner algorithm)
  546. """
  547. ...
  548. def getGlobalMaxEvaluation(self):
  549. """
  550. Get the global max number of evaluation (if inner algorithm)
  551. """
  552. ...
  553. def stop(self):
  554. """
  555. Global stopping criteria (check for parents algorithm hierarchy too)
  556. """
  557. ...
  558. def evaluate(self, solution):
  559. """
  560. Evaluate a solution using evaluator passed when intialize algorithm
  561. """
  562. ...
  563. def update(self, solution):
  564. """
  565. Apply update function to solution using specific `policy`
  566. Check if solution is valid after modification and returns it
  567. """
  568. ...
  569. def isBetter(self, solution):
  570. """
  571. Check if solution is better than best found
  572. """
  573. ...
  574. def run(self, evaluations):
  575. """
  576. Run the specific algorithm following number of evaluations to find optima
  577. """
  578. ...
  579. def progress(self):
  580. """
  581. Log progress and apply callbacks if necessary
  582. """
  583. ...
  584. The notion of hierarchy between algorithms is introduced here. We can indeed have certain dependencies between algorithms.
  585. The methods ``increaseEvaluation``, ``getGlobalEvaluation`` and ``getGlobalMaxEvaluation`` ensure that the expected global number of evaluations is correctly managed, just like the ``stop`` method for the search stop criterion.
  586. The ``evaluate``, ``update`` and ``isBetter`` will be used a lot when looking for a solution in the search space.
  587. In particular the ``update`` function, which will call the ``policy`` instance to generate a new valid solution.
  588. ``isBetter`` method is also overloadable especially if the algorithm does not take any more into account than a single solution to be verified (verification via a population for example).
  589. The ``initRun`` method specify the way you intialise your algorithm (``bestSolution`` and ``currentSolution`` as example) if algorithm not already initialised.
  590. .. note::
  591. The ``initRun`` method can also be used for intialise population of solutions instead of only one best solution, if you want to manage a genetic algorithm.
  592. Most important part is the ``run`` method. Into abstract, the ``run`` method only initialised the current number of evaluation for the algorithm based on the parent algorithm if we are into inner algorithm.
  593. It is always **mandatory** to call the parent class ``run`` method using ``super().run(evaluations)``. Then, using ``evaluations`` parameter which is the number of evaluations budget to run, we can process or continue to find solutions into search space.
  594. .. warning::
  595. The other methods such as ``addCallback``, ``resume`` and ``progress`` will be detailed in the next part focusing on the notion of callback.
  596. Local search algorithm
  597. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  598. We are going to carry out our first local search algorithm within our search space. A `local search` consists of starting from a solution, then applying a mutation or crossover operation to it, in order to obtain a new one.
  599. This new solution is evaluated and retained if it is better. We will speak here of the notion of **neighborhood exploration**. The process is then completed in the same way.
  600. The local search ends after a certain number of evaluations and the best evaluated solution obtained is returned.
  601. Let's implement an algorithm well known under the name of hill climber best improvment inheriting from the mother algorithm class and name it ``HillClimberBestImprovment``.
  602. .. code-block:: python
  603. """
  604. module imports
  605. """
  606. from macop.algorithms.base import Algorithm
  607. class HillClimberBestImprovment(Algorithm):
  608. def run(self, evaluations):
  609. """
  610. Run a local search algorithm
  611. """
  612. # by default use of mother method to initialise variables
  613. super().run(evaluations)
  614. # initialise current solution and best solution
  615. self.initRun()
  616. solutionSize = self._currentSolution._size
  617. # local search algorithm implementation
  618. while not self.stop():
  619. for _ in range(solutionSize):
  620. # update current solution using policy
  621. newSolution = self.update(self._currentSolution)
  622. # if better solution than currently, replace it
  623. if self.isBetter(newSolution):
  624. self._bestSolution = newSolution
  625. # increase number of evaluations
  626. self.increaseEvaluation()
  627. # stop algorithm if necessary
  628. if self.stop():
  629. break
  630. # set new current solution using best solution found in this neighbor search
  631. self._currentSolution = self._bestSolution
  632. return self._bestSolution
  633. Our algorithm is now ready to work. As previously, let us define two operators as well as a random choice strategy.
  634. We will also need to define a **solution initialisation function** so that the algorithm can generate new solutions.
  635. .. code-block:: python
  636. """
  637. Problem instance definition
  638. """
  639. elements_score = [ 4, 2, 10, 1, 2 ] # worth of each object
  640. elements_weight = [ 12, 1, 4, 1, 2 ] # weight of each object
  641. # evaluator instance
  642. evaluator = KnapsackEvaluator(data={'worths': elements_score})
  643. # valid instance using lambda
  644. validator = lambda solution: sum([ elements_weight[i] * solution.getData()[i] for i in range(len(solution.getData()))]) <= 15
  645. # initialiser instance using lambda with default param value
  646. initialiser = lambda x=5: BinarySolution.random(x, validator)
  647. # operators list with crossover and mutation
  648. operators = [SimpleCrossover(), SimpleMutation()]
  649. # policy random instance
  650. policy = RandomPolicy(operators)
  651. # maximizing algorithm (relative to knapsack problem)
  652. algo = HillClimberBestImprovment(initialiser, evaluator, operators, policy, validator, maximise=True, verbose=False)
  653. # run the algorithm and get solution found
  654. solution = algo.run(100)
  655. print(solution.fitness())
  656. .. note::
  657. The ``verbose`` algorithm parameter will log into console the advancement process of the algorithm is set to ``True`` (the default value).
  658. Exploratory algorithm
  659. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  660. As explained in **figure (b)** of **section 8.1**, sometimes the search space is more complicated due to convex parts and need heuristic with other strategy rather than a simple local search.
  661. The way to counter this problem is to allow the algorithm to exit the exploitation phase offered by local search. But rather to seek to explore other parts of the research space. This is possible by simply carrying out several local searches with our budget (number of evaluations).
  662. The idea is to make a leap in the search space in order to find a new local optimum which can be the global optimum. The explained process is illustrated below:
  663. .. image:: _static/documentation/search_space_simple.png
  664. :width: 45 %
  665. :align: center
  666. We are going to implement a more specific algorithm allowing to take a new parameter as input. This is a local search, the one previously developed. For that, we will have to modify the constructor a little.
  667. Let's called this new algorithm ``IteratedLocalSearch``:
  668. .. code-block:: python
  669. """
  670. module imports
  671. """
  672. from macop.algorithms.base import Algorithm
  673. class IteratedLocalSearch(Algorithm):
  674. def __init__(self,
  675. initialiser,
  676. evaluator,
  677. operators,
  678. policy,
  679. validator,
  680. localSearch,
  681. maximise=True,
  682. parent=None,
  683. verbose=True):
  684. super().__init__(initialiser, evaluator, operators, policy, validator, maximise, parent, verbose)
  685. # specific local search associated with current algorithm
  686. self._localSearch = localSearch
  687. # need to attach current algorithm as parent
  688. self._localSearch.setParent(self)
  689. def run(self, evaluations, ls_evaluations=100):
  690. """
  691. Run the iterated local search algorithm using local search
  692. """
  693. # by default use of mother method to initialise variables
  694. super().run(evaluations)
  695. # initialise current solution
  696. self.initRun()
  697. # local search algorithm implementation
  698. while not self.stop():
  699. # create and search solution from local search (stop method can be called inside local search)
  700. newSolution = self._localSearch.run(ls_evaluations)
  701. # if better solution than currently, replace it
  702. if self.isBetter(newSolution):
  703. self._bestSolution = newSolution
  704. self.information()
  705. return self._bestSolution
  706. In the initialization phase we have attached our local search passed as a parameter with the current algorithm as parent.
  707. The goal is to touch keep track of the overall search evaluation number (relative to the parent algorithm).
  708. Then, we use this local search in our ``run`` method to allow a better search for solutions.
  709. .. code-block:: python
  710. """
  711. Problem instance definition
  712. """
  713. elements_score = [ 4, 2, 10, 1, 2 ] # worth of each object
  714. elements_weight = [ 12, 1, 4, 1, 2 ] # weight of each object
  715. # evaluator instance
  716. evaluator = KnapsackEvaluator(data={'worths': elements_score})
  717. # valid instance using lambda
  718. validator = lambda solution: sum([ elements_weight[i] * solution.getData()[i] for i in range(len(solution.getData()))]) <= 15
  719. # initialiser instance using lambda with default param value
  720. initialiser = lambda x=5: BinarySolution.random(x, validator)
  721. # operators list with crossover and mutation
  722. operators = [SimpleCrossover(), SimpleMutation()]
  723. # policy random instance
  724. policy = RandomPolicy(operators)
  725. # maximizing algorithm (relative to knapsack problem)
  726. localSearch = HillClimberBestImprovment(initialiser, evaluator, operators, policy, validator, maximise=True, verbose=False)
  727. algo = IteratedLocalSearch(initialiser, evaluator, operators, policy, validator, localSearch=local_search, maximise=True, verbose=False)
  728. # run the algorithm using local search and get solution found
  729. solution = algo.run(evaluations=100, ls_evaluations=10)
  730. print(solution.fitness())
  731. .. note::
  732. These two last algorithms developed are available in the library within the module macop.algorithms.mono_.
  733. We have one final feature to explore in the next part. This is the notion of ``callback``.
  734. Keep track
  735. ==============
  736. Keeping track of the running algorithm can be useful on two levels. First of all to understand how it unfolded at the end of the classic run. But also in the case of the unwanted shutdown of the algorithm.
  737. This section will allow you to introduce the recovery of the algorithm thanks to a continuous backup functionality.
  738. Logging into algorithm
  739. ~~~~~~~~~~~~~~~~~~~~~~
  740. Some logs can be retrieve after running an algorithm. **Macop** uses the ``logging`` Python package in order to log algorithm advancement.
  741. Here is an example of use when running an algorithm:
  742. .. code-block:: python
  743. """
  744. basic imports
  745. """
  746. import logging
  747. # logging configuration
  748. logging.basicConfig(format='%(asctime)s %(message)s', filename='data/example.log', level=logging.DEBUG)
  749. ...
  750. # maximizing algorithm (relative to knapsack problem)
  751. algo = HillClimberBestImprovment(initialiser, evaluator, operators, policy, validator, maximise=True, verbose=False)
  752. # run the algorithm using local search and get solution found
  753. solution = algo.run(evaluations=100)
  754. print(solution.fitness())
  755. Hence, log data are saved into ``data/example.log`` in our example.
  756. Callbacks introduction
  757. ~~~~~~~~~~~~~~~~~~~~~~~
  758. Having output logs can help to understand an error that has occurred, however all the progress of the research carried out may be lost.
  759. For this, the functionality relating to callbacks has been developed.
  760. Within **Macop**, a callback is a specific instance of macop.callbacks.base.Callback_ that allows you to perform an action of tracing / saving information **every** ``n`` **evaluations** but also reloading information if necessary when restarting an algorithm.
  761. .. code-block:: python
  762. class Callback():
  763. def __init__(self, every, filepath):
  764. ...
  765. @abstractmethod
  766. def run(self):
  767. """
  768. Check if necessary to do backup based on `every` variable
  769. """
  770. pass
  771. @abstractmethod
  772. def load(self):
  773. """
  774. Load last backup line of solution and set algorithm state at this backup
  775. """
  776. pass
  777. def setAlgo(self, algo):
  778. """
  779. Specify the main algorithm instance reference
  780. """
  781. ...
  782. - The ``run`` method will be called during run process of the algo and do backup at each specific number of evaluations.
  783. - The ``load`` method will be used to reload the state of the algorithm from the last information saved. All saved data is saved in a file whose name will be specified by the user.
  784. Towards the use of Callbacks
  785. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  786. We are going to create our own Callback instance called ``BasicCheckpoint`` which will save the best solution found and number of evaluations done in order to reload it for the next run of our algorithm.
  787. .. code-block:: python
  788. """
  789. module imports
  790. """
  791. from macop.callbacks.base import Callback
  792. class BasicCheckpoint(Callback):
  793. def run(self):
  794. """
  795. Check if necessary to do backup based on `every` variable
  796. """
  797. # get current best solution
  798. solution = self._algo._bestSolution
  799. currentEvaluation = self._algo.getGlobalEvaluation()
  800. # backup if necessary every number of evaluations
  801. if currentEvaluation % self._every == 0:
  802. # create specific line with solution data
  803. solutionData = ""
  804. solutionSize = len(solution.getData())
  805. for index, val in enumerate(solution.getData()):
  806. solutionData += str(val)
  807. if index < solutionSize - 1:
  808. solutionData += ' '
  809. # number of evaluations done, solution data and fitness score
  810. line = str(currentEvaluation) + ';' + solutionData + ';' + str(
  811. solution.fitness()) + ';\n'
  812. # check if file exists
  813. if not os.path.exists(self._filepath):
  814. with open(self._filepath, 'w') as f:
  815. f.write(line)
  816. else:
  817. with open(self._filepath, 'a') as f:
  818. f.write(line)
  819. def load(self):
  820. """
  821. Load last backup line and set algorithm state (best solution and evaluations)
  822. """
  823. if os.path.exists(self._filepath):
  824. with open(self._filepath) as f:
  825. # get last line and read data
  826. lastline = f.readlines()[-1]
  827. data = lastline.split(';')
  828. # get evaluation information
  829. globalEvaluation = int(data[0])
  830. # restore number of evaluations
  831. if self._algo.getParent() is not None:
  832. self._algo.getParent()._numberOfEvaluations = globalEvaluation
  833. else:
  834. self._algo._numberOfEvaluations = globalEvaluation
  835. # get best solution data information
  836. solutionData = list(map(int, data[1].split(' ')))
  837. # avoid uninitialised solution
  838. if self._algo._bestSolution is None:
  839. self._algo._bestSolution = self._algo.initialiser()
  840. # set to algorithm the lastest obtained best solution
  841. self._algo._bestsolution.getData() = np.array(solutionData)
  842. self._algo._bestSolution._score = float(data[2])
  843. In this way, it is possible to specify the use of a callback to our algorithm instance:
  844. .. code-block:: python
  845. ...
  846. # maximizing algorithm (relative to knapsack problem)
  847. algo = HillClimberBestImprovment(initialiser, evaluator, operators, policy, validator, maximise=True, verbose=False)
  848. callback = BasicCheckpoint(every=5, filepath='data/hillClimberBackup.csv')
  849. # add callback into callback list
  850. algo.addCallback(callback)
  851. # run the algorithm using local search and get solution found
  852. solution = algo.run(evaluations=100)
  853. print(solution.fitness())
  854. .. note::
  855. It is possible to add as many callbacks as desired in the algorithm in question.
  856. Previously, some methods of the abstract ``Algorithm`` class have not been presented. These methods are linked to the use of callbacks,
  857. in particular the ``addCallback`` method which allows the addition of a callback to an algorithm instance as seen above.
  858. - The ``resume`` method will reload all callbacks list using ``load`` method.
  859. - The ``progress`` method will ``run`` each callbacks during the algorithm search.
  860. If we want to exploit this functionality, then we will need to exploit them within our algorithm. Let's make the necessary modifications for our algorithm ``IteratedLocalSearch``:
  861. .. code-block:: python
  862. """
  863. module imports
  864. """
  865. from macop.algorithms.base import Algorithm
  866. class IteratedLocalSearch(Algorithm):
  867. ...
  868. def run(self, evaluations, ls_evaluations=100):
  869. """
  870. Run the iterated local search algorithm using local search
  871. """
  872. # by default use of mother method to initialise variables
  873. super().run(evaluations)
  874. # initialise current solution
  875. self.initRun()
  876. # restart using callbacks backup list
  877. self.resume()
  878. # local search algorithm implementation
  879. while not self.stop():
  880. # create and search solution from local search
  881. newSolution = self._localSearch.run(ls_evaluations)
  882. # if better solution than currently, replace it
  883. if self.isBetter(newSolution):
  884. self._bestSolution = newSolution
  885. # check if necessary to call each callbacks
  886. self.progress()
  887. self.information()
  888. return self._bestSolution
  889. All the features of **Macop** were presented. The next section will aim to quickly present the few implementations proposed within **Macop** to highlight the modulality of the package.
  890. Implementation examples
  891. =======================
  892. Within the API of **Macop**, you can find an implementation of The Multi-objective evolutionary algorithm based on decomposition (MOEA/D) is a general-purpose algorithm for approximating the Pareto set of multi-objective optimization problems.
  893. It decomposes the original multi-objective problem into a number of single-objective optimization sub-problems and then uses an evolutionary process to optimize these sub-problems simultaneously and cooperatively.
  894. MOEA/D is a state-of-art algorithm in aggregation-based approaches for multi-objective optimization.
  895. .. image:: _static/documentation/search_space_moead.png
  896. :width: 45 %
  897. :align: center
  898. As illustrated below, the two main objectives are sub-divised into 5 single-objective optimization sub-problems in order to find the Pareto front.
  899. - macop.algorithms.multi.MOSubProblem_ class defines each sub-problem of MOEA/D.
  900. - macop.algorithms.multi.MOEAD_ class exploits ``MOSubProblem`` and implements MOEA/D using weighted-sum of objectives method.
  901. An example with MOEAD for knapsack problem is available in knapsackMultiExample.py_.
  902. .. _knapsackMultiExample.py: https://github.com/jbuisine/macop/blob/master/examples/knapsackMultiExample.py
  903. .. _macop.algorithms.base: macop/macop.algorithms.base.html#module-macop.algorithms.base
  904. .. _macop.algorithms.mono: macop/macop.algorithms.mono.html#module-macop.algorithms.mono
  905. .. _macop.solutions.base: macop/macop.solutions.base.html#module-macop.solutions.base
  906. .. _macop.solutions.base.Solution: macop/macop.solutions.base.html#macop.solutions.base.Solution
  907. .. _macop.solutions.discrete.BinarySolution: macop/macop.solutions.discrete.html#macop.solutions.discrete.BinarySolution
  908. .. _macop.evaluators.base.Evaluator: macop/macop.evaluators.base.html#macop.evaluators.base.Evaluator
  909. .. _macop.evaluators.mono.KnapsackEvaluator: macop/macop.evaluators.mono.html#macop.evaluators.mono.KnapsackEvaluator
  910. .. _macop.operators.base: macop/macop.operators.base.html#module-macop.operators.base
  911. .. _macop.operators.discrete.mutators.SimpleBinaryMutation: macop/macop.operators.discrete.mutators.html#macop.operators.discrete.mutators.SimpleBinaryMutation
  912. .. _macop.operators.discrete.crossovers.SimpleCrossover: macop/macop.operators.discrete.crossovers.html#macop.operators.discrete.crossovers.SimpleCrossover
  913. .. _macop.policies.reinforcement: macop.policies.reinforcement.html#module-macop.policies.reinforcement
  914. .. _macop.policies.base: macop.policies.base.html#module-macop.policies.base
  915. .. _macop.callbacks.base.Callback: macop/macop.callbacks.base.html#macop.callbacks.base.Callback
  916. .. _macop.algorithms.multi.MOSubProblem: macop/macop.algorithms.multi.html#macop.algorithms.multi.MOSubProblem
  917. .. _macop.algorithms.multi.MOEAD: macop/macop.algorithms.multi.html#macop.algorithms.multi.MOEAD