Пример #1
0
    def ReproduceWith(self, other, specs, mutation_rate):
        """Reproduce with other FlagSet.

    Args:
      other: A FlagSet to reproduce with.
      specs: A list of spec from which the flag set is created.
      mutation_rate: one in mutation_rate flags will be mutated (replaced by a
        random version of the same flag, instead of one from either of the
        parents).  Set to 0 to disable mutation.

    Returns:
      A GA task made by mixing self with other.
    """

        # Get the flag dictionary.
        father_flags = self.GetFlags().GetFlags()
        mother_flags = other.GetFlags().GetFlags()

        # Flags that are common in both parents and flags that belong to only one
        # parent.
        self_flags = []
        other_flags = []
        common_flags = []

        # Find out flags that are common to both parent and flags that belong soly
        # to one parent.
        for self_flag in father_flags:
            if self_flag in mother_flags:
                common_flags.append(self_flag)
            else:
                self_flags.append(self_flag)

        for other_flag in mother_flags:
            if other_flag not in father_flags:
                other_flags.append(other_flag)

        # Randomly select flags that belong to only one parent.
        output_flags = [
            father_flags[f] for f in self_flags if random.randint(0, 1)
        ]
        others = [mother_flags[f] for f in other_flags if random.randint(0, 1)]
        output_flags.extend(others)
        # Turn on flags that belong to both parent. Randomly choose the value of the
        # flag from either parent.
        for flag in common_flags:
            output_flags.append(
                CrossoverWith(father_flags[flag], mother_flags[flag]))

        # Mutate flags
        if mutation_rate:
            return RandomMutate(specs, FlagSet(output_flags), mutation_rate)

        return GATask(FlagSet(output_flags))
def _GenerateAllFlagsTasks(specs):
    """Generate a task that all the flags are enable.

  All the boolean flags in the specs will be enabled and all the numeric flag
  with have the largest legal value.

  Args:
    specs: A list of spec from which the flag set is created.

  Returns:
    A set containing a task that has all flags enabled.
  """

    flag_set = []

    for spec in specs:
        numeric_flag_match = flags.Search(spec)

        if numeric_flag_match:
            value = (int(numeric_flag_match.group('end')) - 1)
        else:
            value = -1
        flag_set.append(Flag(spec, value))

    return set([Task(FlagSet(flag_set))])
def _GenerateRandomRasks(specs):
    """Generate a task that has random values.

  Args:
    specs: A list of spec from which the flag set is created.

  Returns:
    A set containing a task that has random values.
  """

    flag_set = []

    for spec in specs:
        numeric_flag_match = flags.Search(spec)
        if numeric_flag_match:
            # Numeric flags.
            start = int(numeric_flag_match.group('start'))
            end = int(numeric_flag_match.group('end'))

            value = random.randint(start - 1, end - 1)
            if value != start - 1:
                # If the value falls in the range, this flag is enabled.
                flag_set.append(Flag(spec, value))
        else:
            # Boolean flags.
            if random.randint(0, 1):
                flag_set.append(Flag(spec))

    return set([Task(FlagSet(flag_set))])
def GenerateRandomGATasks(specs, num_tasks, num_trials):
    """Generate a set of tasks for the Genetic Algorithm.

  Args:
    specs: A list of spec from which the flag set is created.
    num_tasks: number of tasks that should be generated.
    num_trials: the maximum number of tries should be attempted to generate the
      set of tasks.

  Returns:
    A set of randomly generated tasks.
  """

    tasks = set([])

    total_trials = 0
    while len(tasks) < num_tasks and total_trials < num_trials:
        new_flag = FlagSet(
            [Flag(spec) for spec in specs if random.randint(0, 1)])
        new_task = GATask(new_flag)

        if new_task in tasks:
            total_trials += 1
        else:
            tasks.add(new_task)
            total_trials = 0

    return tasks
    def Next(self, cache):
        """Calculate the next generation.

    The best neighbor b of the current task is the parent of the next
    generation. The neighbors of b will be the set of tasks to be evaluated
    next.

    Args:
      cache: A set of tasks that have been generated before.

    Returns:
      A set of new generations.
    """

        # The best neighbor.
        current_task = self._next_task
        flag_set = current_task.GetFlags()

        # The neighbors of the best neighbor.
        children_tasks = set([])
        for spec in self._specs:
            for next_flag in flags_util.ClimbNext(flag_set.GetFlags(), spec):
                new_task = Task(FlagSet(next_flag.values()))

                if new_task not in cache:
                    children_tasks.add(new_task)

        return [
            HillClimbingBestBranch(children_tasks, set([current_task]),
                                   self._specs)
        ]
  def testFormattedForUse(self):
    """Test the FormattedForUse method of the Class FlagSet.

    The output should be a sorted list of strings.
    """

    flag_names = range(NUM_TESTS)
    flag_names.reverse()
    flags = []
    result = []

    # Construct the flag set.
    for flag_name in flag_names:
      spec = '%s' % flag_name
      flags.append(Flag(spec))
      result.append(spec)

    flag_set = FlagSet(flags)

    # The results string should be sorted.
    assert sorted(result) == flag_set.FormattedForUse()
  def testEqual(self):
    """Test the equal method of the Class FlagSet.

    Two FlagSet instances are equal if all their flags are equal.
    """

    flag_names = range(NUM_TESTS)

    # Two flag sets having the same flags should be equivalent.
    for flag_name in flag_names:
      spec = '%s' % flag_name

      assert FlagSet([Flag(spec)]) == FlagSet([Flag(spec)])

    # Two flag sets having different flags should be different.
    for flag_name in flag_names:
      spec = '%s' % flag_name
      flag_set = FlagSet([Flag(spec)])
      other_flag_sets = [other for other in flag_names if flag_name != other]
      for other_name in other_flag_sets:
        other_spec = '%s' % other_name
        assert flag_set != FlagSet([Flag(other_spec)])
Пример #8
0
def RandomMutate(specs, flag_set, mutation_rate):
    """Randomly mutate the content of a task.

  Args:
    specs: A list of spec from which the flag set is created.
    flag_set: The current flag set being mutated
    mutation_rate: What fraction of genes to mutate.

  Returns:
    A Genetic Task constructed by randomly mutating the input flag set.
  """

    results_flags = []

    for spec in specs:
        # Randomly choose whether this flag should be mutated.
        if random.randint(0, int(1 / mutation_rate)):
            continue

        # If the flag is not already in the flag set, it is added.
        if spec not in flag_set:
            results_flags.append(Flag(spec))
            continue

        # If the flag is already in the flag set, it is mutated.
        numeric_flag_match = flags.Search(spec)

        # The value of a numeric flag will be changed, and a boolean flag will be
        # dropped.
        if not numeric_flag_match:
            continue

        value = flag_set[spec].GetValue()

        # Randomly select a nearby value of the current value of the flag.
        rand_arr = [value]
        if value + 1 < int(numeric_flag_match.group('end')):
            rand_arr.append(value + 1)

        rand_arr.append(value - 1)
        value = random.sample(rand_arr, 1)[0]

        # If the value is smaller than the start of the spec, this flag will be
        # dropped.
        if value != int(numeric_flag_match.group('start')) - 1:
            results_flags.append(Flag(spec, value))

    return GATask(FlagSet(results_flags))
  def testContain(self):
    """Test the contain method of the Class FlagSet.

    The flag set is also indexed by the specs. The flag set should return true
    for spec if it contains a flag containing spec.
    """

    true_tests = range(NUM_TESTS)
    false_tests = range(NUM_TESTS, NUM_TESTS * 2)

    specs = [str(spec) for spec in true_tests]

    flag_set = FlagSet([Flag(spec) for spec in specs])

    for spec in specs:
      assert spec in flag_set

    for spec in false_tests:
      assert spec not in flag_set
  def testGetItem(self):
    """Test the get item method of the Class FlagSet.

    The flag set is also indexed by the specs. The flag set should return the
    appropriate flag given the spec.
    """

    tests = range(NUM_TESTS)

    specs = [str(spec) for spec in tests]
    flag_array = [Flag(spec) for spec in specs]

    flag_set = FlagSet(flag_array)

    # Created a dictionary of spec and flag, the flag set should return the flag
    # the same as this dictionary.
    spec_flag = dict(zip(specs, flag_array))

    for spec in spec_flag:
      assert flag_set[spec] == spec_flag[spec]
def _GenerateNoFlagTask():
    return set([Task(FlagSet([]))])