Skip to content

Commit

Permalink
(Aegis) finish the parametr refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyangyi committed Oct 26, 2023
1 parent aac5af5 commit 117ccc6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
8 changes: 4 additions & 4 deletions industry/aegis_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import struct
import argparse
from industry.economies import vanilla_temperate, vanilla_subarctic, vanilla_subtropical, firs_temperate, firs_arctic
from industry.lib.parameters import iterate_variations
from industry.lib.parameters import parameter_choices
from industry.lib.validator import validate


Expand All @@ -17,7 +17,7 @@ def initialize_metadata():

# Make up the lists
for meta_economy in all_economies:
for variation in iterate_variations():
for variation in parameter_choices.iterate_variations():
economy = meta_economy.get_economy(variation)
for industry in economy.industries:
if industry not in all_industries:
Expand All @@ -32,7 +32,7 @@ def initialize_metadata():
industry._props["acceptance_types"] = SplitDefinition((0,), {})

for i, meta_economy in enumerate(all_economies):
for variation in iterate_variations():
for variation in parameter_choices.iterate_variations():
economy = meta_economy.get_economy(variation)
for industry in all_industries:
if industry in economy.industries:
Expand Down Expand Up @@ -101,7 +101,7 @@ def main():
gen()
elif args.cmd == "test":
for meta_economy in all_economies:
for variation in iterate_variations():
for variation in parameter_choices.iterate_variations():
economy = meta_economy.get_economy(variation)
try:
validate(economy)
Expand Down
10 changes: 4 additions & 6 deletions industry/docgen/economy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
from industry.lib.parameters import (
docs_parameter_choices,
parameter_choices,
iterate_variations,
parameter_desc,
PRESETS,
)


default_variation = "0" * len(parameter_choices)
default_variation = "0" * len(parameter_choices.choices)


def gen_economy_doc(all_economies, string_manager):
prefix = "docs/industry/economies"
for i, meta_economy in enumerate(all_economies):
for variation in iterate_variations(parameter_choices=docs_parameter_choices):
for variation in docs_parameter_choices.iterate_variations():
economy = meta_economy.get_economy(variation)
variation_desc = economy.parameter_desc
if variation_desc == default_variation:
Expand Down Expand Up @@ -75,7 +73,7 @@ def gen_economy_doc(all_economies, string_manager):

choices_text = []
for preset, preset_params in PRESETS.items():
preset_desc = parameter_desc(preset_params)
preset_desc = parameter_choices.desc(preset_params)
if preset_desc == variation_desc:
choices_text.append(f"{preset}")
else:
Expand All @@ -88,7 +86,7 @@ def gen_economy_doc(all_economies, string_manager):
file=f,
)

for i, (param, choices) in enumerate(docs_parameter_choices):
for i, (param, choices) in enumerate(docs_parameter_choices.choices):
if len(choices) == 1:
continue
choices_text = []
Expand Down
4 changes: 2 additions & 2 deletions industry/lib/economy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ def cargos(self):

@property
def parameter_desc(self):
from industry.lib.parameters import parameter_desc
from industry.lib.parameters import parameter_choices

return parameter_desc(self.parameters)
return parameter_choices.desc(self.parameters)
31 changes: 13 additions & 18 deletions industry/lib/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ def fix_docs_params(self, cat, options):
assert all(o in all_options for o in options)
self.choices[idx] = (cat, options)

def iterate_variations(self, i=0, params={}):
if i == len(self.choices):
yield params
else:
for j in self.choices[i][1]:
new_params = params.copy()
new_params[self.choices[i][0]] = j
for variation in self.iterate_variations(i + 1, new_params):
yield variation

def desc(self, params):
return "".join(str(options.index(params[i])) for i, options in self.choices)


parameter_choices = SearchSpace(
[
Expand Down Expand Up @@ -224,9 +237,6 @@ def fix_docs_params(self, cat, options):
docs_parameter_choices.fix_docs_params("TOWN_GOODS", ["ORGANIC"])


parameter_choices = parameter_choices.choices
docs_parameter_choices = docs_parameter_choices.choices

PRESETS = {
"VANILLA": {
"POLICY": "AUTARKY",
Expand Down Expand Up @@ -264,18 +274,3 @@ def fix_docs_params(self, cat, options):
"TOWN_GOODS": "ORGANIC",
},
}


def iterate_variations(i=0, params={}, parameter_choices=parameter_choices):
if i == len(parameter_choices):
yield params
else:
for j in parameter_choices[i][1]:
new_params = params.copy()
new_params[parameter_choices[i][0]] = j
for variation in iterate_variations(i + 1, new_params, parameter_choices=parameter_choices):
yield variation


def parameter_desc(params):
return "".join(str(options.index(params[i])) for i, options in parameter_choices)

0 comments on commit 117ccc6

Please sign in to comment.