Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpn_mutate.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2020,2023 Weber Yann
  3. *
  4. * This file is part of pyrpn.
  5. *
  6. * pyrpn is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * pyrpn is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with pyrpn. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __rpn_mutate__h__
  20. #define __rpn_mutate__h__
  21. #include "config.h"
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <stdint.h>
  25. #include <sys/random.h>
  26. #include "rpn_parse.h"
  27. /**@file rpn_mutate.h
  28. * @brief Expression mutation headers
  29. */
  30. /**@brief Mutation parameters */
  31. typedef struct rpn_mutation_params_s rpn_mutation_params_t;
  32. /**@brief Type of random values used by mutations */
  33. typedef uint16_t rnd_t;
  34. /** @brief Mutation parameters */
  35. struct rpn_mutation_params_s
  36. {
  37. /**@brief Minimum expression length */
  38. size_t min_len;
  39. /**@brief Weight of adding a token*/
  40. float w_add;
  41. /**@brief Weight of deleting a token*/
  42. float w_del;
  43. /**@brief Weight of mutating a token*/
  44. float w_mut;
  45. /**@brief Weight of mutating a token without type change*/
  46. float w_mut_soft;
  47. /**@brief Weight for each token types (op, const, var) when
  48. * adding a token*/
  49. float w_add_elt[3];
  50. /**@brief Weight for each token types (op, const, var) when
  51. * mutating a token*/
  52. float w_mut_elt[3];
  53. /**@brief For internal use, set by rpn_mutation_init_params
  54. *
  55. * weight reported by groups on rnd_t integers
  56. * - [0..3] -> weights mutation
  57. * - [4..6] -> w_add_elt
  58. * - [7..9] -> w_mut_elt
  59. *
  60. * @note Weights are stored a way allowing fast random choice.
  61. * They are kind of ascending threshold until @ref rnd_t max value.
  62. */
  63. rnd_t _weights[10];
  64. };
  65. /**@brief Initialize mutation parameters
  66. *
  67. * pre-process integers threshold by group
  68. * @param params
  69. * @return 0 if no error else -1 and set ERRNO (EINVAL)
  70. */
  71. int rpn_mutation_init_params(rpn_mutation_params_t *params);
  72. /**@brief Mutate a tokenized rpn expression
  73. * @param toks The tokenized expression
  74. * @param params The mutation parameters
  75. * @return 0 or -1 on error */
  76. int rpn_mutation(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  77. /**@brief Mutate an expression by adding a token
  78. * @param toks The tokenized expression
  79. * @param params The mutation parameters
  80. * @return 0 or -1 on error */
  81. int rpn_mutation_add(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  82. /**@brief Mutate an expression by removing a token
  83. * @param toks The tokenized expression
  84. * @param params The mutation parameters
  85. * @return 0 or -1 on error */
  86. int rpn_mutation_del(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  87. /**@brief Mutate an expression by changing a token
  88. * @param toks The tokenized expression
  89. * @param params The mutation parameters
  90. * @return 0 or -1 on error */
  91. int rpn_mutation_mut(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  92. /**@brief Mutate an expression by changing a token value (not type)
  93. * @param toks The tokenized expression
  94. * @param params The mutation parameters
  95. * @return 0 or -1 on error */
  96. int rpn_mutation_mut_soft(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  97. /**@brief Change the "value" of a token randomly not it's type
  98. * @param tok Point on the token to mutate
  99. * @param toks The tokenized expression the token tok belongs to
  100. * @param params The mutation parameters
  101. * @return 0 or -1 on error */
  102. int rpn_mutation_random_token(rpn_token_t *tok,
  103. rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  104. /**@brief Choose a random token type
  105. * @param type Point on result
  106. * @param weights The weight of each types for the choice
  107. * @note Weights comes from @ref rpn_mutation_params_s._weights and are
  108. * processed in a form allowing a fast random choice.
  109. * @return 0 or -1 on error
  110. */
  111. int rpn_random_token_type(rpn_token_type_t *type, rnd_t *weights);
  112. /**@brief Generate a random value
  113. * @param rand Point on result
  114. * @return -1 on error else 0 */
  115. int rpn_getrandom(rnd_t *rand);
  116. /**@brief Generate a random number between 0 and max (max excluded)
  117. * @param max Maximum value
  118. * @param res Will be set to a value between [..max[
  119. * @return -1 on error else 0 */
  120. int rpn_rand_limit(size_t max, size_t *res);
  121. /**@brief Given a size return an random element with regards to given weights
  122. * @param sz The given size
  123. * @param weights Weights coming from @ref rpn_mutation_params_s._weights
  124. * @param res Points on result
  125. * @return 0 or -1 on error */
  126. int _rpn_random_choice(size_t sz, rnd_t *weights, size_t *res);
  127. /**@brief Given a size and a (random) value, returns an element with regards
  128. * to given weights
  129. * @param sz The given size
  130. * @param weights Weights coming from @ref rpn_mutation_params_s._weights
  131. * @param rand A (random) value
  132. * @param res Points on result */
  133. void __rpn_random_choice(size_t sz, rnd_t *weights, rnd_t rand, size_t *res);
  134. /**@brief Debugging function that dump mutation params in a human readable format
  135. * @param fd The file descriptor on wich we should print parameters
  136. * @param params The mutation parameters to dump */
  137. void _print_params(int fd, rpn_mutation_params_t *params);
  138. #endif