TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
upc.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include "buildtype.h"
11 #include "cherrypi.h"
12 
13 #include <autogradpp/autograd.h>
14 #include <mapbox/variant.hpp>
15 
16 #include <unordered_map>
17 
18 namespace cherrypi {
19 
20 class State;
21 struct Area;
22 struct BuildType;
23 struct Unit;
24 
25 /**
26  * (Unit, Position, Command) tuple.
27  * Specifies the (who, where, what) of an action
28  *
29  * UPCTuples are used for module-to-module communication (via the Blackboard).
30  * Posting a UPCTuple to the Blackboard is akin to requesting a specific action
31  * from another module, and consuming a UPCTuple from the Blackboard implies
32  * that the consumer implements this action. The implementation can also consist
33  * of refining the UPCTuple so that a more lower-level module can actually
34  * execute it.
35  *
36  * For example, a build order module might post a UPCTuple to create a certain
37  * unit type, and a builder module might wait until their are sufficient
38  * resources before consuming it and then select a worker and a location.
39  *
40  * The UPCToCommandModule translates executable UPCTuples (ones with
41  * sharp unit, position and command entries) to actual game commands.
42  */
43 struct UPCTuple {
44  /// An empty default item for the variants defined below
45  using Empty = char; // Any better options here?
46  using UnitMap = std::unordered_map<Unit*, float>;
47  using CommandMap = std::unordered_map<Command, float>;
48  using BuildTypeMap = std::unordered_map<BuildType const*, float>;
49 
50  using SetCreatePriorityState = std::tuple<UpcId, float>;
51 
52  // Using variant for some types but not others is not very consistent, but it
53  // simplifies code working with UPCTuples.
54 
55  /// A typedef for the unit distribution.
56  using UnitT = UnitMap;
57  /// A typedef for the position distribution.
58  /// Possible values:
59  /// - Empty: unspecified position, i.e. uniform over the entire map
60  /// - Position: A single x/y position with probability of one.
61  /// This is more efficient then having a one-hot position tensor.
62  /// - Area*: An area so that every position in this area has probability
63  /// 1/(walktiles in area), and every position outside has probability 0.
64  /// - UnitMap: A distribution over units which can be used instead of
65  /// position. Sometimes it is more efficient to specify target units
66  /// directly (e.g. for resource mining or for attacks).
67  /// - torch::Tensor: A distribution over the whole map. First dimension is Y,
68  /// second is X.
69  using PositionT =
70  mapbox::util::variant<Empty, Position, Area*, UnitMap, torch::Tensor>;
71  /// A typedef for the command distribution
73  /// A typedef for additional structured information ("state").
74  /// Possible values:
75  /// - Empty: unspecified state
76  /// - BuildTypeMap: A distribution over unit types. This is useful for UPCs
77  /// with the "Create" command.
78  /// - std::string: An arbitrary string.
79  /// - Position: An arbitrary position.
80  /// - torch::Tensor: An arbitrary tensor.
81  using StateT = mapbox::util::variant<
82  Empty,
84  std::string,
85  Position,
87  torch::Tensor>;
88 
89  /// A distribution over units that we can control.
91  /// A distribution over positions.
93  /// A distribution over abstract game commands.
95  /// An auxiliary state that can be used to pass additional information.
96  /// Interpretation of this is entirely context-dependent.
98 
99  /// Specifies the (inverse) scale of the position tensor or the single sharp
100  /// position.
101  /// For efficiency reasons, position tensors can be specified at a coarser
102  /// resolution.
103  int scale = 1;
104 
105  /* Methods */
106 
107  /// Returns argmax and probability of position distribution
108  std::pair<Position, float> positionArgMax() const;
109 
110  /// Returns argmax and probability of position distribution over units
111  std::pair<Unit*, float> positionUArgMax() const;
112 
113  /// Returns the probability of a given position
114  float positionProb(int x, int y) const;
115 
116  /// Returns the probability of a given command
117  float commandProb(Command c) const;
118 
119  /// Returns walk tile resolution tensor of position probabilities
120  torch::Tensor positionTensor(State* state) const;
121 
122  /// Returns argmax and probability of the BuildTypeMap distribution in the
123  /// UPC's `state` field.
124  /// If the UPC specifies a different type of state, this function will return
125  /// `(nullptr, 0.0f)`
126  std::pair<BuildType const*, float> createTypeArgMax() const;
127 
128  /// Creates a uniform distribution over all game commands.
129  static CommandT uniformCommand();
130 };
131 
132 /**
133  * Represents a decision of how to control a unit.
134  */
135 struct MicroAction {
136  /// If true:
137  /// * This MicroAction's UPC is the final decision on what command to issue
138  /// this unit. If the UPC is null, the decision is to issue no command.
139  ///
140  /// If false:
141  /// * This MicroAction represents the absence of a decision on what a unit
142  /// should do. Allow downstream entities to make a decision of what to do.
143  bool isFinal = false;
144 
145  /// if (isFinal):
146  /// * This UPC is the final decision on what this unit should do.
147  ///
148  /// if (!isFinal):
149  /// * This UPC should be ignored.
150  std::shared_ptr<UPCTuple> upc = nullptr;
151 
152  /// if (isFinal):
153  /// * Returns the UPC, which may be null.
154  ///
155  /// if (!isFinal):
156  /// * The UPC doesn't matter; returns null.
157  std::shared_ptr<UPCTuple> getFinalUPC() const {
158  return isFinal ? upc : nullptr;
159  }
160 };
161 
162 } // namespace cherrypi
Game state.
Definition: state.h:42
std::pair< BuildType const *, float > createTypeArgMax() const
Returns argmax and probability of the BuildTypeMap distribution in the UPC&#39;s state field...
Definition: upc.cpp:172
CommandT command
A distribution over abstract game commands.
Definition: upc.h:94
Command
Abstract "meta" commands for UPCTuples.
Definition: basetypes.h:314
std::pair< Unit *, float > positionUArgMax() const
Returns argmax and probability of position distribution over units.
Definition: upc.cpp:34
int scale
Specifies the (inverse) scale of the position tensor or the single sharp position.
Definition: upc.h:103
std::tuple< UpcId, float > SetCreatePriorityState
Definition: upc.h:50
replayer::Unit Unit
Definition: state.h:36
std::unordered_map< BuildType const *, float > BuildTypeMap
Definition: upc.h:48
float positionProb(int x, int y) const
Returns the probability of a given position.
Definition: upc.cpp:49
(Unit, Position, Command) tuple.
Definition: upc.h:43
Represents a decision of how to control a unit.
Definition: upc.h:135
char Empty
An empty default item for the variants defined below.
Definition: upc.h:45
UnitT unit
A distribution over units that we can control.
Definition: upc.h:90
mapbox::util::variant< Empty, BuildTypeMap, std::string, Position, SetCreatePriorityState, torch::Tensor > StateT
A typedef for additional structured information ("state").
Definition: upc.h:87
UnitMap UnitT
A typedef for the unit distribution.
Definition: upc.h:56
std::unordered_map< Unit *, float > UnitMap
Definition: upc.h:46
static CommandT uniformCommand()
Creates a uniform distribution over all game commands.
Definition: upc.cpp:187
std::pair< Position, float > positionArgMax() const
Returns argmax and probability of position distribution.
Definition: upc.cpp:15
std::shared_ptr< UPCTuple > getFinalUPC() const
if (isFinal):
Definition: upc.h:157
float commandProb(Command c) const
Returns the probability of a given command.
Definition: upc.cpp:101
Main namespace for bot-related code.
Definition: areainfo.cpp:17
torch::Tensor positionTensor(State *state) const
Returns walk tile resolution tensor of position probabilities.
Definition: upc.cpp:109
StateT state
An auxiliary state that can be used to pass additional information.
Definition: upc.h:97
CommandMap CommandT
A typedef for the command distribution.
Definition: upc.h:72
std::unordered_map< Command, float > CommandMap
Definition: upc.h:47
PositionT position
A distribution over positions.
Definition: upc.h:92
Vec2T< int > Position
Definition: basetypes.h:178
mapbox::util::variant< Empty, Position, Area *, UnitMap, torch::Tensor > PositionT
A typedef for the position distribution.
Definition: upc.h:70