Exemple #1
0
    def step(self, obs):
        super(CollectMineralShards, self).step(obs)

        if (len(self.group_list) == 0
                or common.check_group_list(self.env, [obs])):
            print("init group list")
            obs = common.init(self.env, [obs])[0]
            self.group_list = common.update_group_list([obs])

        #print("group_list ", self.group_list)
        player_relative = obs.observation["screen"][_PLAYER_RELATIVE]
        neutral_y, neutral_x = (player_relative == _PLAYER_NEUTRAL).nonzero()
        player_y, player_x = (player_relative == _PLAYER_FRIENDLY).nonzero()
        if not neutral_y.any() or not player_y.any():
            return actions.FunctionCall(_NO_OP, [])

        r = random.randint(0, 1)

        if _MOVE_SCREEN in obs.observation["available_actions"] and r == 0:

            selected = obs.observation["screen"][_SELECTED]
            player_y, player_x = (selected == _PLAYER_FRIENDLY).nonzero()

            player = [int(player_x.mean()), int(player_y.mean())]
            points = [player]
            closest, min_dist = None, None
            other_dest = None
            my_dest = None
            if ("0" in self.dest_per_marine and "1" in self.dest_per_marine):
                if (self.group_id == 0):
                    my_dest = self.dest_per_marine["0"]
                    other_dest = self.dest_per_marine["1"]
                elif (self.group_id == 1):
                    other_dest = self.dest_per_marine["0"]
                    my_dest = self.dest_per_marine["1"]

            for p in zip(neutral_x, neutral_y):

                if (other_dest):
                    dist = numpy.linalg.norm(
                        numpy.array(other_dest) - numpy.array(p))
                    if (dist < 5):
                        #print("continue since partner will take care of it ", p)
                        continue

                pp = [p[0] // 2 * 2, p[1] // 2 * 2]
                if (pp not in points):
                    points.append(pp)

                dist = numpy.linalg.norm(numpy.array(player) - numpy.array(p))
                if not min_dist or dist < min_dist:
                    closest, min_dist = p, dist

            solve_tsp = False
            if (my_dest):
                dist = numpy.linalg.norm(
                    numpy.array(player) - numpy.array(my_dest))
                if (dist < 2):
                    solve_tsp = True

            if (my_dest is None):
                solve_tsp = True

            if (len(points) < 2):
                solve_tsp = False

            if (solve_tsp):
                # function for printing best found solution when it is found
                from time import clock
                init = clock()

                def report_sol(obj, s=""):
                    print("cpu:%g\tobj:%g\ttour:%s" % \
                          (clock(), obj, s))

                #print("points: %s" % points)
                n, D = mk_matrix(points, distL2)
                # multi-start local search
                #print("random start local search:", n)
                niter = 50
                tour, z = multistart_localsearch(niter, n, D)

                #print("best found solution (%d iterations): z = %g" % (niter, z))
                #print(tour)

                left, right = None, None
                for idx in tour:
                    if (tour[idx] == 0):
                        if (idx == len(tour) - 1):
                            #print("optimal next : ", tour[0])
                            right = points[tour[0]]
                            left = points[tour[idx - 1]]
                        elif (idx == 0):
                            #print("optimal next : ", tour[idx+1])
                            right = points[tour[idx + 1]]
                            left = points[tour[len(tour) - 1]]
                        else:
                            #print("optimal next : ", tour[idx+1])
                            right = points[tour[idx + 1]]
                            left = points[tour[idx - 1]]

                left_d = numpy.linalg.norm(
                    numpy.array(player) - numpy.array(left))
                right_d = numpy.linalg.norm(
                    numpy.array(player) - numpy.array(right))
                if (right_d > left_d):
                    closest = left
                else:
                    closest = right

            #print("optimal next :" , closest)
            self.dest_per_marine[str(self.group_id)] = closest
            #print("dest_per_marine", self.dest_per_marine)
            #dest_per_marine {'0': [56, 26], '1': [52, 6]}

            if (closest is None):
                return actions.FunctionCall(_NO_OP, [])

            return actions.FunctionCall(_MOVE_SCREEN, [_NOT_QUEUED, closest])
        elif (len(self.group_list) > 0):
            player_p = []
            for p in zip(player_x, player_y):
                if (p not in player_p):
                    player_p.append(p)

            self.group_id = random.randint(0, len(self.group_list) - 1)
            return actions.FunctionCall(
                _SELECT_CONTROL_GROUP,
                [[_CONTROL_GROUP_RECALL], [int(self.group_id)]])
        else:
            return actions.FunctionCall(_NO_OP, [])
Exemple #2
0
def solve_tsp(player_relative, selected, group_list, group_id, dest_per_marine,
              xy_per_marine):

    my_dest = None
    other_dest = None
    closest, min_dist = None, None
    actions = []
    neutral_y, neutral_x = (player_relative == 1).nonzero()
    player_y, player_x = (selected == 1).nonzero()

    #for group_id in group_list:
    if "0" in dest_per_marine and "1" in dest_per_marine:
        if group_id == 0:
            my_dest = dest_per_marine["0"]
            other_dest = dest_per_marine["1"]
        else:
            my_dest = dest_per_marine["1"]
            other_dest = dest_per_marine["0"]

    if len(player_x) > 0:
        if group_id == 0:
            xy_per_marine["1"] = [int(player_x.mean()), int(player_y.mean())]
        else:
            xy_per_marine["0"] = [int(player_x.mean()), int(player_y.mean())]

        player = xy_per_marine[str(group_id)]
        points = [player]

        for p in zip(neutral_x, neutral_y):

            if other_dest:
                dist = np.linalg.norm(np.array(other_dest) - np.array(p))
                if dist < 10:
                    # print("continue since partner will take care of it ", p)
                    continue

            pp = [p[0], p[1]]
            if pp not in points:
                points.append(pp)

            dist = np.linalg.norm(np.array(player) - np.array(p))
            if not min_dist or dist < min_dist:
                closest, min_dist = p, dist

        solve_tsp = False
        if my_dest:
            dist = np.linalg.norm(np.array(player) - np.array(my_dest))
            if dist < 0.5:
                solve_tsp = True

        if my_dest is None:
            solve_tsp = True

        if len(points) < 2:
            solve_tsp = False

        if solve_tsp:
            # function for printing best found solution when it is found
            from time import clock
            init = clock()

            def report_sol(obj, s=""):
                print("cpu:%g\tobj:%g\ttour:%s" % \
                      (clock(), obj, s))

            n, D = mk_matrix(points, distL2)
            niter = 50
            tour, z = multistart_localsearch(niter, n, D)

            left, right = None, None
            for idx in tour:
                if tour[idx] == 0:
                    if idx == len(tour) - 1:
                        right = points[tour[0]]
                        left = points[tour[idx - 1]]
                    elif idx == 0:
                        right = points[tour[idx + 1]]
                        left = points[tour[len(tour) - 1]]
                    else:
                        right = points[tour[idx + 1]]
                        left = points[tour[idx - 1]]

            left_d = np.linalg.norm(np.array(player) - np.array(left))
            right_d = np.linalg.norm(np.array(player) - np.array(right))
            if right_d > left_d:
                closest = left
            else:
                closest = right

        #print("optimal next :" , closest)
        dest_per_marine[str(group_id)] = closest
        #print("dest_per_marine", self.dest_per_marine)
        #dest_per_marine {'0': [56, 26], '1': [52, 6]}

        if closest:
            if group_id == 0:
                actions.append({
                    "base_action": group_id,
                    "x0": closest[0],
                    "y0": closest[1]
                })
            else:
                actions.append({
                    "base_action": group_id,
                    "x1": closest[0],
                    "y1": closest[1]
                })

        elif my_dest:
            if group_id == 0:
                actions.append({
                    "base_action": group_id,
                    "x0": my_dest[0],
                    "y0": my_dest[1]
                })
            else:
                actions.append({
                    "base_action": group_id,
                    "x1": my_dest[0],
                    "y1": my_dest[1]
                })
        else:
            if group_id == 0:
                actions.append({"base_action": 2, "x0": 0, "y0": 0})
            else:
                actions.append({"base_action": 2, "x1": 0, "y1": 0})

    # elif(len(group_list)>0):
    #
    #   group_id = random.randint(0,len(group_list)-1)
    #   actions.append({"base_action":group_id})

    if group_id == 0:
        group_id = 1
    else:
        group_id = 0

    if "0" not in xy_per_marine:
        xy_per_marine["0"] = [0, 0]
    if "1" not in xy_per_marine:
        xy_per_marine["1"] = [0, 0]

    return actions, group_id, dest_per_marine, xy_per_marine
Exemple #3
0
def solve_tsp(player_relative, selected, group_list, group_id, dest_per_marine):
  my_dest = None
  other_dest = None
  closest, min_dist = None, None
  actions = []
  neutral_y, neutral_x = (player_relative == 1).nonzero()
  player_y, player_x = (selected == 1).nonzero()

  #for group_id in group_list:
  if("0" in dest_per_marine and "1" in dest_per_marine):
    if(group_id == 0):
      my_dest = dest_per_marine["0"]
      other_dest = dest_per_marine["1"]
    else:
      my_dest = dest_per_marine["1"]
      other_dest = dest_per_marine["0"]

  r = random.randint(0,1)

  if(len(player_x)>0) and r == 0 :

    player = [int(player_x.mean()), int(player_y.mean())]
    points = [player]

    for p in zip(neutral_x, neutral_y):

      if(other_dest):
        dist = np.linalg.norm(np.array(other_dest) - np.array(p))
        if(dist<10):
          # print("continue since partner will take care of it ", p)
          continue

      pp = [p[0]//2*2, p[1]//2*2]
      if(pp not in points):
        points.append(pp)

      dist = np.linalg.norm(np.array(player) - np.array(p))
      if not min_dist or dist < min_dist:
        closest, min_dist = p, dist


    solve_tsp = False
    if(my_dest):
      dist = np.linalg.norm(np.array(player) - np.array(my_dest))
      if(dist < 2):
        solve_tsp = True

    if(my_dest is None):
      solve_tsp = True

    if(len(points)< 2):
      solve_tsp = False

    if(solve_tsp):
      # function for printing best found solution when it is found
      from time import clock
      init = clock()
      def report_sol(obj, s=""):
        print("cpu:%g\tobj:%g\ttour:%s" % \
              (clock(), obj, s))

      #print("points: %s" % points)
      n, D = mk_matrix(points, distL2)
      # multi-start local search
      #print("random start local search:", n)
      niter = 50
      tour,z = multistart_localsearch(niter, n, D)

      #print("best found solution (%d iterations): z = %g" % (niter, z))
      #print(tour)

      left, right = None, None
      for idx in tour:
        if(tour[idx] == 0):
          if(idx == len(tour) - 1):
            #print("optimal next : ", tour[0])
            right = points[tour[0]]
            left = points[tour[idx-1]]
          elif(idx==0):
            #print("optimal next : ", tour[idx+1])
            right = points[tour[idx+1]]
            left = points[tour[len(tour)-1]]
          else:
            #print("optimal next : ", tour[idx+1])
            right = points[tour[idx+1]]
            left = points[tour[idx-1]]

      left_d = np.linalg.norm(np.array(player) - np.array(left))
      right_d = np.linalg.norm(np.array(player) - np.array(right))
      if(right_d > left_d):
        closest = left
      else:
        closest = right

    #print("optimal next :" , closest)
    dest_per_marine[str(group_id)] = closest
    #print("dest_per_marine", self.dest_per_marine)
    #dest_per_marine {'0': [56, 26], '1': [52, 6]}

    if(closest):
      actions.append({"base_action":2,
                      "x0": closest[0], "y0": closest[1]})
  elif(len(group_list)>0):

    group_id = random.randint(0,len(group_list)-1)
    actions.append({"base_action":group_id})
  return actions, group_id, dest_per_marine
Exemple #4
0
def solve_tsp(
    player_relative, 
    selected, 
    group_list, 
    group_id, 
    dest_per_marine,
    xy_per_marine):
  
  my_dest = None
  other_dest = None
  closest, min_dist = None, None
  actions = []
  neutral_y, neutral_x = (player_relative == 1).nonzero()
  player_y, player_x = (selected == 1).nonzero()

  #for group_id in group_list:
  if "0" in dest_per_marine and "1" in dest_per_marine:
    if group_id == 0:
      my_dest = dest_per_marine["0"]
      other_dest = dest_per_marine["1"]
    else:
      my_dest = dest_per_marine["1"]
      other_dest = dest_per_marine["0"]

  if len(player_x) > 0:
    if group_id == 0:
      xy_per_marine["1"] = [int(player_x.mean()), int(player_y.mean())]
    else:
      xy_per_marine["0"] = [int(player_x.mean()), int(player_y.mean())]

    player = xy_per_marine[str(group_id)]
    points = [player]

    for p in zip(neutral_x, neutral_y):

      if other_dest:
        dist = np.linalg.norm(np.array(other_dest) - np.array(p))
        if dist < 10:
          # print("continue since partner will take care of it ", p)
          continue

      pp = [p[0], p[1]]
      if pp not in points:
        points.append(pp)

      dist = np.linalg.norm(np.array(player) - np.array(p))
      if not min_dist or dist < min_dist:
        closest, min_dist = p, dist

    solve_tsp = False
    if my_dest:
      dist = np.linalg.norm(np.array(player) - np.array(my_dest))
      if dist < 0.5:
        solve_tsp = True

    if my_dest is None:
      solve_tsp = True

    if len(points) < 2:
      solve_tsp = False

    if solve_tsp:
      # function for printing best found solution when it is found
      from time import clock
      init = clock()

      def report_sol(obj, s=""):
        print("cpu:%g\tobj:%g\ttour:%s" % \
              (clock(), obj, s))

      n, D = mk_matrix(points, distL2)
      niter = 50
      tour, z = multistart_localsearch(niter, n, D)

      left, right = None, None
      for idx in tour:
        if tour[idx] == 0:
          if idx == len(tour) - 1:
            right = points[tour[0]]
            left = points[tour[idx - 1]]
          elif idx == 0:
            right = points[tour[idx + 1]]
            left = points[tour[len(tour) - 1]]
          else:
            right = points[tour[idx + 1]]
            left = points[tour[idx - 1]]

      left_d = np.linalg.norm(np.array(player) - np.array(left))
      right_d = np.linalg.norm(np.array(player) - np.array(right))
      if right_d > left_d:
        closest = left
      else:
        closest = right

    #print("optimal next :" , closest)
    dest_per_marine[str(group_id)] = closest
    #print("dest_per_marine", self.dest_per_marine)
    #dest_per_marine {'0': [56, 26], '1': [52, 6]}

    if closest:
      if group_id == 0:
        actions.append({
            "base_action": group_id,
            "x0": closest[0],
            "y0": closest[1]
        })
      else:
        actions.append({
            "base_action": group_id,
            "x1": closest[0],
            "y1": closest[1]
        })

    elif my_dest:
      if group_id == 0:
        actions.append({
            "base_action": group_id,
            "x0": my_dest[0],
            "y0": my_dest[1]
        })
      else:
        actions.append({
            "base_action": group_id,
            "x1": my_dest[0],
            "y1": my_dest[1]
        })
    else:
      if group_id == 0:
        actions.append({
            "base_action": 2,
            "x0": 0,
            "y0": 0
        })
      else:
        actions.append({
            "base_action": 2,
            "x1": 0,
            "y1": 0
        })

  # elif(len(group_list)>0):
  #
  #   group_id = random.randint(0,len(group_list)-1)
  #   actions.append({"base_action":group_id})

  if group_id == 0:
    group_id = 1
  else:
    group_id = 0

  if "0" not in xy_per_marine:
    xy_per_marine["0"] = [0, 0]
  if "1" not in xy_per_marine:
    xy_per_marine["1"] = [0, 0]

  return actions, group_id, dest_per_marine, xy_per_marine
  def step(self, obs):
    super(CollectMineralShards, self).step(obs)

    if (len(self.group_list) == 0
        or common.check_group_list(self.env, [obs])):
      print("init group list")
      obs, xy_per_marine = common.init(self.env, [obs])
      obs = obs[0]
      self.group_list = common.update_group_list([obs])

    #print("group_list ", self.group_list)
    player_relative = obs.observation["screen"][_PLAYER_RELATIVE]
    neutral_y, neutral_x = (player_relative == _PLAYER_NEUTRAL).nonzero()
    player_y, player_x = (player_relative == _PLAYER_FRIENDLY).nonzero()
    if not neutral_y.any() or not player_y.any():
      return actions.FunctionCall(_NO_OP, [])

    r = random.randint(0, 1)

    if _MOVE_SCREEN in obs.observation["available_actions"] and r == 0:

      selected = obs.observation["screen"][_SELECTED]
      player_y, player_x = (selected == _PLAYER_FRIENDLY).nonzero()
      if(len(player_x) == 0):
        return actions.FunctionCall(_NO_OP, [])
      
      player = [int(player_x.mean()), int(player_y.mean())]
      points = [player]
      closest, min_dist = None, None
      other_dest = None
      my_dest = None
      if ("0" in self.dest_per_marine and "1" in self.dest_per_marine):
        if (self.group_id == 0):
          my_dest = self.dest_per_marine["0"]
          other_dest = self.dest_per_marine["1"]
        elif (self.group_id == 1):
          other_dest = self.dest_per_marine["0"]
          my_dest = self.dest_per_marine["1"]

      for p in zip(neutral_x, neutral_y):

        if (other_dest):
          dist = numpy.linalg.norm(
            numpy.array(other_dest) - numpy.array(p))
          if (dist < 5):
            #print("continue since partner will take care of it ", p)
            continue

        pp = [p[0], p[1]]
        if (pp not in points):
          points.append(pp)

        dist = numpy.linalg.norm(numpy.array(player) - numpy.array(p))
        if not min_dist or dist < min_dist:
          closest, min_dist = p, dist

      solve_tsp = False
      if (my_dest):
        dist = numpy.linalg.norm(
          numpy.array(player) - numpy.array(my_dest))
        if (dist < 2):
          solve_tsp = True

      if (my_dest is None):
        solve_tsp = True

      if (len(points) < 2):
        solve_tsp = False

      if (solve_tsp):
        # function for printing best found solution when it is found
        from time import clock
        init = clock()

        def report_sol(obj, s=""):
          print("cpu:%g\tobj:%g\ttour:%s" % \
                (clock(), obj, s))

        #print("points: %s" % points)
        n, D = mk_matrix(points, distL2)
        # multi-start local search
        #print("random start local search:", n)
        niter = 50
        tour, z = multistart_localsearch(niter, n, D)

        #print("best found solution (%d iterations): z = %g" % (niter, z))
        #print(tour)

        left, right = None, None
        for idx in tour:
          if (tour[idx] == 0):
            if (idx == len(tour) - 1):
              #print("optimal next : ", tour[0])
              right = points[tour[0]]
              left = points[tour[idx - 1]]
            elif (idx == 0):
              #print("optimal next : ", tour[idx+1])
              right = points[tour[idx + 1]]
              left = points[tour[len(tour) - 1]]
            else:
              #print("optimal next : ", tour[idx+1])
              right = points[tour[idx + 1]]
              left = points[tour[idx - 1]]

        left_d = numpy.linalg.norm(
          numpy.array(player) - numpy.array(left))
        right_d = numpy.linalg.norm(
          numpy.array(player) - numpy.array(right))
        if (right_d > left_d):
          closest = left
        else:
          closest = right

      #print("optimal next :" , closest)
      self.dest_per_marine[str(self.group_id)] = closest
      #print("dest_per_marine", self.dest_per_marine)
      #dest_per_marine {'0': [56, 26], '1': [52, 6]}

      if (closest is None):
        return actions.FunctionCall(_NO_OP, [])

      return actions.FunctionCall(_MOVE_SCREEN, [_NOT_QUEUED, closest])
    elif (len(self.group_list) > 0):
      player_p = []
      for p in zip(player_x, player_y):
        if (p not in player_p):
          player_p.append(p)

      self.group_id = random.randint(0, len(self.group_list) - 1)
      return actions.FunctionCall(
        _SELECT_CONTROL_GROUP,
        [[_CONTROL_GROUP_RECALL], [int(self.group_id)]])
    else:
      return actions.FunctionCall(_NO_OP, [])