def make_random_tree(instances, instance_classes, attr, data, node_label):
    avgClassValue = float(utilities.avgClassValue(instance_classes))
    if avgClassValue > 0 and avgClassValue < 1 and len(attr) > 0:
        attr_value = attr[random.randint(0, len(attr) - 1)]
        child = utilities.getChildInstances(data, attr_value, instances,
                                            instance_classes)
        parent = utilities.Node(None, None, None, None, None, None)
        parent.instances = instances
        parent.instance_classes = instance_classes
        parent.attr = attr_value
        attr = list(attr)
        attr.remove(attr_value)
        parent.left = make_random_tree(child[0][0], child[0][1], attr, data,
                                       (2 * node_label))
        parent.right = make_random_tree(child[1][0], child[1][1], attr, data,
                                        ((2 * node_label) + 1))
        parent.label = node_label
        return parent
    else:
        parent = utilities.Node(None, None, None, None, None, None)
        parent.instances = instances
        parent.instance_classes = instance_classes
        parent.label = node_label
        if len(attr) == 0:
            if avgClassValue >= 0.5:
                parent.attr = 1
            else:
                parent.attr = 0
        else:
            parent.attr = int(avgClassValue)
        return parent
    return 0
Example #2
0
def print_astar_costs():
    roads = load_map_from_csv()
    with open("problems.csv") as problems_file:
       read_file = csv.reader(problems_file,delimiter=',')
       for splited_line in read_file:
           source = int(splited_line[0])
           target = int(splited_line[1])
           cost = find_astar_cost(source, target, utilities.g,utilities.h,roads)
           with open('results/AStarRuns.txt', 'a') as output:
               problem = utilities.RoutingProblem(source,target,roads,utilities.cost_function)
               cost_h = utilities.h(problem,utilities.Node(problem.source),utilities.Node(problem.target))
               output.write(str(cost) +","+ str(cost_h) + '\n')
def make_tree(instances, instance_classes, attr, data, node_label):
    avgClassValue = float(utilities.avgClassValue(instance_classes))
    if avgClassValue > 0 and avgClassValue < 1 and len(attr) > 0:
        max_attr = ''
        max_IG = -999
        max_child_instances = []
        for attr_value in attr[:]:
            parent_entropy = utilities.getEntropy(instance_classes)
            child = utilities.getChildInstances(data, attr_value, instances,
                                                instance_classes)
            left_child_entropy = utilities.getEntropy(child[0][1])
            right_child_entropy = utilities.getEntropy(child[1][1])
            length_left_child = len(child[0][0])
            length_right_child = len(child[1][0])
            length_parent = len(instances)
            IG = parent_entropy - (
                ((length_left_child / length_parent) * left_child_entropy) +
                ((length_right_child / length_parent) * right_child_entropy))
            if IG >= max_IG:
                max_IG = IG
                max_attr = attr_value
                max_child_instances = child
        parent = utilities.Node(None, None, None, None, None, None)
        parent.instances = instances
        parent.instance_classes = instance_classes
        parent.attr = max_attr
        attr = list(attr)
        attr.remove(max_attr)
        parent.left = make_tree(max_child_instances[0][0],
                                max_child_instances[0][1], attr, data,
                                (2 * node_label))
        parent.right = make_tree(max_child_instances[1][0],
                                 max_child_instances[1][1], attr, data,
                                 ((2 * node_label) + 1))
        parent.label = node_label
        return parent
    else:
        parent = utilities.Node(None, None, None, None, None, None)
        parent.instances = instances
        parent.instance_classes = instance_classes
        parent.label = node_label
        if len(attr) == 0:
            if avgClassValue >= 0.5:
                parent.attr = 1
            else:
                parent.attr = 0
        else:
            parent.attr = int(avgClassValue)
        return parent
    return 0
Example #4
0
def print_idastar_costs():
    roads = load_map_from_csv()
    with open("problems.csv") as problems_file:
        read_file = csv.reader(problems_file, delimiter=',')
        counter = 0
        for splited_line in read_file:
            if counter >= 5:
                break
            counter += 1
            source = int(splited_line[0])
            target = int(splited_line[1])
            path = find_idastar_route(source, target, roads)
            path_cost = path_idastar_cost(path, roads)
            with open('results/IDAstarRuns.txt', 'a') as output:
                problem = utilities.RoutingProblem(source, target, roads,
                                                   utilities.cost_function)
                cost_h = utilities.h(problem, utilities.Node(problem.source),
                                     utilities.Node(problem.target))
                output.write(str(path_cost) + "," + str(cost_h) + '\n')


# print_idastar_costs()
def printTree(node, indent):
    parent = utilities.Node(None, None, None, None, None, None)
    parent = node
    print("| " * indent, end="")
    if isPureNode(parent.left):
        print(parent.attr, ' = 0 : ', parent.left.attr)
    else:
        print(parent.attr, ' = 0 : ')
        printTree(parent.left, indent + 1)
    print("| " * indent, end="")
    if isPureNode(parent.right):
        print(parent.attr, ' = 1 : ', parent.right.attr)
    else:
        print(parent.attr, ' = 1 : ')
        printTree(parent.right, indent + 1)
Example #6
0
def find_astar_route(source,target,g,h,roads=None):
  if roads is None:
      roads = load_map_from_csv()
  problem = utilities.RoutingProblem(source,target,roads,utilities.cost_function)
  path , cost = utilities.best_first_graph_search(problem, f=lambda n: utilities.g(n)+utilities.h(problem,n,utilities.Node(problem.target)))
  return path
def isPureNode(node):
    parent = utilities.Node(None, None, None, None, None, None)
    parent = node
    return (type(parent.attr) is int)