Problem

How many schemata survive

randomized estimation bit tricks counting string matching

A genetic algorithm keeps a population of n strings of length l of zeros and ones; string i has fitness fi. A new generation is produced as in the schema theorem: selection proportional to fitness, one-point crossover of pairs with probability pc (the cut at one of l − 1 places), mutation of each bit with probability pm.

A researcher wants to know which schemata the population will surely not lose. A schema is a string of length l of the symbols 0, 1 and *; a string matches the schema if it agrees with it at all positions where the schema has no asterisk. For each schema H compute the schema theorem’s lower bound for the mean number of matching strings in the next generation:

$$m(H)\cdot\frac{f(H)}{\bar f}\cdot\left(1 - p_c\,\frac{\delta(H)}{l - 1}\right)\cdot(1 - p_m)^{o(H)}.$$

Here m(H) is the number of matching strings, f(H) their mean fitness, f̄ the mean fitness of the whole population, o(H) the number of positions of the schema without an asterisk, δ(H) the distance between the first and last such positions (0 if there are fewer than two). If no strings match, the bound is 0. With l = 1 there is nowhere to cross, and the crossover factor is 1.

Input

The first line holds n, l, q (1 ≤ n ≤ 1000, 1 ≤ l ≤ 64, 1 ≤ q ≤ 1000). The second holds pc and pm (from 0 to 1). Then come n lines: a string of the population and its fitness, an integer from 1 to 106. Then q schemata, one per line.

Output

For each schema, the bound, each on a new line. The answer is accepted if the error is at most 10−6 (relative for numbers greater than one).

Examples

Example 1

Input
4 4 4
0.7 0.01
1010 5
1110 3
0011 2
1000 2
1**0
*11*
****
0***
Output
0.980100000
0.751410000
4.000000000
0.660000000

Example 2

Input
3 1 3
0.5 0.1
1 4
0 1
1 2
1
0
*
Output
2.314285714
0.385714286
3.000000000

Limits

Time · Python
6000 ms
Time · Go
1500 ms
Memory
256 MB
Judging
8 tests

Walkthrough

Open it step by step: each step gives away a little more. You can jump straight to the solution, but a problem solved with one hint stays with you longer.

1Hint 1

Notice that m(H)·f(H) is simply the total fitness of the matching strings, and there is no need to divide by m(H) only to multiply by it again. What remains is to find the matching strings quickly.

2Hint 2

A string of length up to 64 fits into one 64-bit number. How can you check with one operation whether a number agrees with it at all positions where the schema has no asterisk?

3The idea

For a schema build two masks: care, ones where the schema fixes a bit, and want, the fixed bits themselves (zeros in place of the asterisks). A string x matches when x & care == want: the AND with the mask clears the positions under asterisks, and the rest must agree. The theorem’s bound: the total fitness of the matching strings, divided by the mean fitness, multiplied by 1 − pc·δ/(l − 1) and by (1 − pm)o.

Why the denominator is l − 1 and not l: a string of length l has only l − 1 places between neighbouring bits where a cut can go, and a schema with outermost fixed positions i < j is destroyed by a cut in one of the j − i = δ gaps between them.

4Walkthrough

Turn the population’s strings into numbers, add up all the fitnesses, build the masks, order and length for each schema and go over the population. In the fourth and fifth tests the population is a real run of a genetic algorithm on the Royal Road from the article after 30 and 120 generations. The schemata there are blocks of eight ones and pairs of blocks. A pair of distant blocks has a length δ of almost 64, and crossover with probability 0.7 almost certainly cuts it: the theorem’s bound for it is small even if there are many strings with both blocks. This is exactly what the theorem warns about: crossover destroys long schemata.

4+Solution
5Complexity

O(n·l) to read the population and O(l + n) per schema: O((n + q)·l + n·q) in all. Comparing each string with each schema character by character would cost O(n·q·l), 64 times more.

6Common mistakes
  • l instead of l − 1 in the share of destroyed schemata. On the first example it gives 1.552 instead of 0.980 for the schema 1**0.
  • Division by zero with l = 1. There is nowhere to cut a one-bit string, and the crossover factor is 1: the second example.
  • (1 − pm·o) instead of (1 − pm)o. These are the first two terms of the expansion, not the probability itself; in the seventh test pm = 1, and the difference is huge.
  • The length of a schema with one fixed position. It is zero, not one: there is nothing to cut.

Algorithms behind it