-4

i got stuck when i was trying to convert my code to MPI and Crossover solution. My problem is about the Crossover is too hard for me to understand and more difficult to implement all of those solution to MPI. If anyone could give me hint, or example, or any related document. I will included my code below for everyone to see.

Thank you very much

#include <string> 
#include <cstdlib> 
#include <iostream> 
#include <cassert> 
#include <algorithm> 
#include <vector> 

std::string allowed_chars = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

class selection 
{ 
public:
  static int fitness(std::string candidate) 
  { 
    assert(target.length() == candidate.length()); 

    int fitness_so_far = 0; 

    for (int i = 0; i < target.length(); ++i) 
    { 
      int target_pos = allowed_chars.find(target[i]); 
      int candidate_pos = allowed_chars.find(candidate[i]); 
      int diff = std::abs(target_pos - candidate_pos); 
      fitness_so_far -= std::min(diff, int(allowed_chars.length()) - diff); 
    } 

    return fitness_so_far; 
  } 

  // get the target string length 
  static int target_length() { return target.length(); } 
private: 
  static std::string target; 
}; 

std::string selection::target = "METHINKS IT IS LIKE A WEASEL";

void move_char(char& c, int distance) 
{ 
  while (distance < 0) 
    distance += allowed_chars.length(); 
  int char_pos = allowed_chars.find(c); 
  c = allowed_chars[(char_pos + distance) % allowed_chars.length()]; 
} 

std::string mutate(std::string parent, double mutation_rate) 
{ 
  for (int i = 0; i < parent.length(); ++i) 
    if (std::rand()/(RAND_MAX + 1.0) < mutation_rate) 
    { 
      int distance = std::rand() % 3 + 1; 
      if(std::rand()%2 == 0) 
 move_char(parent[i], distance); 
      else 
        move_char(parent[i], -distance); 
    } 
  return parent; 
}
bool less_fit(std::string const& s1, std::string const& s2) 
{ 
  return selection::fitness(s1) < selection::fitness(s2); 
} 

int main() 
{ 
  int const C = 100; 

  std::srand(time(0)); 

  std::string parent; 
  for (int i = 0; i < selection::target_length(); ++i) 
  { 
    parent += allowed_chars[std::rand() % allowed_chars.length()]; 
  } 

  int const initial_fitness = selection::fitness(parent); 

  for(int fitness = initial_fitness; 
      fitness < 0; 
      fitness = selection::fitness(parent)) 
  { 
    std::cout << parent << ": " << fitness << "\n"; 
    double const mutation_rate = 0.02 + (0.9*fitness)/initial_fitness; 
    typedef std::vector<std::string> childvec; 
    childvec childs; 
    childs.reserve(C+1); 

    childs.push_back(parent); 
    for (int i = 0; i < C; ++i) 
      childs.push_back(mutate(parent, mutation_rate)); 

    parent = *std::max_element(childs.begin(), childs.end(), less_fit); 
  } 
  std::cout << "final string: " << parent << "\n"; 
} 
manlio
  • 18,345
  • 14
  • 76
  • 126
  • 2
    Please, describe what the presented code should do and what is going wrong. At the moment your question is not much like a question :) – werediver May 19 '16 at 13:03

1 Answers1

0

For cross over select 2 parents strings and associated split index(es),

then generate 2 new string:

std::string new_string1 = s1.substring(0, split_index1) + s2.substring(split_index2);
std::string new_string2 = s2.substring(0, split_index2) + s1.substring(split_index1);`.
Jarod42
  • 203,559
  • 14
  • 181
  • 302