Пример #1
0
    def re_create_example(workflows, machines, reset_task_status=False):
        blp_tasks, blp_machines = ExampleGen.blueprint_example(
            workflows, machines)
        blp_tasks = sum(blp_tasks, [])
        new_machines = [
            Machine.blueprint_to_machine(blp_m) for blp_m in blp_machines
        ]
        new_workflows = []
        tasks = []

        for wf_id in range(len(workflows)):
            if reset_task_status is False:
                tasks = [
                    Task.blueprint_to_task(blp_t) for blp_t in blp_tasks
                    if blp_t.wf_id == wf_id
                ]
            else:
                for blp_t in blp_tasks:
                    if blp_t.wf_id == wf_id:
                        blp_t.status = TaskStatus.READY if len(
                            blp_t.parents_names
                        ) == 0 else TaskStatus.UNSCHEDULED
                        tasks.append(Task.blueprint_to_task(blp_t))
            new_workflows.append(
                Workflow.blueprint_to_workflow(wf_id, tasks, new_machines))
        return new_workflows, new_machines
Пример #2
0
	def test_task_delete_also_subtasks_to_database(self):
		"""check if number of rows before and after delete change"""
		# add task & subtasks
		self.model.add()
		subtasks = Task()
		subtasks.name = "subtask_test"
		subtasks.node_id = self.model.id
		subtasks.add()
		subtasks.add()
		# delete and search tasks
		data = {'node_id': self.model.id}
		self.model.delete()
		sql_query = "SELECT COUNT(*) FROM {} WHERE node_id = :node_id".format(self.model.table_name)
		count = self.model.database.cursor.execute( sql_query, data ).fetchone()
		self.assertEqual(count[0], 0)
Пример #3
0
	def _tree(self):
		#I begin to destroy the old treeview if he exist
		for widget in self.tree_holder.winfo_children():
			widget.destroy()
		
		self.tree = ttk.Treeview(self.tree_holder)

		self.tree["columns"]=("id","time")
		self.tree["displaycolumns"]=("time")
		self.tree.column("time", width=100 )

		self.tree.heading("time", text="time")

		# and each tasks in cascade
		for task in Task.all():
			self.tree.insert( '', 'end', "task_{}".format(task.id), 
				value=(task.id, task.spended_time), text=task.name, 
				tag='status_{}'.format(task.status), open=not bool(task.status))

			if task.node_id != 0:
				self.tree.move("task_{}".format(task.id), "task_{}".format(task.node_id), 'end')

		#I begin to apply different style for different status
		self.tree.tag_configure('status_0', font=Setting.FONT_UNDONE , foreground=Setting.COLOR_UNDONE )
		self.tree.tag_configure('status_1', font=Setting.FONT_DONE , foreground=Setting.COLOR_DONE)

		self.tree.bind('<ButtonRelease-3>' , self.show_context_menu )
		self.tree.bind('<ButtonRelease-1>' , self.show_details )

		self.tree.pack(fill=X , side=TOP)
Пример #4
0
def get_tasks_from_json_file(file_name, wf_id, network_kbps):
    with open(file_name) as f:
        data = json.load(f)

    # Visualize the graph to check it.
    # create_csv_file_to_visualize_graph(data['workflow']['jobs'])
    tasks = []

    # n_tasks = len(data['workflow']['jobs'])
    # 1) Parse first the machines you have to do the workflow. ( we create our machines )
    # 2) Parse the data for the tasks in the: data['workflow']['jobs'] ---> job['files']
    #       For the above dictionary you should sum the file sizes together
    #       to get the overall communication cost.
    for job in data['workflow']['jobs']:
        # Name & id
        # job['name'] <- This can and should be used as an id right away
        job_id = get_id_from_name(job['name'])
        ####################################

        # 3) Create the Task class based on the data you parsed.
        tasks.append(
            Task(id_=job_id,
                 wf_id=wf_id,
                 name=job['name'],
                 costs=[],
                 runtime=job['runtime'],
                 files=job['files'],
                 children_names=job['children'],
                 parents_names=job['parents']))
    for t in tasks:
        t.create_edges(tasks, network_kbps)
    return tasks
Пример #5
0
 def generate_gas_tasks(self):
     if self.worker_assignments.get_available_units(
     ):  # Only generate if there are available workers
         for refinary in self.building_manager.get_buildings_of_type(
                 UnitType(UNIT_TYPEID.TERRAN_REFINERY, self.ida_bot)):
             for i in range(3):
                 self.last_tick_mining_tasks += 1
                 self.worker_assignments.add_task(
                     Task(task_type=TaskType.GAS,
                          pos=refinary.get_unit().position))
Пример #6
0
 def load_small_from_data(best_of='workflows'):
     machines = [
         Machine.blueprint_to_machine(
             MachineBlueprint(m_info[0], m_info[1], m_info[2], CORE_SPEED,
                              m_info[3]))
         for m_info in SMALL_EXAMPLE['machines']
     ]
     return machines, [
         Workflow.blueprint_to_workflow(
             id_=i,
             tasks=[Task.blueprint_to_task(blp_t) for blp_t in blp_tasks],
             machines=machines)
         for i, blp_tasks in enumerate(SMALL_EXAMPLE[best_of])
     ]
Пример #7
0
    def create_random_small():
        machines = [Machine.create_random_machine(i) for i in range(2)]
        workflows = []

        for wf_id in range(N_WORKFLOWS):
            tasks = [Task.create_random_task(t_id, wf_id) for t_id in range(3)]
            tasks[0].is_entry = True
            tasks[0].status = TaskStatus.READY
            tasks[2].is_exit = True
            # Task obj is mutable no need to return something
            ExampleGen.create_edges_for_example(tasks)
            workflows.append(
                Workflow(id_=wf_id,
                         machines=machines,
                         tasks=tasks,
                         wf_type="Random",
                         add_dummies=False))
        return workflows, machines
Пример #8
0
 def generate_mining_tasks(self):
     nr_mining_jobs = len(self.worker_assignments.get_available_units())
     if self.worker_assignments.get_available_units(
     ):  # Only generate if there are available workers
         base_camps = [base for base in self.ida_bot.minerals_in_base]
         while base_camps:
             closest_base = self.get_closest_base(base_camps)
             base_camps.remove(closest_base)
             for i in range(
                     2 * len(self.ida_bot.minerals_in_base[closest_base])):
                 self.worker_assignments.add_task(
                     Task(task_type=TaskType.MINING,
                          pos=Point2D(closest_base.depot_position.x,
                                      closest_base.depot_position.y),
                          base_location=closest_base))
                 nr_mining_jobs -= 1
                 if nr_mining_jobs == 0:
                     return
Пример #9
0
	def add(self):
		"""add a new task"""
		task = self._get_select_item()

		node_id = 0
		try:
			node_id = task.id
		except:
			node_id = 0

		new_task = Task()
		new_task.name = 'no name'
		new_task.description = 'no description'
		new_task.node_id = node_id
		new_task.status = 0
		new_task.add()
		self._tree()
Пример #10
0
def printerSimulation(numSeconds, ppm: int):
    printer = Printer(ppm)
    printQueue = Queue()
    waitingTimes = []

    for currentSecond in range(numSeconds):
        if newPrintTask():
            task = Task(currentSecond)
            printQueue.enqueue(task)

        if (not printer.isBusy()) and (not printQueue.isEmpty()):
            nextTask = printQueue.dequeue()
            waitingTimes.append(nextTask.waitTime(currentSecond))
            printer.next(nextTask)

        printer.tick()

    averageWaitingTime = sum(waitingTimes) / len(waitingTimes)

    print(
        "Average waiting time is {:.^14.2f} secs and {:.>5d} tasks remaining".
        format(averageWaitingTime, printQueue.size()))
    def reached_goal(self, current_frame):
        if self.health > self.unit.hit_points:
            self.health = self.unit.hit_points
            self.attack = True
            self.manager.visited.append(self.goal)
            self.manager.frame_stamps.append(current_frame)
            for cur_goal in self.manager.goals:
                if cur_goal.equal(self.goal):
                    self.manager.goals.remove(cur_goal)
            goal = self.manager.bot.base_location_manager.get_player_starting_base_location(
                player_constant=PLAYER_SELF)
            self.set_goal(goal.position)
            return True
        if self.goal is None:
            return True
        else:
            if self.unit.position.distance(self.goal) < 8:
                self.health = self.unit.hit_points
                self.manager.visited.append(self.goal)
                self.manager.frame_stamps.append(current_frame)
                if self.manager.scouts_requested < 1 and len(
                        self.manager.bot.unit_manager.scout_units) < 2:
                    task_scout = Task(
                        TaskType.SCOUT,
                        pos=self.manager.bot.base_location_manager.
                        get_player_starting_base_location(
                            PLAYER_SELF).position)
                    self.manager.bot.assignment_manager.add_task(task_scout)
                    self.manager.scouts_requested += 1

                for cur_goal in self.manager.goals:
                    if cur_goal.equal(self.goal):
                        self.manager.goals.remove(cur_goal)
                return True
            else:
                return False
Пример #12
0
    def handle_strategy(self):
        """
        Generates jobs depending on our chosen strategy
        """

        curr_seconds = self.current_frame // 24

        # Only look at new strategy and generate new tasks every now and then
        if curr_seconds - self.last_handled_strategy < HANDLE_STRATEGY_DELAY:
            return

        # The previous tasks generated have not yet been assigned, don't create new tasks
        if self.assignment_manager.military_assignments.tasks:
            return

        # Calculate new predicted strategy
        strategy = self.strategy_network.get_strategy()

        # Now handling a strategy decision
        self.last_handled_strategy = curr_seconds

        # Get all of our command centers
        base_location_manager: BaseLocationManager = self.base_location_manager
        commandcenters = self.building_manager.get_total_buildings_of_type(
            UnitType(UNIT_TYPEID.TERRAN_COMMANDCENTER, self))
        #command_centers = self.building_manager.get_buildings_of_type(UnitType(UNIT_TYPEID.TERRAN_COMMANDCENTER, self)) + \
        #                  self.building_manager.get_under_construction_of_type(UnitType(UNIT_TYPEID.TERRAN_COMMANDCENTER, self))
        commandcenters = list(reversed(list(commandcenters)))
        if strategy == StrategyName.OFFENSIVE:
            offensive_groups = 1
            defensive_groups = 0
            if not self.block:
                self.block = True
                self.send_chat(self.messages[0])
            closest_enemy = self.get_closest_enemy_building()
            if not closest_enemy:
                attack_pos = self.scout_manager.get_enemy_target()
            else:
                attack_pos = closest_enemy.position
        else:  # strategy == StrategyName.DEFENSIVE
            offensive_groups = 0
            if len(commandcenters) <= 2:
                defensive_groups = 1
            elif len(commandcenters) <= 3:
                defensive_groups = 2
            else:
                defensive_groups = 3

        # Generate all offensive tasks
        offensive_tasks = [
            Task(task_type=TaskType.ATTACK, pos=attack_pos)
            for i in range(offensive_groups)
        ]

        # Generate all defensive tasks
        defensive_tasks = [
            Task(task_type=TaskType.DEFEND,
                 pos=self.get_choke_point(
                     commandcenters[i].get_unit().position))
            # Loop through all bases we have and
            for i in range(defensive_groups) if commandcenters
        ]

        # Add all generated tasks to assignment_manager
        for task in [*offensive_tasks, *defensive_tasks]:
            self.assignment_manager.add_task(task)
    def add_task(self, action_type):
        built_prerequisites = self.get_built_prerequisites(action_type)
        if not built_prerequisites is None and not built_prerequisites.unit_typeid == UNIT_TYPEID.INVALID:
            self.add_task(built_prerequisites)

        if self.resource_manager.can_afford(action_type):
            location_near = self.idabot.base_location_manager.get_player_starting_base_location(
                PLAYER_SELF).depot_position
            if action_type.unit_typeid == UNIT_TYPEID.TERRAN_COMMANDCENTER:
                build_pos = self.idabot.base_location_manager.get_next_expansion(
                    PLAYER_SELF).depot_position
                build_location = Point2D(build_pos.x, build_pos.y)
            elif action_type.is_building:
                build_pos = self.idabot.our_building_placer.get_build_location(
                    action_type)
                build_pos = Point2DI(int(build_pos.x), int(build_pos.y))
                build_location = Point2D(build_pos.x, build_pos.y)
            task = None

            if action_type.is_worker or action_type.is_combat_unit:
                task = Task(TaskType.TRAIN, produce_unit=action_type)
            elif action_type.is_addon:
                task = Task(TaskType.ADD_ON, construct_building=action_type)
            elif action_type.is_refinery:
                for base_location in reversed(
                        list(
                            self.idabot.base_location_manager.
                            get_occupied_base_locations(PLAYER_SELF))):
                    for geyser in self.idabot.get_geysers(base_location):
                        if self.get_refinery(geyser) is None:
                            task = Task(TaskType.BUILD,
                                        pos=geyser.position,
                                        geyser=geyser,
                                        construct_building=action_type)
                            print("Adding Task: ", task.task_type,
                                  "Action_type: ", action_type)
                            self.assignment_manager.add_task(task)
                            return
                return
            elif action_type.is_building:

                if action_type.unit_typeid in [UNIT_TYPEID.TERRAN_BARRACKS, UNIT_TYPEID.TERRAN_STARPORT,
                                               UNIT_TYPEID.TERRAN_FACTORY] and \
                        len(self.idabot.building_manager.get_total_buildings_of_type(action_type)) >= 3 and self.idabot.minerals < 1000:
                    action_type = self.name_to_type("Cyclone")
                    action = Task(TaskType.TRAIN, produce_unit=action_type)

                if action_type.unit_typeid == UNIT_TYPEID.TERRAN_MISSILETURRET:
                    action_type = self.name_to_type("Marauder")
                    task = Task(TaskType.TRAIN, produce_unit=action_type)

                elif action_type.unit_typeid == UNIT_TYPEID.TERRAN_ENGINEERINGBAY and \
                        len(self.idabot.building_manager.get_total_buildings_of_type(self.name_to_type("EngineeringBay"))) > 0:
                    action_type = self.name_to_type("Marauder")
                    task = Task(TaskType.TRAIN, produce_unit=action_type)
                elif len(self.idabot.building_manager.get_total_buildings_of_type(self.name_to_type("CommandCenter"))) >= self.max_command_centers \
                        and action_type.unit_typeid == UNIT_TYPEID.TERRAN_COMMANDCENTER:
                    action_type = self.name_to_type("Marauder")
                    task = Task(TaskType.TRAIN, produce_unit=action_type)
                else:
                    task = Task(TaskType.BUILD,
                                pos=build_location,
                                build_position=build_pos,
                                construct_building=action_type)

            print("Adding Task: ", task.task_type, "Action_type: ",
                  action_type)
            self.assignment_manager.add_task(task)
Пример #14
0
 def create_task(self, name):
     task = Task(name)
     self.tasks.append(task)
     self.all_tasks.append(task)
     return task
        #TODO: kom på ngt sätt att kunna använda detta när vi redan har grupper och vill lägga till enheter
        self.task_index = len(cs)
        map(lambda x: x.append(1))
        max_value = 0
        best_group = -1
        for i, coalition in enumerate(cs):
            value = self.v(coalition) - self.v(coalition + [unit])
            if value > max_value:
                max_value = value
                best_group = i
        return best_group


if __name__ == '__main__':

    t1 = Task(TaskType(1))
    t2 = Task(TaskType(2))

    csg = CoalitionstructureGenerator()
    b = []
    dict_to_test = {
        "typ 1": [11, 12, 13],
        "typ 2": [21, 22, 23, 24],
        "typ 3": [31, 32, 33],
        "typ 4": [41, 42, 43, 44, 45, 46, 47, 48],
        type(t1): [t1, t2]
    }  #, "typ 6": [61, 62, 63, 64]}
    dict_to_test = {
        "typ1": [11, 12, 13, 14, 15, 16, 17, 18],
        type(t1): [1, 2, 3, 4, 5, 6]
    }
 def ask_for_scout(self):
     task_scout = Task(
         TaskType.SCOUT,
         pos=self.bot.base_location_manager.
         get_player_starting_base_location(PLAYER_SELF).position)
     self.bot.assignment_manager.add_task(task_scout)
Пример #17
0
    Clear screen
    :return:
    """
    if os.system == "nt":
        os.system('cls')
    else:
        os.system('clear')

# Script doesn't execute when imported
if __name__ == '__main__':
    # The script will keep running till the user is satisfied
    while True:
        clear_screen()

        # job to do
        i = Task().job_selected()

        # validation if the input is a number (int)
        # route user to each Class methods
        if isinstance(i, int):

            if 1 <= i <= 3:
                if i == 1:
                    AddTask()
                elif i == 2:
                    clear_screen()
                    SearchTask()
                elif i == 3:
                    clear_screen()
                    print("Thank you for using our Mars Data Log.")
                    break
Пример #18
0
 def generate_task(self):
     task = Task()
     for question in self.questions:
         task.add_question(question)
     self.task = task
Пример #19
0
	def setUp(self):
		Task.database = Database('data/test.sqlite')
		WorkTime.database = Database('data/test.sqlite')
		self.model = Task()
		self.model.name = "task_test"
		self.model.node_id = 0
Пример #20
0
    def load_fixed_small_random():
        machines = [
            Machine.blueprint_to_machine(
                MachineBlueprint(m_info[0], m_info[1], m_info[2], CORE_SPEED,
                                 m_info[3]))
            for m_info in SMALL_EXAMPLE['machines']
        ]

        a_b = randint(1, 100)
        a_c = randint(1, 100)
        a_d = randint(1, 100)
        b_e = randint(1, 100)
        c_e = randint(1, 100)
        d_e = randint(1, 100)
        e_f = randint(1, 100)
        e_g = randint(1, 100)
        f_h = randint(1, 100)
        g_h = randint(1, 100)
        blp_worklows = [[
            TaskBlueprint(0, 0, 'T-A', randint(1, 100), [{
                "w": a_b,
                "n": 'T-B'
            }, {
                "w": a_c,
                "n": 'T-C'
            }, {
                "w": a_d,
                "n": 'T-D'
            }], [], TaskStatus.READY, True, False),
            TaskBlueprint(1, 0, 'T-B', randint(1, 100), [{
                "w": b_e,
                "n": 'T-E'
            }], [{
                "w": a_b,
                "n": 'T-A'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(2, 0, 'T-C', randint(1, 100), [{
                "w": c_e,
                "n": 'T-E'
            }], [{
                "w": a_c,
                "n": 'T-A'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(3, 0, 'T-D', randint(1, 100), [{
                "w": d_e,
                "n": 'T-E'
            }], [{
                "w": a_d,
                "n": 'T-A'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(4, 0, 'T-E', randint(1, 100), [{
                "w": e_f,
                "n": 'T-F'
            }, {
                "w": e_g,
                "n": 'T-G'
            }], [{
                "w": b_e,
                "n": 'T-B'
            }, {
                "w": c_e,
                "n": 'T-C'
            }, {
                "w": d_e,
                "n": 'T-D'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(5, 0, 'T-F', randint(1, 100), [{
                "w": f_h,
                "n": 'T-H'
            }], [{
                "w": e_f,
                "n": 'T-E'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(6, 0, 'T-G', randint(1, 100), [{
                "w": g_h,
                "n": 'T-H'
            }], [{
                "w": e_g,
                "n": 'T-E'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(7, 0, 'T-H', randint(1, 100), [], [{
                "w": f_h,
                "n": 'T-F'
            }, {
                "w": g_h,
                "n": 'T-G'
            }], TaskStatus.UNSCHEDULED, False, True),
        ]]

        _a_t_b = randint(1, 100)
        _b_t_c = randint(1, 100)
        _c_t_d = randint(1, 100)
        blp_worklows.append([
            TaskBlueprint(0, 1, 'T-A', randint(1, 100), [{
                "w": _a_t_b,
                "n": 'T-B'
            }], [], TaskStatus.READY, True, False),
            TaskBlueprint(1, 1, 'T-B', randint(1, 100), [{
                "w": _b_t_c,
                "n": 'T-C'
            }], [{
                "w": _a_t_b,
                "n": 'T-A'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(2, 1, 'T-C', randint(1, 100), [{
                "w": _c_t_d,
                "n": 'T-D'
            }], [{
                "w": _b_t_c,
                "n": 'T-B'
            }], TaskStatus.UNSCHEDULED, False, False),
            TaskBlueprint(3, 1, 'T-D', randint(1, 100), [], [{
                "w": _c_t_d,
                "n": 'T-C'
            }], TaskStatus.UNSCHEDULED, False, True),
        ])
        return machines, [
            Workflow.blueprint_to_workflow(
                id_=i,
                tasks=[Task.blueprint_to_task(blp_t) for blp_t in blp_tasks],
                machines=machines) for i, blp_tasks in enumerate(blp_worklows)
        ]
Пример #21
0
class TaskTest(unittest.TestCase):

	model = None

	def setUp(self):
		Task.database = Database('data/test.sqlite')
		WorkTime.database = Database('data/test.sqlite')
		self.model = Task()
		self.model.name = "task_test"
		self.model.node_id = 0



	def test_check_tables_exists(self):
		"""check if table exists in database with a simple SQL query"""
		sql_query = """SELECT name  FROM sqlite_master 
						WHERE type='table' AND name='{table}'
						""".format(table=Task.table_name)
		self.assertIsNotNone( self.model.database.cursor.execute( sql_query ).fetchone() )



	def test_model_can_be_added_in_database(self):
		"""check number of rows before and after insertion"""
		sql_query = "SELECT COUNT(id) FROM {}".format(self.model.table_name)
		old_count = self.model.database.cursor.execute( sql_query ).fetchone()
		self.model.add()
		new_count = self.model.database.cursor.execute( sql_query ).fetchone()
		self.assertEqual(old_count[0]+1, new_count[0])



	def test_model_can_be_finded(self):
		"""add an object find a random id in database and check if find() method can find it"""
		sql_query = "SELECT id FROM {} LIMIT 1".format( self.model.table_name)
		id = self.model.database.cursor.execute( sql_query ).fetchone()
		sql_result =  self.model.find_by( 'id', id[0] )
		self.assertIsNotNone( sql_result )


	
	def test_model_can_be_deleted_to_database(self):
		"""check if number of rows before and after delete change"""
		sql_query = "SELECT COUNT(id) FROM {}".format(self.model.table_name)
		old_count = self.model.database.cursor.execute( sql_query ).fetchone()
		self.model.add()
		self.model.delete()
		new_count = self.model.database.cursor.execute( sql_query ).fetchone()
		self.assertEqual(old_count[0], new_count[0])



	def test_task_delete_also_subtasks_to_database(self):
		"""check if number of rows before and after delete change"""
		# add task & subtasks
		self.model.add()
		subtasks = Task()
		subtasks.name = "subtask_test"
		subtasks.node_id = self.model.id
		subtasks.add()
		subtasks.add()
		# delete and search tasks
		data = {'node_id': self.model.id}
		self.model.delete()
		sql_query = "SELECT COUNT(*) FROM {} WHERE node_id = :node_id".format(self.model.table_name)
		count = self.model.database.cursor.execute( sql_query, data ).fetchone()
		self.assertEqual(count[0], 0)



	def test_all_method(self):
		"""check if all method return same qty objects than COUNT(*)"""
		sql_query = "SELECT COUNT(*) FROM {}".format(self.model.table_name)
		count = self.model.database.cursor.execute( sql_query ).fetchone()
		model_class = self.model.__class__
		self.assertEqual( count[0] , len(list(model_class.all())))



	def test_worktimes_property(self):
		"""check if wortimes property return same qty objects than COUNT(*)"""
		self.model.add()
		WorkTime(self.model).add()
		WorkTime(self.model).add()
		WorkTime(self.model).add()
		data = {'task_id':self.model.id}
		sql_query = "SELECT COUNT(*) FROM worktimes WHERE task_id=:task_id"
		count = self.model.database.cursor.execute( sql_query, data ).fetchone()
		self.assertEqual( count[0] , len(list(self.model.worktimes)))