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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 RPN expression 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 maximum value for rnd_t */
  35. extern const rnd_t rnd_t_max;
  36. /**@brief Default values for mutations parameters */
  37. extern rpn_mutation_params_t rpn_mutation_params_default;
  38. /**@brief RPN expression mutation parameters */
  39. struct rpn_mutation_params_s
  40. {
  41. /**@brief Minimum expression length */
  42. size_t min_len;
  43. /**@brief Weight of adding a token*/
  44. float w_add;
  45. /**@brief Weight of deleting a token*/
  46. float w_del;
  47. /**@brief Weight of mutating a token*/
  48. float w_mut;
  49. /**@brief Weight of mutating a token without type change*/
  50. float w_mut_soft;
  51. /**@brief Weight for each token types (op, const, var) when
  52. * adding a token*/
  53. float w_add_elt[3];
  54. /**@brief Weight for each token types (op, const, var) when
  55. * mutating a token*/
  56. float w_mut_elt[3];
  57. /**@brief For internal use, set by rpn_mutation_init_params
  58. *
  59. * weight reported by groups on rnd_t integers
  60. * - [0..3] -> weights mutation
  61. * - [4..6] -> w_add_elt
  62. * - [7..9] -> w_mut_elt
  63. *
  64. * @note Weights are stored a way allowing fast random choice.
  65. * They are kind of ascending threshold until @ref rnd_t max value.
  66. */
  67. rnd_t _weights[10];
  68. };
  69. /**@brief Initialize mutation parameters
  70. *
  71. * pre-process integers threshold by group
  72. * @param params
  73. * @return 0 if no error else -1 and set ERRNO (EINVAL)
  74. */
  75. int rpn_mutation_init_params(rpn_mutation_params_t *params);
  76. /**@brief Mutate a tokenized rpn expression
  77. * @param toks The tokenized expression
  78. * @param params The mutation parameters
  79. * @return 0 or -1 on error */
  80. int rpn_mutation(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  81. /**@brief Mutate an expression by adding a token
  82. * @param toks The tokenized expression
  83. * @param params The mutation parameters
  84. * @return 0 or -1 on error */
  85. int rpn_mutation_add(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  86. /**@brief Mutate an expression by removing a token
  87. * @param toks The tokenized expression
  88. * @param params The mutation parameters
  89. * @return 0 or -1 on error */
  90. int rpn_mutation_del(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  91. /**@brief Mutate an expression by changing a token
  92. * @param toks The tokenized expression
  93. * @param params The mutation parameters
  94. * @return 0 or -1 on error */
  95. int rpn_mutation_mut(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  96. /**@brief Mutate an expression by changing a token value (not type)
  97. * @param toks The tokenized expression
  98. * @param params The mutation parameters
  99. * @return 0 or -1 on error */
  100. int rpn_mutation_mut_soft(rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  101. /**@brief Change the "value" of a token randomly not it's type
  102. * @param tok Point on the token to mutate
  103. * @param toks The tokenized expression the token tok belongs to
  104. * @param params The mutation parameters
  105. * @return 0 or -1 on error */
  106. int rpn_mutation_random_token(rpn_token_t *tok,
  107. rpn_tokenized_t *toks, rpn_mutation_params_t *params);
  108. /**@brief Choose a random token type
  109. * @param type Point on result
  110. * @param weights The weight of each types for the choice
  111. * @note Weights comes from @ref rpn_mutation_params_s._weights and are
  112. * processed in a form allowing a fast random choice.
  113. * @return 0 or -1 on error
  114. */
  115. int rpn_random_token_type(rpn_token_type_t *type, rnd_t *weights);
  116. /**@brief Generate a random value
  117. * @param rand Point on result
  118. * @return -1 on error else 0 */
  119. int rpn_getrandom(rnd_t *rand);
  120. /**@brief Generate a random number between 0 and max (max excluded)
  121. * @param max Maximum value
  122. * @param res Will be set to a value between [..max[
  123. * @return -1 on error else 0 */
  124. int rpn_rand_limit(size_t max, size_t *res);
  125. /**@brief Conver an array of float weights to an array for fast random
  126. * choices (see @ref _rpn_random_choices() )
  127. * @param sz The number of items in to choose from
  128. * @param weights Array of size sz with items weights (positive values)
  129. * @param fast_weights Array of size sz pointing on result
  130. * @return -1 on error else 0 */
  131. int rpnifs_fast_rnd_weights(size_t sz, const float *weights,
  132. rnd_t *fast_weights);
  133. /**
  134. * @return A float between [-range[1] .. -range[0]] or [range[0] .. range[1]]
  135. */
  136. int rpnifs_rnd_float(float range[2], float *res);
  137. /**@brief Given a size return an random element with regards to given weights
  138. * @param sz The given size
  139. * @param weights Weights coming from @ref rpn_mutation_params_s._weights
  140. * @param res Points on result
  141. * @return 0 or -1 on error
  142. * @todo refactor rename without rpn prefix */
  143. int _rpn_random_choice(size_t sz, rnd_t *weights, size_t *res);
  144. /**@brief Given a size and a (random) value, returns an element with regards
  145. * to given weights
  146. * @param sz The given size
  147. * @param weights Weights coming from @ref rpn_mutation_params_s._weights
  148. * @param rand A (random) value
  149. * @param res Points on result */
  150. void __rpn_random_choice(size_t sz, rnd_t *weights, rnd_t rand, size_t *res);
  151. /**@brief Debugging function that dump mutation params in a human readable format
  152. * @param fd The file descriptor on wich we should print parameters
  153. * @param params The mutation parameters to dump */
  154. void _print_params(int fd, rpn_mutation_params_t *params);
  155. #endif