Ejemplo n.º 1
0
 def cleanup(self, tgt_config):
     logfile, self.tgt = dpdk_helper.set_defaults(self.home, self.progname,
                                                  tgt_config, "cleanup")
     dpdk_helper.print_defaults(self.tgt, 0, self.dpdk_path, logfile)
     self.logfd = logger.open_log(logfile)
     self.__cleanup()
     return 0
Ejemplo n.º 2
0
    def prepare(self):
        assert self.target.dolock().owned == state.LOCK_EX
        self.target.build_starting()
        self.before_t = _try_stat(self.target.name)

        newstamp = self.target.read_stamp()
        if newstamp.is_override_or_missing(self.target):
            if newstamp.is_missing():
                # was marked generated, but is now deleted
                debug3('oldstamp=%r newstamp=%r\n', self.target.stamp, newstamp)
                self.target.forget()
                self.target.refresh()
            elif vars.OVERWRITE:
                warn('%s: you modified it; overwrite\n', self.target.printable_name())
            else:
                warn('%s: you modified it; skipping\n', self.target.printable_name())
                return 0
        if self.target.exists_not_dir() and not self.target.is_generated:
            # an existing source file that was not generated by us.
            # This step is mentioned by djb in his notes.
            # For example, a rule called default.c.do could be used to try
            # to produce hello.c, but we don't want that to happen if
            # hello.c was created in advance by the end user.
            if vars.OVERWRITE:
                warn('%s: exists and not marked as generated; overwrite.\n',
                     self.target.printable_name())
            else:
                warn('%s: exists and not marked as generated; not redoing.\n',
                     self.target.printable_name())
                debug2('-- static (%r)\n', self.target.name)
                return 0

        (self.dodir, self.dofile, self.dobasedir, self.dobasename, self.doext) = _find_do_file(self.target)
        if not self.dofile:
            if newstamp.is_missing():
                err('no rule to make %r\n', self.target.name)
                return 1
            else:
                self.target.forget()
                debug2('-- forget (%r)\n', self.target.name)
                return 0  # no longer a generated target, but exists, so ok

        self.outdir = self._mkoutdir()
        # name connected to stdout
        self.tmpname_sout = self.target.tmpfilename('out.tmp')
        # name provided as $3
        self.tmpname_arg3 = os.path.join(self.outdir, self.target.basename())
        # name for the log file
        unlink(self.tmpname_sout)
        unlink(self.tmpname_arg3)
        self.log_fd = logger.open_log(self.target, truncate=True)
        self.tmp_sout_fd = os.open(self.tmpname_sout, os.O_CREAT|os.O_RDWR|os.O_EXCL, 0666)
        close_on_exec(self.tmp_sout_fd, True)
        self.tmp_sout_f = os.fdopen(self.tmp_sout_fd, 'w+')

        return None
Ejemplo n.º 3
0
 def reset(self, tgt_config, n_pages, device_list):
     logfile, self.tgt = dpdk_helper.set_defaults(self.home, self.progname,
                                                  tgt_config, "init")
     dpdk_helper.print_defaults(self.tgt, 0, self.dpdk_path, logfile)
     self.logfd = logger.open_log(logfile)
     print
     self.__cleanup()
     print
     self.__setup(n_pages, device_list)
     return 0
Ejemplo n.º 4
0
def main(argc, argv):
    home = os.environ['HOME']
    ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(
        home, progname)
    vm_uuid, vm_xml, vrf_id, evpn_id = vca_helper.set_vrf_defaults(home)
    logfd = logger.open_log(logfile)
    ovs_helper.print_defaults(ovs_path, os_release, hostname, logfile)

    try:
        opts, args = getopt.getopt(argv, "hu:C:V:cds:S")
    except getopt.GetoptError as err:
        print progname + ": invalid argument, " + str(err)
        usage()

    config_file = "/usr/local/openvswitch/templates/vm_ports_default_config.json"
    uplink_iface = "port1"
    configure = False
    deconfigure = False
    status = False
    status_arg = ""
    vstatus = False
    rc = 0
    for opt, arg in opts:
        if opt == "-c":
            configure = True
        elif opt == "-d":
            deconfigure = True
        elif opt == "-s":
            status = True
            status_arg = arg
        elif opt == "-S":
            vstatus = True
        elif opt == "-u":
            uplink_iface = arg
        elif opt == "-C":
            config_file = arg
        elif opt == "-h":
            usage()

    if (configure == False and deconfigure == False and status == False
            and vstatus == False):
        print progname + ": invalid usage"
        usage

    if (configure == True):
        rc = do_configure(progname, ovs_path, br, config_file, uplink_iface,
                          logfd)
    elif (deconfigure == True):
        rc = do_deconfigure(config_file, logfd)
    elif (status == True):
        rc = do_status(progname, ovs_path, br, config_file, status_arg, logfd)
    elif (vstatus == True):
        rc = do_vstatus(progname, ovs_path, br, config_file, logfd)
    sys.exit(rc)
def prologue(cfg: Namespace, *varargs) -> SummaryWriter:
    # sanity checks
    assert cfg.device == "cpu" or (cfg.device == "cuda"
                                   and torch.cuda.is_available())

    # dirs
    base_dir = f"../experiments/{cfg.exp_name}"

    os.makedirs(f"{base_dir}/out", exist_ok=True)
    os.makedirs(f"{base_dir}/chkpt", exist_ok=True)
    os.makedirs(f"{base_dir}/logs", exist_ok=True)
    os.makedirs(f"{base_dir}/trainlogs", exist_ok=True)
    logger.open_log(f"{base_dir}/trainlogs")

    dump_cfg(f"{base_dir}/train_config.txt", vars(cfg))

    # tb writer
    writer = SummaryWriter(f"{base_dir}/logs")

    return writer
Ejemplo n.º 6
0
 def build(self, tgt_config, c_flag):
     if (tgt_config != "default") and (tgt_config != "ivshmem"):
         print self.progname + ": unknown target configuration"
         return 1
     if (self.dpdk_sb == ""):
         print self.progname + ": missing DPDK sandbox"
         return 1
     logfile, self.tgt = dpdk_helper.set_defaults(self.home, self.progname,
                                                  tgt_config, "build")
     dpdk_helper.print_defaults(self.tgt, c_flag, self.dpdk_path, logfile)
     self.logfd = logger.open_log(logfile)
     self.__build_dpdk(c_flag)
     self.__build_vmxnet3()
     return 0
Ejemplo n.º 7
0
def main(argc, argv):
	ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(home, progname)
	try:
		opts, args = getopt.getopt(argv, "hs:")
	except getopt.GetoptError as err:
		print progname + ": invalid argument, " + str(err)
		usage()
	for opt, arg in opts:
		if opt == "-s":
			ovs_sb = arg
			OVS_DIR = home + "/Linux/src/sandbox/" + ovs_sb + "/VCA"
			ovs_path = OVS_DIR + "/utilities"
		else:
			usage()
	logfd = logger.open_log(logfile)
	bridge = ovs_bridge.Bridge(ovs_path, br, logfd)
	bridge.dump_flows()
	exit(0)
Ejemplo n.º 8
0
def main(argc, argv):
    ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(
        home, progname)
    global testcase_id, listing
    exit_on_failure = False
    ovs_vers = ovs_helper.get_ovs_version(ovs_path)
    suite_list = ["all"]
    test_subset = []
    try:
        opts, args = getopt.getopt(argv, "hes:t:l")
    except getopt.GetoptError as err:
        print progname + ": invalid argument, " + str(err)
        usage()
    for opt, arg in opts:
        if opt == "-h":
            usage()
        elif opt == "-l":
            listing = True
        elif opt == "-e":
            exit_on_failure = True
        elif opt == "-s":
            suite_list = arg.split(",")
        elif opt == "-t":
            test_subset = arg.split(",")
        else:
            usage()
    logfd = logger.open_log(logfile)
    print "Test Configuration:"
    print "Suite: " + str(suite_list)
    for suite in suite_list:
        if (validate_args(progname, suite, vm_name, aux_vm_name, mirror_dst_ip,
                          mirror_dst_port) == False):
            exit(1)
        if (suite == "XXX"):
            run_XXX(br, logfd, ovs_path, ovs_vers, exit_on_failure,
                    test_subset)
        else:
            run_XXX(br, logfd, ovs_path, ovs_vers, exit_on_failure,
                    test_subset)

    exit(0)
Ejemplo n.º 9
0
def main(argc, argv):
	ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(home, progname)
	try:
		opts, args = getopt.getopt(argv, "hs:")
	except getopt.GetoptError as err:
		print progname + ": invalid argument, " + str(err)
		usage()
	for opt, arg in opts:
		if opt == "-s":
			ovs_sb = arg
			OVS_DIR = home + "/Linux/src/sandbox/" + ovs_sb + "/VCA"
			ovs_path = OVS_DIR + "/utilities"
		else:
			usage()
	logfd = logger.open_log(logfile)
	vrf = vca_vrf.VRF(ovs_path, br, 0, None, 0, None, logfd)
	vrf.show()
	evpn = vca_evpn.EVPN(ovs_path, br, logfd, 0, None, 0, 0, None, 0, 0)
	evpn.show()
	dhcp = vca_evpn_dhcp.DHCP(ovs_path, br, logfd, 0, None, 0, 0, None, 0, 0)
	dhcp.show()
	exit(0)
Ejemplo n.º 10
0
def main(argc, argv):
	ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(home, progname)
	try:
		opts, args = getopt.getopt(argv, "hs:i:t:T:l:r:");
	except getopt.GetoptError as err:
		print progname + ": invalid argument, " + str(err)
		usage()

	vm_ip_local = ""
	port_type = ""
	mgmt_iface = ""
	tnl_type = ""
	rtep_ip = ""
	for opt, arg in opts:
		if opt == "-s":
			ovs_sb = arg
			OVS_DIR = home + "/Linux/src/sandbox/" + ovs_sb + "/VCA"
			ovs_path = OVS_DIR + "/utilities"
		elif opt == "-i":
			mgmt_iface = arg
		elif opt == "-t":
			port_type = arg
		elif opt == "-l":
			vm_ip_local = arg
		elif opt == "-T":
			Topt_tok = arg.split(":")
			tnl_type = Topt_tok[0]
			rtep_ip = Topt_tok[1]
			tnl_key = ovs_helper.set_tnl_key(tnl_type)
		elif opt == "-r":
			vm_ip_remote = arg
		elif opt == "-h":
			usage()

	logfd = logger.open_log(logfile)
	bridge = ovs_bridge.Bridge(ovs_path, br, logfd)
	mgmt_ip = net.get_iface_ip(mgmt_iface)
	vm_iface = ovs_helper.set_vm_iface(mgmt_iface, port_type)

	if (len(vm_ip_local) == 0):
		print(progname + ": Need value of local VM IP address")
		usage()
	if (len(rtep_ip) == 0):
		print(progname + ": Need value of RTEP IP address")
		usage()
	if (len(port_type) == 0):
		print(progname + ": Need value of port type")
		usage()
	if (len(tnl_type) == 0):
		print(progname + ": Need value of tunnel type")
		usage()
	if (len(tnl_key) == 0):
		print(progname + ": Tunnel key not set, specify tunnel type")
		usage()
	if (len(mgmt_iface) == 0):
		print(progname + ": Need value of management interface")
		usage()
	if (port_type != "internal" and port_type != "vlan"):
		print(progname + ": Need a supported type of port")
		exit(1)

	ovs_helper.print_defaults(ovs_path, os_release, hostname, logfile)
	time.sleep(2)

	tap = ovs_vport_tap.Tap(ovs_path, port_type, br, vm_iface,
				vm_ip_local, logfd)
	time.sleep(2)

	rtep = ovs_vport_tnl.Tunnel(ovs_path, br, tnl_type, tnl_key, rtep_ip,
				    "rtep", logfd, True)
	rtep_port = rtep.get_tnl_name()
	time.sleep(2)
	ltep = ovs_vport_tnl.Tunnel(ovs_path, br, tnl_type, tnl_key, mgmt_ip,
				    "ltep", logfd, True)
	ltep_port = ltep.get_tnl_name()
	time.sleep(2)

	flow = vca_flows.Flows(ovs_path, br, logfd)
	flow.set_l2_flow(vm_iface, rtep_port)
	flow.set_l2_flow(ltep_port, vm_iface)
def main(argc, argv):
    ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(
        home, progname)
    vm_uuid, vm_xml, vrf_id, evpn_id = vca_helper.set_vrf_defaults(home)
    try:
        opts, args = getopt.getopt(argv, "Chs:i:t:T:l:r:m:FV:")
    except getopt.GetoptError as err:
        print progname + ": invalid argument, " + str(err)
        usage()

    vm_ip_local = ""
    vm_ip_remote = ""
    vm_mac_remote = ""
    port_type = ""
    mgmt_iface = ""
    tnl_type = ""
    rtep_ip = ""
    add_flows = False
    vm_type = "vm"
    for opt, arg in opts:
        if opt == "-s":
            ovs_sb = arg
            OVS_DIR = home + "/Linux/src/sandbox/" + ovs_sb + "/VCA"
            ovs_path = OVS_DIR + "/utilities"
        elif opt == "-i":
            mgmt_iface = arg
        elif opt == "-t":
            port_type = arg
        elif opt == "-l":
            vm_ip_local = arg
        elif opt == "-V":
            vm_type = arg
        elif opt == "-T":
            Topt_tok = arg.split(":")
            tnl_type = Topt_tok[0]
            rtep_ip = Topt_tok[1]
            tnl_key = ovs_helper.set_tnl_key(tnl_type)
            vrf_tnl_key = str(int(tnl_key))
            evpn_tnl_key = str(int(tnl_key) + 1)
        elif opt == "-r":
            vm_ip_remote = arg
        elif opt == "-m":
            vm_mac_remote = arg
        elif opt == "-F":
            add_flows = True
        elif opt == "-h":
            usage()

    logfd = logger.open_log(logfile)
    bridge = ovs_bridge.Bridge(ovs_path, br, logfd)
    mgmt_ip = net.get_iface_ip(mgmt_iface)
    vm_iface = ovs_helper.set_vm_iface(mgmt_iface, port_type)

    if (len(vm_ip_local) == 0):
        print(progname + ": Need value of local VM IP address")
        usage()
    if (len(vm_ip_remote) == 0):
        print(progname + ": Need value of remote VM IP address")
        usage()
    if (len(vm_mac_remote) == 0):
        print(progname + ": Need value of remote VM mac address")
        usage()
    if (len(rtep_ip) == 0):
        print(progname + ": Need value of RTEP IP address")
        usage()
    if (len(port_type) == 0):
        print(progname + ": Need value of port type")
        usage()
    if (len(tnl_type) == 0):
        print(progname + ": Need value of tunnel type")
        usage()
    if ((len(vrf_tnl_key) == 0) and (len(evpn_tnl_key) == 0)):
        print(progname + ": Tunnel key not set, specify tunnel type")
        usage()
    if (len(mgmt_iface) == 0):
        print(progname + ": Need value of management interface")
        usage()
    if (port_type != "internal" and port_type != "vlan"):
        print(progname + ": Need a supported type of port")
        exit(1)

    ovs_helper.print_defaults(ovs_path, os_release, hostname, logfile)
    time.sleep(2)

    tap = ovs_vport_tap.Tap(ovs_path, port_type, br, vm_iface, vm_ip_local,
                            logfd)
    vm_netmask = tap.get_netmask()
    vm_subnet = tap.get_subnet()
    vm_mac = tap.get_mac()
    vm_gw_ip = tap.get_gw_ip()
    ofp_port_no = tap.get_ofp_port()
    time.sleep(2)

    vm = vca_vm.VM(ovs_path, br, vm_uuid, vm_iface, vm_ip_local, vm_subnet,
                   vm_netmask, vm_gw_ip, vm_mac, vm_xml, vm_type, logfd)
    vm.define()
    time.sleep(2)
    vm.start()
    time.sleep(2)

    evpn = vca_evpn.EVPN(ovs_path, br, logfd, vrf_id, tnl_type, vrf_tnl_key,
                         evpn_id, evpn_tnl_key, mgmt_ip, vm_ip_local)
    time.sleep(2)
    if (add_flows == True):
        evpn.set_flows(vm_iface, vm_ip_local, vm_netmask, vm_subnet, vm_mac,
                       vm_gw_ip, vm_uuid, rtep_ip, vm_ip_remote, vm_mac_remote,
                       vm_type)

    vm_port_no = vm.vm_port()
    port_str = "VM port: " + str(vm_port_no) + ", OFP port: " + str(
        ofp_port_no)
    if (vm_port_no > 0 and ofp_port_no > 0 and vm_port_no == ofp_port_no):
        print("VM " + vm_uuid + " is resolved, " + port_str)
    else:
        print("VM " + vm_uuid + " is NOT resolved, " + port_str)
        exit(1)
Ejemplo n.º 12
0
def main(argc, argv):
    ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(
        home, progname)
    try:
        opts, args = getopt.getopt(argv, "Chs:i:t:T:l:r:n:")
    except getopt.GetoptError as err:
        print progname + ": invalid argument, " + str(err)
        usage()

    vm_ip_local = ""
    port_type = ""
    mgmt_iface = ""
    tnl_type = ""
    rtep_ip = ""
    n_tunnels = 0
    for opt, arg in opts:
        if opt == "-s":
            ovs_sb = arg
            OVS_DIR = home + "/Linux/src/sandbox/" + ovs_sb + "/VCA"
            ovs_path = OVS_DIR + "/utilities"
        elif opt == "-C":
            cmd = "ovs-vsctl show | grep Port | grep -v alubr0 | grep -v svc | awk '{print $2}' | sed 's/\"//g'"
            port_list = shell.call_prog_as_is(cmd)
            for port in port_list.splitlines():
                cmd = "ovs-vsctl del-port " + port
                print "Running " + cmd
                shell.call_prog_as_is(cmd)
            sys.exit(1)
        elif opt == "-i":
            mgmt_iface = arg
        elif opt == "-t":
            port_type = arg
        elif opt == "-l":
            vm_ip_local = arg
        elif opt == "-T":
            Topt_tok = arg.split(":")
            tnl_type = Topt_tok[0]
            rtep_ip = Topt_tok[1]
            tnl_key = ovs_helper.set_tnl_key(tnl_type)
        elif opt == "-r":
            vm_ip_remote = arg
        elif opt == "-n":
            n_tunnels = int(arg)
        elif opt == "-h":
            usage()

    logfd = logger.open_log(logfile)
    bridge = ovs_bridge.Bridge(ovs_path, br, logfd)
    mgmt_ip = net.get_iface_ip(mgmt_iface)
    vm_iface = ovs_helper.set_vm_iface(mgmt_iface, port_type)

    if (len(vm_ip_local) == 0):
        print(progname + ": Need value of local VM IP address")
        usage()
    if (len(rtep_ip) == 0):
        print(progname + ": Need value of RTEP IP address")
        usage()
    if (len(port_type) == 0):
        print(progname + ": Need value of port type")
        usage()
    if (len(tnl_type) == 0):
        print(progname + ": Need value of tunnel type")
        usage()
    if (len(tnl_key) == 0):
        print(progname + ": Tunnel key not set, specify tunnel type")
        usage()
    if (len(mgmt_iface) == 0):
        print(progname + ": Need value of management interface")
        usage()
    if (port_type != "internal" and port_type != "vlan"):
        print(progname + ": Need a supported type of port")
        exit(1)

    ovs_helper.print_defaults(ovs_path, os_release, hostname, logfile)
    time.sleep(2)

    for x in range(0, n_tunnels):
        key = tnl_key + str(x)
        print "ITERATION: " + str(x) + ", key: " + key
        rtep = ovs_vport_tnl.Tunnel(ovs_path, br, tnl_type, key, rtep_ip,
                                    "rtep", logfd, True)
        rtep_port = rtep.get_tnl_name()
        ltep = ovs_vport_tnl.Tunnel(ovs_path, br, tnl_type, key, mgmt_ip,
                                    "ltep", logfd, True)
        ltep_port = ltep.get_tnl_name()
        print "Created rtep_port " + rtep_port + " ltep_port " + ltep_port + " with tnl_key " + key
        if (x % 2) == 0:
            print "Deleting rtep_port " + rtep_port
            rtep.destroy()
            print "Deleting ltep_port " + ltep_port
            ltep.destroy()
        if (process.proc_exists("ovs-vswitchd") == False
                or process.n_procs_by_name("ovs-vswitchd") < 2):
            print "ovs-vswitchd not found, exitting"
            sys.exit(1)
        print
        time.sleep(2)
Ejemplo n.º 13
0
def main(argc, argv):
    ovs_path, hostname, os_release, logfile, br, vlan_id = ovs_helper.set_defaults(
        home, progname)
    try:
        opts, args = getopt.getopt(argv, "hs:t:l:i:n:v:d:")
    except getopt.GetoptError as err:
        print progname + ": invalid argument, " + str(err)
        usage()
    vm_iface = ""
    vm_ip_local = ""
    vport_type = ""
    n_vports = 0
    dpdk_vendor_name = ""
    dpdk_device_list = ""
    ovs_sb = ""
    for opt, arg in opts:
        if opt == "-s":
            ovs_sb = arg
            OVS_DIR = home + "/Linux/src/sandbox/" + ovs_sb + "/VCA"
            ovs_path = OVS_DIR + "/utilities"
            ovs_vswitchd_path = OVS_DIR + "/vswitchd"
        elif opt == "-t":
            vport_type = arg
        elif opt == "-l":
            vm_ip_local = arg
        elif opt == "-i":
            vm_iface = arg
        elif opt == "-n":
            n_vports = arg
        elif opt == "-v":
            dpdk_vendor_name = arg
        elif opt == "-d":
            dpdk_device_list = arg
        else:
            usage()
    if (len(ovs_sb) == 0):
        ovs_vswitchd_path = ovs_path
    if (len(vm_iface) == 0):
        print(progname + ": Need value of VM interface name")
        usage()
    if (len(vm_ip_local) == 0):
        print(progname + ": Need value of local VM IP address")
        usage()
    if (len(vport_type) == 0):
        print(progname + ": Need value of port type")
        usage()
    if ((len(dpdk_device_list) != 0) and (len(dpdk_vendor_name) == 0)):
        print(progname + ": device list requires a valid DPDK vendor")
        usage()
    if ((len(dpdk_vendor_name) != 0) and (len(dpdk_device_list) == 0)):
        print(progname +
              ": DPDK vendor requires DPDK device list specification")
        usage()
    ovs_helper.print_defaults(ovs_path, os_release, hostname, logfile)
    logfd = logger.open_log(logfile)
    bridge = ovs_bridge.Bridge(ovs_path, br, logfd)
    if ((len(dpdk_vendor_name) != 0) and (len(dpdk_device_list) != 0)):
        ovs_dpdk_switch = ovs_dpdk_vendor_switch.SDK_OVS_Switch()
        ovs_dpdk_obj = ovs_dpdk_switch.alloc_SDK_OVS(dpdk_vendor_name,
                                                     ovs_vswitchd_path, br,
                                                     dpdk_device_list)
        print ovs_dpdk_obj.get_vswitchd_cmdline()
        sys.exit(0)
    tap = ovs_vport_tap.Tap(ovs_path, vport_type, br, vm_iface, vm_ip_local,
                            logfd)
    vm_mac = tap.get_mac()
    print "VM port mac address: " + vm_mac
    exit(0)
Ejemplo n.º 14
0
def run(params, filename, directory, steps=2000, quiet=False):

    try:
        for k, v in params.items():
            if type(v) == np.ndarray:
                params[k] = v[0]

        if not directory.endswith('/'):
            directory += '/'

        import json
        with open('params.json', 'w') as out:
            json.dump(params, out)

        scores = open(path + 'scores', 'a')
        score = 0
        max_score = 0
        possible_score = 0
        step_taken, tmp_score = 0, 0

        t = tp.TextPlayer(filename, directory)
        start_info = t.run()

        from golovinAgent import GolovinAgent

        agent = GolovinAgent(t, start_info, params)

        if start_info is None:
            print 'start_info is None'
            t.quit()
            exit(0)
        logger.open_log(path, filename)
        noneCount = 0
        for i in range(steps):
            command, command_type, response, additional = agent.makeAction()
            step_taken += 1
            if not quiet:
                print 'map size:', len(agent.map.edges)
                print agent.desc
                print agent.inv.text
                print command
                print response
            logger.log('description', agent.desc)
            logger.log('inventory', agent.inv.text)
            logger.log(command_type, command)
            logger.log('response', response)

            # tscore = t.get_score()
            # if tscore is not None:
            #     (score, possible_score) = tscore
            #     if score > max_score:
            #         agent.save_path()
            #     max_score = max(max_score, score)
            #     if not quiet:
            #         print("Score:", score, "Possible score:", possible_score)

            #     logger.log('score', str(score) + ' ' + str(possible_score))
            #     noneCount = 0
            # else:
            #     noneCount += 1
            #     if noneCount > 10:
            #         break
            print("--- ", command, response)
            if "*** The End ***" in response:
                tmp_score = 1
                break
            elif "*** You lost! ***" in response:
                tmp_score = -1
                break

            if quiet:
                print '\r{0}: {1}%, score: {2} / {3}'.format(
                    filename, (i + 1) * 100 / steps, score, possible_score),

        if not quiet:
            agent.map.update()
            agent.map.print_all()
            for _ in range(5):
                agent.map.update()
                print 'number of places:', len(agent.map.edges)
            for k, v in agent.places.items():
                print k
                print 'score:', v.score()

        if score != max_score:
            agent.run_best_path()
            tscore = t.get_score()
            if tscore is not None:
                (score, possible_score) = tscore
                max_score = max(max_score, score)
                if not quiet:
                    print("Score:", score, "Possible score:", possible_score)

        t.quit()

    except IOError:
        if not quiet:
            print "IOError"
        pass

    scores.write("{3} {0} (max {2}) / {1} (step {4}, score {5})\n".format(
        score, possible_score, max_score, filename, step_taken, tmp_score))
    if quiet:
        print '\r{0}: finished, score: {1} / {3}, max score: {2} / {3} (step {4}, score {5})'.format(
            filename, score, max_score, possible_score, step_taken, tmp_score)
    else:
        print 'final score:', score
        print 'max score:', max_score

    return (score + max_score) / 2.0
Ejemplo n.º 15
0
def main():
    if len(sys.argv) > 3:
        file_name = "logs/raw_data/"
        file_name += sys.argv[2]
        config_name = "configs/" + sys.argv[3]
    elif len(sys.argv) > 1:
        file_name = "logs/raw_data/log_file.txt"
    else:
        print("Not enough arguments")
        sys.exit(0)

    print(config_name)
    cFile = open(config_name, "r")
    #cFile = open("configs_extras.txt", "r")
    log_file = logger.open_log(file_name)
    configs = list()

    # construct a list of config arrays representing all tha arms
    for line in cFile:
        config = numpy.asarray([int(n) for n in line.split()])
        configs.append(config)

    # retrieve list of students for this run
    students = sim.get_student_list(log_file)
    #students = list()
    #students.append(sim.Student(0, 0, 0, 1, 1, "Test", 1.0))


    if (int(sys.argv[1]) == 0):
        epsilon = 0.05
        bound = 100000
        if (len(sys.argv) > 4):
            epsilon = float(sys.argv[4])
        if (len(sys.argv) > 5):
            bound = int(sys.argv[5])
        message = "Running epsilon greedy algorithm with epsilon = %f," \
                " and bounded by %d pulls...\n" %(epsilon, bound)
        print(message)
        log_file.write(message)
        best_arm = egreedy.epsilon_greedy(students, configs,
                bound, epsilon, log_file)

    elif (int(sys.argv[1]) == 1):
        conf = 0.95
        e = 0.01 #epsilon
        c_e = ((2+e)/2) * ((1/math.log(1+e))** ( 1 + e)) # C sub epsilon
        beta = 1
        lambda_ = 9
        bound = 100000
        delta = (((math.sqrt(1 + (conf/2)) - 1)**2)/(4*c_e))

        if (len(sys.argv) > 4):
            bound = int(sys.argv[4])

        message = "Running lil-UCB algorithm with epsilon: %.3f, confidence:" \
                " %.3f, beta: %d, lambda: %d...\n" %( e, conf, beta, lambda_)
        print(message)
        log_file.write(message)
        best_arm = lil_ucb.lil_ucb(students, configs, delta,
                e, lambda_, beta , 1-conf, log_file, bound)
    else:
        bound = 50000
        if (len(sys.argv) > 4):
            bound = int(sys.argv[4])
        message = "Running sequential-halving algorithm bounded " \
                "by %d pulls...\n" %(bound)
        print(message)
        log_file.write(message)
        best_arm = sequential_halving.sequential_halving(students,
                configs, bound, log_file)

    log_file.write("--- END OF EXPERIMENT ---\n\n")
    logger.close_log(log_file)