Exemplo n.º 1
0
def build_NN_pref_graph(domain, learner, device=None):
    p_graph = PreferenceGraph(domain)
    equals = []
    for pair in domain.each_pair():
        inp = prepare_pair(pair)
        if device is not None:
            inp = inp.to(device)
        label = learner.forward_squash(inp)
        label = Relation.parse_label(label)
        if label == Relation.strict_preference():
            p_graph.arc(pair[0], pair[1])
        elif label == Relation.strict_dispreference():
            p_graph.arc(pair[1], pair[0])
        elif label == Relation.equal():
            found = False
            for item in equals:
                for entry in item:
                    if entry == pair[0] or entry == pair[1]:
                        found = True
                        item.append(pair[0])
                        item.append(pair[1])
                        break
                if found:
                    break
            if not found:
                equals.append([pair[0], pair[1]])
    for eq_set in equals:
        p_graph.share_arcs(eq_set)
    return p_graph
Exemplo n.º 2
0
 def compare(self, alt1, alt2):
     val1 = self.eval(alt1)
     val2 = self.eval(alt2)
     if val1 > val1:
         return Relation.strict_preference()
     elif val1 < val2:
         return Relation.strict_dispreference()
     return Relation.equal()
Exemplo n.º 3
0
 def compare(self, alt1, alt2):
     if (not alt1.matches(self.domain)) or not (alt2.matches(self.domain)):
         return None
     for attr in self.importance:
         v1 = alt1.value(attr)
         v2 = alt2.value(attr)
         if v1 != v2:
             if self.orders[attr].index(v1) < self.orders[attr].index(v2):
                 return Relation.strict_preference()
             else:
                 return Relation.strict_dispreference()
     return Relation.equal()
 def compare(self, alt1, alt2):
     pref = self.order(alt1)
     v1,v2 = alt1.value(self.attr),alt2.value(self.attr)
     if v1 not in pref or v2 not in pref:
         return Relation.equal()
     r1,r2 = pref.index(v1),pref.index(v2)
     if r1 < r2:
         return Relation.strict_preference()
     elif r1 > r2:
         return Relation.strict_dispreference()
     else:
         return Realtion.equal()
Exemplo n.º 5
0
 def compare(self, alt1, alt2, dnf=True):
     satVecs = [[], []]
     if dnf:
         satVecs[0] = self.eval_DNF(alt1)
         satVecs[1] = self.eval_DNF(alt2)
     else:
         satVecs[0] = self.eval_CNF(alt1)
         satVecs[1] = self.eval_CNF(alt2)
     for i in range(len(self.ranks)):
         result = self._pareto(satVecs[0][i], satVecs[1][i])
         if result != Relation.equal():
             return result
     return Relation.equal()
Exemplo n.º 6
0
 def compare(self, alt1, alt2):
     for rank in self.attr_order:
         rel = Relation.equal()
         for attr in rank:
             if (alt1.value(attr) != alt2.value(attr)):
                 attr_rel = self.preferences[attr].compare(alt1, alt2)
                 if rel == Relation.equal():
                     rel = attr_rel
                 elif not (rel == attr_rel):
                     return Relation.incomparable()
         if not (rel == Relation.equal()):
             return rel
     return Relation.equal()
 def compare(self, alt1, alt2, dnf=True):
     rank1 = rank2 = 0
     if dnf:
         rank1 = self.eval_DNF(alt1)
         rank2 = self.eval_DNF(alt2)
     else:
         rank1 = self.eval_CNF(alt1)
         rank2 = self.eval_CNF(alt2)
     if rank1 < rank2:
         return Relation.strict_dispreference()
     elif rank1 > rank2:
         return Relation.strict_preference()
     return Relation.equal()
Exemplo n.º 8
0
 def learn_greedy_maximin(ex_set, domain):
     ex_set.unflag_all()
     for ex in ex_set.each_unflagged():
         if ex.get_relation() == Relation.equal():
             ex.flag()
     importance = []
     orders = [[j+1 for j in range(domain.attr_length(i))] for i in range(domain.length())]
     possible_next = [i for i in range(domain.length())]
     agents = ex_set.get_agents()
     while len(possible_next) != 0:
         best_attr = possible_next[0]
         best_score = -1
         best_order = [i+1 for i in range(domain.attr_length(best_attr))]
         for attr in possible_next:
             values = [i+1 for i in range(domain.attr_length(attr))]
             for order in permutations(values,len(values)):
                 agent_counts = {}
                 for agent in agents:
                     agent_counts[agent] = 0
                 for ex in ex_set.each_unflagged():
                     if ex.get_alts()[0].value(attr) == ex.get_alts()[1].value(attr):
                         continue
                     rank1 = order.index(ex.get_alts()[0].value(attr))
                     rank2 = order.index(ex.get_alts()[1].value(attr))
                     if ex.get_relation() == Relation.strict_preference():
                         if rank1 > rank2:
                             agent_counts[ex.get_agent()] += 1
                     if ex.get_relation() == Relation.strict_dispreference():
                         if rank1 < rank2:
                             agent_counts[ex.get_agent()] += 1
                 incorrect = 0
                 for _, count in agent_counts.items():
                     if count > incorrect:
                         incorrect = count
                 if best_score == -1 or incorrect < best_score:
                     best_score = incorrect
                     best_attr = attr
                     best_order = order[:]
         importance.append(best_attr)
         orders[best_attr] = best_order
         possible_next.remove(best_attr)
         for ex in ex_set.each_unflagged():
             if ex.get_alts()[0].value(best_attr) != ex.get_alts()[1].value(best_attr):
                 ex.flag()
     result = LPM(domain)
     result.importance = importance
     result.orders = orders
     return result
Exemplo n.º 9
0
def evaluate_cuda_multi(ex_set, learner, device=None):
    count = 0
    agent_counts = {}
    for agent in ex_set.get_agents():
        agent_counts[agent] = 0
    for i in range(len(ex_set)):
        inp, expect = ex_set[i]
        if device is not None:
            inp = inp.to(device)
        label = learner.forward_squash(inp)  #.to(torch.device('cpu'))
        label = Relation.parse_label(label)
        if label.value == expect - 2:
            if ex_set.get(i).get_agent() is not None:
                agent_counts[ex_set.get(i).get_agent()] += 1
    result = []
    agents = list(agent_counts.keys())
    agents.sort()
    for agent in agents:
        total = ex_set.agent_count(agent)
        count = agent_counts[agent]
        if total > 0:
            result.append(count / float(total))
        else:
            result.append(0)
    return result
Exemplo n.º 10
0
def evaluate(ex_set, learner):
    count = 0
    for example in ex_set.example_list():
        inp, _ = prepare_example(example)
        label = Relation.parse_label(learner.forward_squash(inp))
        if label == example.get_relation():
            count += 1
        del inp
        del label
    return count / float(len(ex_set))
Exemplo n.º 11
0
 def _pareto(self, sVec1, sVec2):
     if len(sVec1) != len(sVec2):
         return Relation.incomparable()
     result = Relation.equal()
     for i in range(len(sVec1)):
         if result == Relation.equal():
             if sVec1[i] < sVec2[i]:
                 result = Relation.strict_preference()
             elif sVec2[i] < sVec1[i]:
                 result = Relation.strict_dispreference()
         elif result == Relation.strict_preference():
             if sVec2[i] < sVec1[i]:
                 return Relation.incomparable()
         elif result == Relation.strict_dispreference():
             if sVec1[i] < sVec2[i]:
                 return Relation.incomparable()
     return result
Exemplo n.º 12
0
def full_cuda_eval(domain, learner, agent, device=None):
    count = 0
    total = 0
    for pair in domain.each_pair():
        total += 1
        expect = agent.build_example(pair[0], pair[1]).relation
        inp = prepare_pair(pair)
        if device is not None:
            inp = inp.to(device)
        label = learner.forward_squash(inp)
        label = Relation.parse_label(label)
        if label == expect:
            count += 1
    return count / (float(total))
Exemplo n.º 13
0
def evaluate_cuda(ex_set, learner, device=None):
    count = 0
    for i in range(len(ex_set)):
        inp, expect = ex_set[i]
        if device is not None:
            inp = inp.to(device)
        label = learner.forward_squash(inp)  #.to(torch.device('cpu'))
        label = Relation.parse_label(label)
        if label.value == expect - 2:
            count += 1
        del inp
        del expect
        del label
    return count / float(len(ex_set))
Exemplo n.º 14
0
 def compare(self, alt1, alt2):
     # Base case
     if self.leaf:
         return Relation.equal()
     # See if comparison happens on this node.
     if alt1.value(self.attr) == alt2.value(self.attr):
         # No comparison. Proceed down the tree.
         if self.split:
             c_index = self.cpt.order(alt1)
             c_index = c_index.index(alt1.value(self.attr))
             return self.children[c_index].compare(alt1, alt2)
         else:
             return self.children[0].compare(alt1, alt2)
     else:
         # Handle differences
         order = self.cpt.order(alt1)
         if order.index(alt1.value(self.attr)) < order.index(
                 alt2.value(self.attr)):
             return Relation.strict_preference()
         elif order.index(alt1.value(self.attr)) > order.index(
                 alt2.value(self.attr)):
             return Relation.strict_dispreference()
         else:
             return Relation.equal()