Wrappers
envs.freeciv_wrapper.tensor_wrapper.TensorWrapper
Bases: Wrapper
TensorWrapper is used to make Civrealm environment tensorized by converting observations from FreecivBaseEnv into tensors and tensor actions back to actions compatible with FreecivBaseEnv.
TensorWrapper is composed TensorBase
, TensorAction
, TensorObservation
and CacheLastObs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env |
FreecivBaseEnv
|
|
required |
config |
dict
|
tensor env configuration |
default_tensor_config
|
Attributes:
Name | Type | Description |
---|---|---|
config |
dict
|
tensor wrapper configuration |
Source code in src/civrealm/envs/freeciv_wrapper/tensor_wrapper.py
envs.freeciv_wrapper.tensor_wrapper.TensorBase
Bases: Wrapper
A basic wrapper that deals with config loading and entity id recording, required by all tensor-related wrappers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env |
FreecivBaseEnv
|
|
required |
config |
dict
|
tensor env configuration |
default_tensor_config
|
Attributes:
Name | Type | Description |
---|---|---|
config |
dict
|
A dict that specifies all configurations related to tensor wrapper. |
my_player_id |
int
|
My player id. |
unit_ids |
list
|
A sorted list of my unit ids. |
city_ids |
list
|
A sorted list of my city ids. |
others_unit_ids |
list
|
A sorted list of others unit ids. |
others_city_ids |
list
|
A sorted list of others city ids. |
dipl_ids |
list
|
A list of others player ids. |
units |
dict
|
ruleset information about units. |
unit_types |
list
|
A list of all unit types. |
unit_costs |
list
|
A list of int indicating unit costs. |
improvements |
dict
|
Ruleset information about city improvements. |
impr_costs |
list
|
A list of int indicating city improvements costs. |
Source code in src/civrealm/envs/freeciv_wrapper/tensor_base_wrapper.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
update_config()
Update config using ruleset information at the start of the turn.
Source code in src/civrealm/envs/freeciv_wrapper/tensor_base_wrapper.py
update_sequence_ids(observation)
Use city, unit and dipl information in observation to update ids.
Source code in src/civrealm/envs/freeciv_wrapper/tensor_base_wrapper.py
envs.freeciv_wrapper.action_wrapper.TensorAction
Bases: Wrapper
A wrapper that defines tensor action spaces, transforms tensor actions into actions that could be handeled by FreecivBaseEnv instance, and adds masks to observations.
TensorAction wrapper is composed of five wrappers, including TruncateDiplCity
,
DiplomacyLoop
, CombineTechResearchGoal
, PersistentCityProduction
, and EmbarkWrapper
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env |
TensorBase
|
A FreecivBaseEnv instance that has been wrapped by TensorBase. |
required |
Attributes:
Name | Type | Description |
---|---|---|
aciton_config |
dict
|
a dict that configs that specify sizes of mutable entities and action layout. |
mask |
dict
|
a dict of masks of type numpy ndarray indicating available actions and entities. 0-> unavilalbe, 1->availble. |
available_actions |
dict
|
cached info['available_actions'], a dict that indicates available actions. |
action_space |
Dict
|
a gymnasium.spaces.Dict with keys |
Source code in src/civrealm/envs/freeciv_wrapper/action_wrapper.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
action(action)
Translate tensor action, a dict of keys ['actor_type','city_id','unit_id',
'dipl_id','city_action_type','unit_action_type','dipl_action_type',
'gov_action_type','tech_action_type']
to FreecivBaseEnv
action,
a tuple (actor_type, entity_id, action_name)
.
Source code in src/civrealm/envs/freeciv_wrapper/action_wrapper.py
reset_mask()
Reset self.mask
This is usually called at the start of a new turn to reset masks.
Source code in src/civrealm/envs/freeciv_wrapper/action_wrapper.py
update_obs_with_mask(observation, info, action=None)
Update self.mask using observation, info and action from the unwrapped env, and add self.mask to the observation of the wrapped env.
Source code in src/civrealm/envs/freeciv_wrapper/action_wrapper.py
envs.freeciv_wrapper.action_wrapper.EmbarkWrapper
Bases: Wrapper
Unify embark actions of all units to 'embark_{dir8}' where dir8 in [0,...7]
indicating 8 directions.
Sometimes a unit can embark multiple carrier on the same direction. In that case, the wrapper automatically choose the carrier with the smallest unit id.
Attributes:
Name | Type | Description |
---|---|---|
embarkable_units |
dict
|
a dict of embarkable units with key=(embarking_unit_id, dir8) and value=[carrier_ids] |
Source code in src/civrealm/envs/freeciv_wrapper/embark_wrapper.py
action(action)
Translate embark_{dir8}
action into embark actions that can be handled by FreecivBaseEnv.
Source code in src/civrealm/envs/freeciv_wrapper/embark_wrapper.py
info(info)
Complete or modify embark actions in info['availble_actions']['unit']
If a unit has no embark_.*
action, then set all embark_{dir8}
action to False
If a unit has embark_{dir}=True
, set all embark_{other_dirs}
action to False
If a unit has embark_{carrier_id}_{dir}=True
, store that carrier_id
and set its embark_{dir8}
accordingly.
Source code in src/civrealm/envs/freeciv_wrapper/embark_wrapper.py
envs.freeciv_wrapper.observation_wrapper.TensorObservation
Bases: Wrapper
A wrapper that defines tensor observation space, transforms observations got from FreecivBaseEnv into tensor observations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env |
TensorBase
|
A FreecivBaseEnv wrapped by TensorBase wrapper |
required |
Attributes:
Name | Type | Description |
---|---|---|
observation_config |
dict
|
tensor observation configuration |
observation_space |
Dict
|
a gymnasium.spaces.Dict with keys speficified in configuration;
observation with keywords |
obs_initialized |
bool
|
whether observation spaces has been initialized |
obs_layout |
dict
|
a dict that specify shapes of flattened numpy arrays in observation |
Source code in src/civrealm/envs/freeciv_wrapper/observation_wrapper.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
observation(observation)
convert observations obtained from FreecivBaseEnv
into a dict of flattend numpy arrays.
Source code in src/civrealm/envs/freeciv_wrapper/observation_wrapper.py
envs.freeciv_wrapper.observation_wrapper.CacheLastObs
Bases: Wrapper
Cache last observation, and override observation with cached observation if terminated or truncated.
Attributes:
Name | Type | Description |
---|---|---|
cached_last_obs |
dict
|
observation cached from the last call of step() or reset() |
Source code in src/civrealm/envs/freeciv_wrapper/observation_wrapper.py
envs.freeciv_wrapper.llm_wrapper.LLMWrapper
Bases: Wrapper
A wrapper for llm. It tells the surrounding observations of each unit and city extracted from FreecivBaseEnv, based on which llm can generate actions for units and cities. It transforms action_keys of actions from FreecivBaseEnv to readable action_names such that llm can understand. After llm have chosen an action and return the action_name to env, this wrapper transforms the action_name to an action_key, and then execute the corresponding action.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env |
FreecivBaseEnv
|
A FreecivBaseEnv |
required |
Attributes:
Name | Type | Description |
---|---|---|
llm_default_settings |
dict
|
settings for llm_wrapper. |
action_names |
dict
|
a dict matches action_keys from FreecivBaseEnv to readable action_names |
tile_length_radius |
int
|
(length of a tile - 1) / 2 |
tile_width_radius |
int
|
(width of a tile - 1) / 2 |
tile_info_template |
dict
|
a dict describes detailed surrounding observations of a unit or a city |
block_length_radius |
int
|
(length of a block - 1) / 2 |
block_width_radius |
int
|
(width of a block - 1) / 2 |
block_info_template |
dict
|
a dict describes zoomed-out surrounding observations of a unit or a city |
ctrl_types |
list
|
a list describes which components of the game to control by llm; we temporarily only consider unit and city in llm_wrapper |
ctrl_action_categories |
dict
|
a dict describes which categories of actions llm can take; it can be seen as an action mask |
Source code in src/civrealm/envs/freeciv_wrapper/llm_wrapper.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
get_actor_info(x, y, obs, info, ctrl_type, actor_id, utype=None)
Convert observations and available actions of a specific actor from FreecivBaseEnv
into a dict of natural language
Source code in src/civrealm/envs/freeciv_wrapper/llm_wrapper.py
get_llm_info(obs, info)
Convert observations and available actions of all actors from FreecivBaseEnv
into a dict of natural language
Source code in src/civrealm/envs/freeciv_wrapper/llm_wrapper.py
get_mini_map_info(x, y, length_r, width_r, template)
Convert observations of a specific actor from FreecivBaseEnv
into a dict of natural language
Source code in src/civrealm/envs/freeciv_wrapper/llm_wrapper.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|