0

In a few words, how do I:

  • go from double to bits,
  • then do the crossover (one point, two point),
  • and go back to double

I can develop the roulette wheel selection. What I don't see clearly is how mixing two doubles might give me a "better" double. Is that completely random? If the "fittest" of my doubles and my "weakest" one combine, won't they produce a mid-point double?

Elaboration: Shortest distance from a point to this curve

EDIT 1: Without slowing down the program too much.

EDIT 2: I considered using a byte[], but I don't know if that would go against the genetic algorithm part.

Community
  • 1
  • 1
OFRBG
  • 1,653
  • 14
  • 28
  • I think you need to set the scene a little more - start by describing your problem and solution. What is your chromosome and what does it represent? – Ant P Jan 27 '13 at 22:50
  • Mixing bits of two doubles could produce a child double that is WAY different than either of the two parents. – omittones Jan 27 '13 at 22:53
  • If you have a significant precision beyond which you dontcare, then you could multiply to an integer, then crossover/mutate that then return to float. – NWS Jan 28 '13 at 09:10

3 Answers3

1

What would be best here really does depend on what the double "means" and how large a portion of the genome each double represents. Often, the best approach would just be to treat the doubles as discrete and to cross entire doubles at a time.

So, if your parents are {0.1 0.2 0.3 0.4 0.5} and {0.6 0.7 0.8 0.9 1.0} then they might produce (examples):

  • {0.1 0.6 0.3 0.4 0.5} under uniform selection (select each gene from a random parent)
  • {0.1 0.2 0.3 0.9 1.0} under single-point crossover (take the first n genes from one parent and the remainder from the other, where n is a random number
  • {0.1 0.7 0.8 0.4 0.5} under two-point crossover (choose two random numbers and insert the genes between them from one parent into the other)
Joel Rein
  • 3,608
  • 1
  • 26
  • 32
  • Afaik i know, you remove the weakest ones. What happens if those are the only ones that contain 0.1 or 0.2? they would never be able to get back into the gene pool then right? (which might be bad, as those might be perfect for one of the genes, which is not discovered yet) Unless you hope that a mutation reintroduces them, but that doesnt seem very reliable to me. – Viezevingertjes Feb 28 '19 at 11:21
  • 1
    @Viezevingertjes Yes, strong selection, especially early in the process before the GA has had time to explore the space is problematic. You want to use methods of selection which allow diversity to survive, at least long enough to explore the terrain around them to be explored. This is one of the advantages of, for example, tournament selection - it allows selection to be quite weak (by using a small tournament size), and can also easily be tuned (e.g. ramping up the tournament size as a run continues to increase selection pressure). – Joel Rein Feb 28 '19 at 14:44
1

This worked for me:

BitArray BAA1 = new BitArray(BitConverter.GetBytes(a1));
BitArray BAA2 = new BitArray(BitConverter.GetBytes(a2));

    for (int i = r.Next(0, 64); i > 0; i--)
            {
                temp = BAA1.Get(i);
                temp2 = BAA2.Get(i);

                BAA1.Set(i, temp2);
                BAA2.Set(i, temp);


                temp = BAB1.Get(i);
                temp2 = BAB2.Get(i);

                BAB1.Set(i, temp2);
                BAB2.Set(i, temp);
            }

        byte[] tempbytes = new byte[BAA1.Length];

        BAA1.CopyTo(tempbytes, 0);
        double baa1 = BitConverter.ToDouble(tempbytes, 0);

        BAA2.CopyTo(tempbytes, 0);
        double baa2 = BitConverter.ToDouble(tempbytes, 0);

baa1 and baa2 are the end products of the cross.

OFRBG
  • 1,653
  • 14
  • 28
-2

I think it might be better to perform slightly different crossower of doubles, eg. if you have 1.0 and 2.0, why not:

  1. take the middle, (1.0 + 2.0) / 2 = 1.5
  2. add a small mutation, 1.5 * random(0.9, 1.0) = 1.57

Mixing bits of two doubles could produce a child double that is WAY different than either of the two parents.

omittones
  • 847
  • 1
  • 10
  • 23
  • Are you sure that in essence, the program does not require the use of some kind of *genetic* crossover? – OFRBG Jan 27 '13 at 22:58
  • Well, this IS some kind of genetic crossover. If your fitness is good on both 1.0 and 2.0, it stands to reason that fitness peak is also in the vicinity of 1.0 and 2.0. – omittones Jan 27 '13 at 23:01
  • To make it even more _crossoverish_, you can do this: *rnd = random(0.0, 1.0); new_value = parent1 * rnd + parent2 * (1.0 - rnd)*, this way the child will lie somewhere between two parents. You can increase random span to add the possibility of child exiting parent interval. – omittones Jan 27 '13 at 23:04
  • 2
    You've got to be *very, very* careful with anything like this. This is called "blending inheritance" and it does not work - it quite quickly wipes out any diversity in the population, which is quite bad. – Joel Rein Jan 31 '13 at 12:22
  • I think this would just result in everything being averaged out right? – Viezevingertjes Feb 28 '19 at 11:25