Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cstring>
  4.  
  5. #define _USE_MATH_DEFINES
  6.  
  7. using namespace std;
  8.  
  9. float float_rand(float min, float max)
  10. {
  11.  
  12. float scale = rand() / (float)RAND_MAX; /* [0, 1.0] */
  13. return min + scale * (max - min); /* [min, max] */
  14. }
  15.  
  16. float * return_solution_list(int number_of_solution, float min_value, float max_value){
  17. float* solutions = static_cast<float *>(malloc(sizeof(float) * number_of_solution));
  18. for (int i = 0; i < number_of_solution; ++i) {
  19. solutions[i] = float_rand(min_value,max_value);
  20. }
  21. return solutions;
  22. }
  23.  
  24. float rastigin_function(float* solution, int dimension){
  25. float response = 0.00;
  26. response = 10*dimension;
  27. for (int i = 0; i <dimension ; ++i) {
  28. response += solution[i]*solution[i] - 10*cos(M_PI*2*solution[i]);
  29. }
  30. return response;
  31. }
  32.  
  33. int main() {
  34. float m,M;
  35. int solution;
  36. cout<< "enter number of solution: ";
  37. cin >> solution;
  38. cout<< "enter minim value: ";
  39. cin >> m;
  40. cout<< "enter maxim value: ";
  41. cin >> M;
  42. float * sol = return_solution_list(solution,m,M);
  43. cout<< "Soul: " << rastigin_function(sol, solution);
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement