Exemplo n.º 1
0
def process_OOD(file_name):
    name = os.path.basename(file_name).split(".")[0]
    print("### " + name)
    print("```")
    model = Model(file_name)
    de = DE(model)
    stat = de.run()
    print("Time Taken : ", stat.runtime)
    stat.tiles()
    data_map = stat.median_spread()
    objs = stat.spit_objectives()
    headers = [obj.__name__.split("_")[-1] for obj in de.settings.obj_funcs]
    cluster_input = [headers] + objs
    print("")
    g = KMeans(k=2)
    clusters = g.run(cluster_input)
    med_spread_plot(data_map, headers, fig_name="img/gen_bin_" + name)
    plot_clusters(clusters,
                  fig_name="img/bin_" + name,
                  col_names=headers,
                  s=50,
                  edgecolors='none')
    print("```")
    print("![1](../../src/img/gen_bin_%s.png)" % name)
    return stat.runtime
Exemplo n.º 2
0
def test_pystar():
    from pystar.models.CSServices import graph
    from pystar.model import Model
    #print(graph.nodes)
    model = Model(graph)
    de = DE(model)
    stat = de.run()
    stat.tiles()
    lastgen = stat.generations[-1]
    maxs = [-1] * len(lastgen[0].objectives)
    bests = [None] * len(lastgen[0].objectives)
    for point in lastgen:
        for i in range(len(maxs)):
            if point.objectives[i] > maxs[i]:
                maxs[i] = point.objectives[i]
                bests[i] = point
    print(maxs)
    columns = ["id", "type", "value", "is_random", "name"]
    stat.tabulate(columns, bests[0])
Exemplo n.º 3
0
def test_pystar():
  from pystar.models.CSServices import graph
  from pystar.model import Model
  #print(graph.nodes)
  model = Model(graph)
  de = DE(model)
  stat = de.run()
  stat.tiles()
  last_gen = stat.generations[-1]
  maxs = [-1]*len(last_gen[0].objectives)
  bests = [None]*len(last_gen[0].objectives)
  for point in last_gen:
    for i in range(len(maxs)):
      if point.objectives[i] > maxs[i]:
        maxs[i] = point.objectives[i]
        bests[i] = point
  print(maxs)
  columns = ["id", "type", "value", "is_random", "name"]
  stat.tabulate(columns, bests[0])
Exemplo n.º 4
0
def process_ood(file_name):
  name = os.path.basename(file_name).split(".")[0]
  print("### " + name)
  print("```")
  model = Model(file_name)
  de = DE(model)
  stat = de.run()
  print("Time Taken : ", stat.runtime)
  stat.tiles()
  data_map = stat.median_spread()
  objs = stat.spit_objectives()
  headers = [obj.__name__.split("_")[-1] for obj in de.settings.obj_funcs]
  cluster_input = [headers] + objs
  print("")
  g = KMeans(k=2)
  clusters = g.run(cluster_input)
  med_spread_plot(data_map, headers, fig_name="img/gen_bin_"+name)
  plot_clusters(clusters,
                fig_name="img/bin_"+name,
                col_names=headers,
                s=50, edgecolors='none')
  print("```")
  print("![1](../../src/img/gen_bin_%s.png)"%name)
  return stat.runtime
Exemplo n.º 5
0
 def __init__(self, model, **settings):
   O.__init__(self)
   self.model = model
   #self.model.bases.extend(self.get_conflicts());self.model.assign_costs()
   self.settings = default().update(**settings)
   self.de = DE(model, gens = self.settings.k1)
Exemplo n.º 6
0
class Star1(O):
  def __init__(self, model, **settings):
    O.__init__(self)
    self.model = model
    #self.model.bases.extend(self.get_conflicts());self.model.assign_costs()
    self.settings = default().update(**settings)
    self.de = DE(model, gens = self.settings.k1)


  def get_conflicts(self):
    model = self.model
    graph = model.get_tree()
    nodes = []
    for node in graph.get_nodes():
      if node.type == "softgoal" and len(node.from_edges) > 1:
        toggle = None
        for edge_id in node.from_edges:
          edge = graph.get_edge(edge_id)
          if edge.type == "contribution":
            temp_toggle = sign(template.Edge.get_contribution_weight(edge.value))
            if toggle is None: toggle = temp_toggle
            if temp_toggle != toggle:
              nodes.append(node)
              break
    return nodes

  def sample(self):
    stat = self.de.run()
    stat.settings.gen_step = self.settings.gen_step
    stat.tiles()
    best = set()
    for obj_index in range(len(self.de.settings.obj_funcs)):
      sorted_pop = sorted(stat.generations[-1], key=lambda x: x.objectives[obj_index], reverse=True)[:len(stat.generations[-1])//5]
      best.update(sorted_pop)
    rest = set()
    for gen in stat.generations:
      for point in gen:
        if not point in best:
          rest.add(point)
    return best, rest

  # def sample(self):
  #   stat = self.de.run()
  #   stat.settings.gen_step = self.settings.gen_step
  #   stat.tiles()
  #   population = set()
  #   for gen in stat.generations:
  #     for point in gen:
  #       population.add(point)
  #   best_size = int(len(population) * self.settings.best_percent/100)
  #   best = sel_nsga2(self.model, list(population), best_size)
  #   rest = population - set(best)
  #   return list(best), list(rest)

  def rank(self, best, rest):
    best_size = len(best)
    rest_size = len(rest)
    p_best = best_size / (best_size + rest_size)
    p_rest = rest_size / (best_size + rest_size)
    decisions = []
    for dec_node in self.model.bases:
      f_best, pos_count, neg_count = 0, 0, 0
      for point in best:
        if point.decisions[dec_node.id] > 0:
          pos_count += 1
        elif point.decisions[dec_node.id] < 0:
          neg_count += 1
      f_pos_best = pos_count / best_size
      l_pos_best = f_pos_best * p_best
      f_neg_best = neg_count / best_size
      l_neg_best = f_neg_best * p_best
      f_pos_rest, f_neg_rest = 0, 0
      for point in rest:
        if point.decisions[dec_node.id] > 0:
          f_pos_rest += 1
        else:
          f_neg_rest += 1
      f_pos_rest /= rest_size
      f_neg_rest /= rest_size
      l_pos_rest = f_pos_rest * p_rest
      l_neg_rest = f_neg_rest * p_rest
      if l_pos_best == 0 and l_pos_rest == 0:
        sup_pos = 0
      else:
        sup_pos = l_pos_best ** 2 / (l_pos_best + l_pos_rest)
      if l_neg_best == 0 and l_neg_rest == 0:
        sup_neg = 0
      else:
        sup_neg = l_neg_best ** 2 / (l_neg_best + l_neg_rest)
      decisions.append(Decision(id = dec_node.id, name = dec_node.name,
                                support=sup_pos, value = 1,
                                type = dec_node.type, container=dec_node.container,
                                cost = self.model.cost_map[dec_node.id]))
      decisions.append(Decision(id = dec_node.id, name = dec_node.name,
                                support=sup_neg, value = -1,
                                type = dec_node.type, container=dec_node.container,
                                cost = self.model.cost_map[dec_node.id]))
    decisions.sort(key=lambda x:x.support, reverse=True)
    sorted_decs = []
    aux = set()
    for dec in decisions:
      if dec.id not in aux:
        sorted_decs.append(dec)
        aux.add(dec.id)
    assert len(sorted_decs) == len(self.model.bases), "Mismatch after sorting support"
    return sorted_decs

  def generate(self, presets = None):
    population = list()
    while len(population) < self.settings.k2:
      point = Point(self.model.generate())
      if not point in population:
        for preset in presets:
          point.decisions[preset.id] = preset.value
        population.append(point)
    return population

  @staticmethod
  def objective_stats(generations):
    stats = []
    obj_len = len(generations[0][0].objectives)
    objective_map={}
    for i in range(obj_len):
      objectives = []
      data_map = {}
      meds = []
      iqrs = []
      for gen, pop in enumerate(generations):
        objs = [pt.objectives[i] for pt in pop]
        objectives.append(objs)
        med, iqr = median_iqr(objs)
        meds.append(med)
        iqrs.append(iqr)
      objective_map[i] = objectives
      data_map["meds"] = meds
      data_map["iqrs"] = iqrs
      stats.append(data_map)
    return stats, objective_map

  def evaluate(self, point, decisions):
    model = self.model
    if not point.objectives:
      model.reset_nodes(point.decisions)
      funcs = [Point.eval_softgoals, Point.eval_goals, Point.eval_paths]
      point.objectives = [func(model) for func in funcs]
      point.objectives.append(sum(decision.cost for decision in decisions if decision.value > 0))
      point._nodes = [node.clone() for node in model.get_tree().get_nodes()]
    return point.objectives

  def prune(self, support):
    gens = []
    for i in range(len(support)):
      decisions = support[:i]
      population = self.generate(decisions)
      for point in population:
        self.evaluate(point, decisions)
        # TODO - Mark Best objective model here
        #self.de.settings.evaluation(point, self.model, self.de.settings.obj_funcs)
      gens.append(population)
    obj_stats, objective_map = self.objective_stats(gens)
    return obj_stats, gens, objective_map

  def report(self, stats, sub_folder, fig_name):
    #headers = [obj.__name__.split("_")[-1] for obj in self.de.settings.obj_funcs]
    headers = ["softgoals", "goals", "costs"]
    med_spread_plot(stats, headers, fig_name="img/"+sub_folder+"/"+fig_name+".png")
    return "img/"+sub_folder+"/"+fig_name+".png"

  @staticmethod
  def get_elbow(gens, index, obj_index=None):
    pop = gens[index]
    pop = sorted(pop, key=lambda x: x.objectives[obj_index])
    point = pop[len(pop)//2]
    return point
Exemplo n.º 7
0
 def __init__(self, model, **settings):
     O.__init__(self)
     self.model = model
     self.model.bases.extend(self.get_conflicts())
     self.settings = default().update(**settings)
     self.de = DE(model, gens=self.settings.k1)
Exemplo n.º 8
0
class Star1(O):
    def __init__(self, model, **settings):
        O.__init__(self)
        self.model = model
        self.model.bases.extend(self.get_conflicts())
        self.settings = default().update(**settings)
        self.de = DE(model, gens=self.settings.k1)

    def get_conflicts(self):
        model = self.model
        graph = model.get_tree()
        nodes = []
        for node in graph.get_nodes():
            if node.type == "softgoal" and len(node.from_edges) > 1:
                toggle = None
                for edge_id in node.from_edges:
                    edge = graph.get_edge(edge_id)
                    if edge.type == "contribution":
                        temp_toggle = sign(
                            template.Edge.get_contribution_weight(edge.value))
                        if toggle is None: toggle = temp_toggle
                        if temp_toggle != toggle:
                            nodes.append(node)
                            break
        return nodes

    def sample(self):
        stat = self.de.run()
        stat.tiles()
        best = set()
        for obj_index in range(len(self.de.settings.obj_funcs)):
            sorted_pop = sorted(stat.generations[-1],
                                key=lambda x: x.objectives[obj_index],
                                reverse=True)[:len(stat.generations[-1]) // 5]
            best.update(sorted_pop)
        rest = set()
        for gen in stat.generations:
            for point in gen:
                if not point in best:
                    rest.add(point)
        return best, rest

    def rank(self, best, rest):
        best_size = len(best)
        rest_size = len(rest)
        p_best = best_size / (best_size + rest_size)
        p_rest = rest_size / (best_size + rest_size)
        sup_map = {}
        for dec_node in self.model.bases:
            f_best = 0
            for point in best:
                if point.decisions[dec_node.id] > 0:
                    f_best += 1
            f_best /= best_size
            l_best = f_best * p_best
            f_rest = 0
            for point in rest:
                if point.decisions[dec_node.id] > 0:
                    f_rest += 1
            f_rest /= best_size
            l_rest = f_rest * p_rest
            sup_best = l_best**2 / (l_best + l_rest)
            sup_map[dec_node.id] = sup_best
        return sup_map

    def generate(self, presets=None):
        population = list()
        while len(population) < self.de.settings.candidates:
            point = Point(self.model.generate())
            if not point in population:
                population.append(point)
        for point in population:
            for preset in presets:
                point.decisions[preset] = 1
        return population

    def objective_stats(self, generations):
        stats = []
        for i in range(len(self.de.settings.obj_funcs)):
            data_map = {}
            meds = []
            iqrs = []
            for gen, pop in enumerate(generations):
                objs = [pt.objectives[i] for pt in pop]
                med, iqr = median_iqr(objs)
                meds.append(med)
                iqrs.append(iqr)
            data_map["meds"] = meds
            data_map["iqrs"] = iqrs
            stats.append(data_map)
        return stats

    def prune(self, support):
        gens = []
        for i in range(len(support)):
            sup_keys = support[:i]
            population = self.generate(sup_keys)
            for point in population:
                self.de.settings.evaluation(point, self.model,
                                            self.de.settings.obj_funcs)
            gens.append(population)
        obj_stats = self.objective_stats(gens)
        return obj_stats

    def report(self, stats):
        headers = [
            obj.__name__.split("_")[-1] for obj in self.de.settings.obj_funcs
        ]
        med_spread_plot(stats,
                        headers,
                        fig_name="img/costs/" + self.model.get_tree().name +
                        ".png")
Exemplo n.º 9
0
class Star1(O):
  def __init__(self, model, **settings):
    O.__init__(self)
    self.model = model
    #self.model.bases.extend(self.get_conflicts());self.model.assign_costs()
    self.settings = default().update(**settings)
    self.de = DE(model, gens = self.settings.k1)


  def get_conflicts(self):
    model = self.model
    graph = model.get_tree()
    nodes = []
    for node in graph.get_nodes():
      if node.type == "softgoal" and len(node.from_edges) > 1:
        toggle = None
        for edge_id in node.from_edges:
          edge = graph.get_edge(edge_id)
          if edge.type == "contribution":
            temp_toggle = sign(template.Edge.get_contribution_weight(edge.value))
            if toggle is None: toggle = temp_toggle
            if temp_toggle != toggle:
              nodes.append(node)
              break
    return nodes

  def sample(self):
    stat = self.de.run()
    stat.settings.gen_step = self.settings.gen_step
    stat.tiles()
    best = set()
    for obj_index in range(len(self.de.settings.obj_funcs)):
      sorted_pop = sorted(stat.generations[-1], key=lambda x: x.objectives[obj_index], reverse=True)[:len(stat.generations[-1])//5]
      best.update(sorted_pop)
    rest = set()
    for gen in stat.generations:
      for point in gen:
        if not point in best:
          rest.add(point)
    return best, rest

  # def sample(self):
  #   stat = self.de.run()
  #   stat.settings.gen_step = self.settings.gen_step
  #   stat.tiles()
  #   population = set()
  #   for gen in stat.generations:
  #     for point in gen:
  #       population.add(point)
  #   best_size = int(len(population) * self.settings.best_percent/100)
  #   best = sel_nsga2(self.model, list(population), best_size)
  #   rest = population - set(best)
  #   return list(best), list(rest)

  def rank(self, best, rest):
    best_size = len(best)
    rest_size = len(rest)
    p_best = best_size / (best_size + rest_size)
    p_rest = rest_size / (best_size + rest_size)
    decisions = []
    for dec_node in self.model.bases:
      f_best, pos_count, neg_count = 0, 0, 0
      for point in best:
        if point.decisions[dec_node.id] > 0:
          pos_count += 1
        elif point.decisions[dec_node.id] < 0:
          neg_count += 1
      f_pos_best = pos_count / best_size
      l_pos_best = f_pos_best * p_best
      f_neg_best = neg_count / best_size
      l_neg_best = f_neg_best * p_best
      f_pos_rest, f_neg_rest = 0, 0
      for point in rest:
        if point.decisions[dec_node.id] > 0:
          f_pos_rest += 1
        else:
          f_neg_rest += 1
      f_pos_rest /= rest_size
      f_neg_rest /= rest_size
      l_pos_rest = f_pos_rest * p_rest
      l_neg_rest = f_neg_rest * p_rest
      if l_pos_best == 0 and l_pos_rest == 0:
        sup_pos = 0
      else:
        sup_pos = l_pos_best ** 2 / (l_pos_best + l_pos_rest)
      if l_neg_best == 0 and l_neg_rest == 0:
        sup_neg = 0
      else:
        sup_neg = l_neg_best ** 2 / (l_neg_best + l_neg_rest)
      decisions.append(Decision(id = dec_node.id, name = dec_node.name,
                                support=sup_pos, value = 1,
                                type = dec_node.type, container=dec_node.container,
                                cost = self.model.cost_map[dec_node.id]))
      decisions.append(Decision(id = dec_node.id, name = dec_node.name,
                                support=sup_neg, value = -1,
                                type = dec_node.type, container=dec_node.container,
                                cost = self.model.cost_map[dec_node.id]))
    decisions.sort(key=lambda x:x.support, reverse=True)
    sorted_decs = []
    aux = set()
    for dec in decisions:
      if dec.id not in aux:
        sorted_decs.append(dec)
        aux.add(dec.id)
    assert len(sorted_decs) == len(self.model.bases), "Mismatch after sorting support"
    return sorted_decs

  def generate(self, presets = None):
    population = list()
    while len(population) < self.settings.k2:
      point = Point(self.model.generate())
      if not point in population:
        for preset in presets:
          point.decisions[preset.id] = preset.value
        population.append(point)
    return population

  @staticmethod
  def objective_stats(generations):
    stats = []
    obj_len = len(generations[0][0].objectives)
    objective_map={}
    for i in range(obj_len):
      objectives = []
      data_map = {}
      meds = []
      iqrs = []
      for gen, pop in enumerate(generations):
        objs = [pt.objectives[i] for pt in pop]
        objectives.append(objs)
        med, iqr = median_iqr(objs)
        meds.append(med)
        iqrs.append(iqr)
      objective_map[i] = objectives
      data_map["meds"] = meds
      data_map["iqrs"] = iqrs
      stats.append(data_map)
    return stats, objective_map

  def evaluate(self, point, decisions):
    model = self.model
    if not point.objectives:
      model.reset_nodes(point.decisions)
      funcs = [Point.eval_softgoals, Point.eval_goals, Point.eval_paths]
      point.objectives = [func(model) for func in funcs]
      point.objectives.append(sum(decision.cost for decision in decisions if decision.value > 0))
      point._nodes = [node.clone() for node in model.get_tree().get_nodes()]
    return point.objectives

  def prune(self, support):
    gens = []
    for i in range(len(support)):
      decisions = support[:i]
      population = self.generate(decisions)
      for point in population:
        self.evaluate(point, decisions)
        # TODO - Mark Best objective model here
        #self.de.settings.evaluation(point, self.model, self.de.settings.obj_funcs)
      gens.append(population)
    obj_stats, objective_map = self.objective_stats(gens)
    return obj_stats, gens, objective_map

  def report(self, stats, sub_folder, fig_name):
    #headers = [obj.__name__.split("_")[-1] for obj in self.de.settings.obj_funcs]
    headers = ["softgoals", "goals", "costs"]
    med_spread_plot(stats, headers, fig_name="img/"+sub_folder+"/"+fig_name+".png")
    return "img/"+sub_folder+"/"+fig_name+".png"

  @staticmethod
  def get_elbow(gens, index, obj_index=None):
    pop = gens[index]
    pop = sorted(pop, key=lambda x: x.objectives[obj_index])
    point = pop[len(pop)//2]
    return point