Exemple #1
0
class Robot:
    dcmotor = DC()
    relay = Relay()
    sensor = UltrasonicSensor()
    status = 0
    robotIsRight = True

    def get_distance(self):
        self.relay.switchRelay(1)
        self.dcmotor.forward()
        self.status = 1
        while self.robotIsRight:
            time.sleep(0.5)
            dist = self.sensor.distance()
            print(str(dist))
            if dist < 10:
                self.motorStop()
                self.status = 0
            else:
                self.relay.switchRelay(1)
                self.dcmotor.forward()

    def startProgram(self):
        x = threading.Thread(target=self.get_distance).start()

    def motorStop(self):
        self.dcmotor.stop()
        self.relay.switchRelay(0)
Exemple #2
0
def from_directory(directory):
    dcs = []
    tenants = []

    filenames = os.listdir(directory)
    dcs_filenames = [x for x in filenames if x != "tenants.xml"]

    dcs_filenames = sorted(dcs_filenames, key=lambda x: int(x.split(".")[0]))

    dcs_paths = [os.path.join(directory, x) for x in dcs_filenames]
    for path in dcs_paths:
        content = None
        with open(path, "r") as f:
            content = f.read()

        bs = BeautifulSoup(content, 'xml')
        dc = DC(bs)
        dcs.append(dc)
        tenants.extend(dc.tenants_placed)

    path = os.path.join(directory, "tenants.xml")
    tenants += read_tenants_from_file(path)

    # return dcs, tenants, tenants_placed
    e = Evaluator_base()
    for tenant in tenants:
        tenant.evaluation = e.get_tenant_evaluation(tenant)

    return dcs, tenants
Exemple #3
0
def testAuto(num_sw=16, num_port=48, num_group=4, num_tries=10000, control_threshold=0.9):
    """Run an automatic test with given parameters.
    Generate group randomly, and send traffic randomly with control_threshold
    @param num_sw: Number of switches in the datacenter
    @param num_port: Number of ports of each switch
    @param num_group: Number of groups in the datacenter
    @param num_tries: Number of random tests
    @param control_threshold: How many percent flows are cross-group
    """
    dc = DC(Flat(num_sw=num_sw, num_port=num_port))
    dc.genGroupRandom(num_group=num_group)
    dc.testShuffleTraffic(num_tries, control_threshold)
    dc.showInfo()
    dc.showStat()
Exemple #4
0
def dcsolver(initial="dog", goal="cat", cost='steps'):
    """ solve a dog-cat problem, print the solution, it's cost the the time taken """
    problem = DC(initial, goal, cost)
    start = time.time()
    solution = astar_search(problem)
    elapsed = time.time() - start
    if solution:
        path = ' '.join([node.state for node in solution.path()])
        path_cost = int(round(solution.path_cost))
    else:
        path = "NO SOLUTION"
        path_cost = -1
    print(f"{problem} cost:{path_cost}; time:{elapsed: .3f}; solution:{path}")
Exemple #5
0
def dcs_gen1():
    paths = ["../data/generated_dcs/{}.xml".format(x) for x in range(1, 21)]
    dcs = []
    for path in paths:
        with open(path, "r") as f:
            content = f.read()
        bs = BeautifulSoup(content, 'xml')
        bs.find("tenants").extract()
        bs.find("dcxml").append(bs.new_tag('tenants'))

        dcs.append(DC(bs))

    return (dcs)
Exemple #6
0
def dcsolver(initial, goal, cost='steps'):
    if initial not in dictionary:
        print "%s is not in my dictionary" % initial
        return
    if goal not in dictionary:
        print "%s is not in my dictionary" % goal
        return
    start = time.time()
    solution = astar_search(DC(initial, goal, cost))
    elapsed = time.time() - start
    if solution:
        path = ' '.join([node.state for node in solution.path()])
        path_cost = int(round(solution.path_cost))
    else:
        path = 'NO SOLUTION %s %s' % (initial, goal)
        path_cost = -1
    print "%s %s:\t%s (%.4f)" % (cost, path_cost, path, elapsed)
Exemple #7
0
def folder_loader(template_folder_xml, dcs_count):
    dcs = []
    # tenants_placed = []

    for i in range(1, dcs_count + 1):
        path = template_folder_xml.format(i)
        content = None
        with open(path, "r") as f:
            content = f.read()

        bs = BeautifulSoup(content, 'xml')
        dcs.append(DC(bs))
        # for tenant_bs in bs.find_all("tenant"):
        #     tenants_placed.append(Tenant(tenant_bs))

    path = template_folder_xml.format("remained_tenants")

    tenants = read_tenants_from_file(path)

    # return dcs, tenants, tenants_placed
    return dcs, tenants
Exemple #8
0
def domestic():
    filenames = os.listdir("../data/domestic_dcs/")
    paths = ["../data/domestic_dcs/{}".format(x) for x in filenames]
    paths = ["../data/domestic_dcs/{}.xml".format(x) for x in range(1, 4)]
    dcs = []
    for path in paths:
        with open(path, "r") as f:
            content = f.read()
        bs = BeautifulSoup(content, 'xml')
        bs.find("tenants").extract()
        bs.find("dcxml").append(bs.new_tag('tenants'))

        dcs.append(DC(bs))

    tenants = []
    content = None
    with open("../data/domestic_tenants.xml", "r") as f:
        content = f.read()
    bs = BeautifulSoup(content, 'xml')
    for tenant_bs in bs.find_all("tenant"):
        tenants.append(Tenant(tenant_bs))

    return dcs, tenants
Exemple #9
0
def with_return():
    paths = ["../data/domestic_return/{}.xml".format(x) for x in range(1, 3)]
    dcs = []
    for path in paths:
        with open(path, "r") as f:
            content = f.read()
        bs = BeautifulSoup(content, 'xml')
        bs.find("tenants").extract()
        bs.find("dcxml").append(bs.new_tag('tenants'))

        dcs.append(DC(bs))

    tenants = []
    content = None
    with open("../data/domestic_return/tenants.xml", "r") as f:
        content = f.read()
    bs = BeautifulSoup(content, 'xml')
    for tenant_bs in bs.find_all("tenant"):
        tenants.append(Tenant(tenant_bs))

    dcs[0].tenants_placed.append(tenants[0])
    tenants[0].mark = "fst"

    return dcs, tenants
	def __init__(self, bezeichnung, status=0, red=0, green=0, blue=0):
		DC.__init__(self, bezeichnung, status, red, green)
		self._blue = blue
Exemple #11
0
def testManu(host_file='host_sw_map.txt', sw_file='metis_input.txt.part.10', trace_file='flow_anon_2008-01-01.agg.txt'):
    """Read host,sw,trace from out files, and run the test.
    @param host_file: File that stores the host-sw mapping information.
        every line with format: host_id sw_id
    @param sw_file: File that stores the sw-group mapping information
    @param trace_file: File that stores the trace record information
    """
    if not (os.path.isfile(host_file) and os.path.isfile(sw_file)):
        print "[ERROR] Invalid file name, host_sw_map=%s, sw_group_map=%s." %(host_file,sw_file)
        return
    dc = DC()
    print "readin sw-group mapping information."
    dc.readinEdgeSWInfo(sw_file)
    print "readin host-sw mapping information."
    dc.readinVMInfo(host_file)
    #dc.showInfo()
    print "readin trace file."
    if os.path.isfile(trace_file):
        dc.testTraffic(trace_file)
    else:
        print "%s, No traffic file is given for testing." %trace_file
    dc.showInfo()
    dc.showStat()
Exemple #12
0
 def __init__(self, engine="CHROME"):
     self.browser = self.get_browser(engine)
     self.nyc = NYC(self.browser)
     self.dc = DC(self.browser)