validator.rst 838 B

1234567891011121314151617181920212223242526272829303132333435
  1. 4. Validate a solution
  2. ======================
  3. When an optimization problem requires respecting certain constraints, Macop allows you to quickly verify that a solution is valid.
  4. It is based on a defined function taking a solution as input and returning the validity criterion (true or false).
  5. 4.1. Validator definition
  6. ~~~~~~~~~~~~~~~~~~~~~~~~~
  7. .. code-block:: python
  8. """
  9. Problem instance definition
  10. """
  11. elements_score = [ 4, 2, 10, 1, 2 ] # value of each object
  12. elements_weight = [ 12, 1, 4, 1, 2 ] # weight of each object
  13. """
  14. validator function definition
  15. """
  16. def validator(solution):
  17. weight_sum = 0
  18. for i, w in enumerate(elements_weight):
  19. weight_sum += w * solution._data[i]
  20. return weight_sum <= 15
  21. 4.2. Use of validator
  22. ~~~~~~~~~~~~~~~~~~~~~