Exemplo n.º 1
0
def importTasks():

# ---  Read tasks from files.	
	global taskList,resourceFileName,taskFileName,dependencyFileName
	global resources
	fptasks = open(taskFileName,"r")#"tasks.txt","r")

	for line in fptasks.readlines():		
		newTask = Task()
		t = line.split(",")	
		newTask.name = t[1]		
		newTask.id = int(t[0])
		newTask.duration = int(t[2])
		adjacencyList[newTask.id] = []
		taskList.append(newTask)	
	fptasks.close()			

#---- Read resources
	fpresources = open(resourceFileName,"r")#("resources.txt","r")
	
	for line in fpresources.readlines():
		r = line.strip().split(",")
		newResource = Resource(int(r[0]),r[1])
		resources += [newResource]
				
	fpresources.close()

# ---- Read depedencies and create predecessor list -----
	fpdependencies = open(dependencyFileName,"r")#("dependencies.txt","r")	

	for line in fpdependencies.readlines():
		line = line.rstrip()
		print(line)		
		x = line.split(",")
		if(len(x)>1):
			task = getTaskFromId(int(x[0]))
			for i in range(1,len(x)):
				task.predecessors.append(int(x[i]))
		"""if len(x) >1:			
			z = x[1]		
			for task in taskList:
				if task.id == int(x[0]):			 					
					y = z.strip().split(",")	
					for b in y:
						task.predecessors.append(int(b))"""								
				#print(x[0],task.predecessor)
	fpdependencies.close()


# ---- Create successor list ----
	for succTask in taskList:
		#succTask = getTaskFromId()
		for predId in succTask.predecessors:
			task = getTaskFromId(predId)
			task.successors.append(succTask.id)
 			adjacencyList[task.id] = task.successors
Exemplo n.º 2
0
def create_agents_and_tasks(num_agents: int, num_tasks: int, WorldInfoInput: WorldInfo, config_data):
    """
    Generate agents and tasks based on a json configuration file.

    config_data:
        config_file_name = "config.json"
        json_file = open(config_file_name)
        config_data = json.load(json_file)

    """

    # Create some default agents

    # quad
    agent_quad_default = Agent()
    # agent type
    agent_quad_default.agent_type = config_data["AGENT_TYPES"].index("quad")
    # agent cruise velocity (m/s)
    agent_quad_default.nom_velocity = float(config_data["QUAD_DEFAULT"]["NOM_VELOCITY"])
    # agent fuel penalty (per meter)
    agent_quad_default.fuel = float(config_data["QUAD_DEFAULT"]["FUEL"])

    # car
    agent_car_default = Agent()
    # agent type
    agent_car_default.agent_type = config_data["AGENT_TYPES"].index("car")
    # agent cruise velocity (m/s)
    agent_car_default.nom_velocity = float(config_data["CAR_DEFAULT"]["NOM_VELOCITY"])
    # agent fuel penalty (per meter)
    agent_car_default.fuel = float(config_data["CAR_DEFAULT"]["FUEL"])


    # Create some default tasks

    # Track
    task_track_default = Task()
    # task type
    task_track_default.task_type = config_data["TASK_TYPES"].index("track")
    # task reward
    task_track_default.task_value = float(config_data["TRACK_DEFAULT"]["TASK_VALUE"])
    # task start time (sec)
    task_track_default.start_time = float(config_data["TRACK_DEFAULT"]["START_TIME"])
    # task expiry time (sec)
    task_track_default.end_time = float(config_data["TRACK_DEFAULT"]["END_TIME"])
    # task default duration (sec)
    task_track_default.duration = float(config_data["TRACK_DEFAULT"]["DURATION"])

    # Rescue
    task_rescue_default = Task()
    # task type
    task_rescue_default.task_type = config_data["TASK_TYPES"].index("rescue")
    # task reward
    task_rescue_default.task_value = float(config_data["RESCUE_DEFAULT"]["TASK_VALUE"])
    # task start time (sec)
    task_rescue_default.start_time = float(config_data["RESCUE_DEFAULT"]["START_TIME"])
    # task expiry time (sec)
    task_rescue_default.end_time = float(config_data["RESCUE_DEFAULT"]["END_TIME"])
    # task default duration (sec)
    task_rescue_default.duration = float(config_data["RESCUE_DEFAULT"]["DURATION"])

    # create empty list, each element is a dataclass Agent() or Task()
    AgentList = []
    TaskList = []

    # create random agents
    for idx_agent in range(0, num_agents):
        # create a new instance of dataclass agent_quad_default
        if (idx_agent/num_agents <= 0.5):
            AgentList.append(Agent(**agent_quad_default.__dict__))
        else:
            AgentList.append(Agent(**agent_car_default.__dict__))

        # AgentList.append(Agent(**agent_quad_default.__dict__))
        AgentList[idx_agent].agent_id = idx_agent
        AgentList[idx_agent].x = random.uniform(WorldInfoInput.limit_x[0], WorldInfoInput.limit_x[1])
        AgentList[idx_agent].y = random.uniform(WorldInfoInput.limit_y[0], WorldInfoInput.limit_y[1])
        AgentList[idx_agent].z = 0

    # create random tasks (track only)
    for idx_task in range(0, num_tasks):
        # create a new instance of dataclass task_track_default
        if (idx_task/num_tasks <= 0.5):
            TaskList.append(Task(**task_track_default.__dict__))
        else:
            TaskList.append(Task(**task_rescue_default.__dict__))
        
        TaskList[idx_task].task_id = idx_task
        TaskList[idx_task].x = random.uniform(WorldInfoInput.limit_x[0], WorldInfoInput.limit_x[1])
        TaskList[idx_task].y = random.uniform(WorldInfoInput.limit_y[0], WorldInfoInput.limit_y[1])
        TaskList[idx_task].z = 0
        TaskList[idx_task].start_time = random.uniform( 0,
            max(float(config_data["TRACK_DEFAULT"]["END_TIME"]), float(config_data["RESCUE_DEFAULT"]["END_TIME"])) - \
            max(float(config_data["TRACK_DEFAULT"]["DURATION"]), float(config_data["RESCUE_DEFAULT"]["DURATION"])) )
        TaskList[idx_task].end_time = TaskList[idx_task].start_time + TaskList[idx_task].duration

    for n in range(0,num_tasks):
        print("Task " + str(n))
        print(str(TaskList[n].x)+", "+str(TaskList[n].y)+", "+str(TaskList[n].z))
        print(str(TaskList[n].start_time)+" - "+str(TaskList[n].end_time))
    for m in range(0,num_agents):
        print("Agent " + str(n))
        print(str(AgentList[m].x)+", "+str(AgentList[m].y)+", "+str(AgentList[m].z))

    return AgentList, TaskList