Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mandd/gawithcustomsampler #2084

Merged
merged 8 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def getpotToInputTree(getpot):
parentNodes.append(currentNode)
currentNode = TreeStructure.InputNode(tag=line.strip('[]./'))
closeEntry = False

#------------------
# closing node
elif line.startswith(('[../]', '[]')):
Expand Down
14 changes: 8 additions & 6 deletions ravenframework/Optimizers/Optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ def __init__(self):
self._constraintFunctions = [] # list of constraint functions
self._impConstraintFunctions = [] # list of implicit constraint functions
self._requireSolnExport = True # optimizers only produce result in solution export
self.optAssemblerList = ['DataObjects', 'Distributions', 'Functions', 'Files'] # List of assembler entities required to initialize an optmizer
# __private
# additional methods
self.addAssemblerObject('Constraint', InputData.Quantity.zero_to_infinity) # Explicit (input-based) constraints
self.addAssemblerObject('ImplicitConstraint', InputData.Quantity.zero_to_infinity) # Implicit constraints
self.addAssemblerObject('Sampler', InputData.Quantity.zero_to_one) # This Sampler can be used to initialize the optimization initial points (e.g. partially replace the <initial> blocks for some variables)


# register adaptive sample identification criteria
self.registerIdentifier('traj') # the trajectory of interest

Expand Down Expand Up @@ -208,7 +210,8 @@ def _localGenerateAssembler(self, initDict):
self.assemblerDict['DataObjects'] = []
self.assemblerDict['Distributions'] = []
self.assemblerDict['Functions'] = []
for mainClass in ['DataObjects', 'Distributions', 'Functions']:
self.assemblerDict['Files'] = []
for mainClass in self.optAssemblerList:
for funct in initDict[mainClass]:
self.assemblerDict[mainClass].append([mainClass,
initDict[mainClass][funct].type,
Expand All @@ -217,7 +220,7 @@ def _localGenerateAssembler(self, initDict):

def localInputAndChecks(self, xmlNode, paramInput):
"""
unfortunately-named method that serves as a pass-through for input reading.
Method that serves as a pass-through for input reading.
comes from inheriting from Sampler and _readMoreXML chain.
@ In, xmlNode, xml.etree.ElementTree.Element, xml element node (don't use!)
@ In, paramInput, InputData.ParameterInput, parameter specs interpreted
Expand All @@ -234,9 +237,8 @@ def _localWhatDoINeed(self):
@ Out, needDict, dict, list of objects needed
"""
needDict = {}
needDict['Distributions'] = [(None,'all')]
needDict['Functions' ] = [(None,'all')]
needDict['DataObjects' ] = [(None,'all')]
for elem in self.optAssemblerList:
needDict[elem] = [(None,'all')]

return needDict

Expand Down Expand Up @@ -401,7 +403,7 @@ def _initializeInitSampler(self, externalSeeding):
self._initSampler = sampler
# initialize sampler
samplerInit = {}
for entity in ['Distributions', 'Functions', 'DataObjects']:
for entity in self.optAssemblerList:
samplerInit[entity] = dict((entry[2], entry[3]) for entry in self.assemblerDict.get(entity, []))
self._initSampler._localGenerateAssembler(samplerInit)
# assure sampler provides useful info
Expand Down
2 changes: 1 addition & 1 deletion ravenframework/Samplers/CustomSampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _localWhatDoINeed(self):
This method is a local mirror of the general whatDoINeed method.
It is implemented by the samplers that need to request special objects
@ In, None
@ Out, needDict, dict, list of objects needed (in this case it is empty, since no distrubtions are needed and the Source is loaded automatically)
@ Out, needDict, dict, list of objects needed (in this case it is empty, since no distribtions are needed and the Source is loaded automatically)
"""
needDict = {}
needDict['Functions'] = [] # In case functions have been inputted
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
x1,x2,x3
6,6,6
2,2,6
4,5,3
3,4,2
2,6,1
1,2,4
2,2,2
4,4,3
2,6,6
2,1,3
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0" ?>
<Simulation verbosity="debug">
<TestInfo>
<name>framework/Optimizers/GA.MaxwReplacementCustomSampler</name>
<author>mandd</author>
<created>2022-02-24</created>
<classesTested>GeneticAlgorithm</classesTested>
<description>
This test assesses the Genetic algorithm when a custom sampler (initialized by file) is used to
initialize the initial population
</description>
<analytic>
This test uses myLocalSum's analytic objective function.
</analytic>
</TestInfo>

<RunInfo>
<WorkingDir>MaxwReplacement_customsampler</WorkingDir>
<Sequence>optimize, print</Sequence>
<batchSize>1</batchSize>
</RunInfo>

<Files>
<Input name="restart.csv" type="">restart.csv</Input>
</Files>

<Steps>
<MultiRun name="optimize" re-seeding="2286">
<Input class="DataObjects" type="PointSet">placeholder</Input>
<Model class="Models" type="ExternalModel">myLocalSum</Model>
<Optimizer class="Optimizers" type="GeneticAlgorithm">GAopt</Optimizer>
<SolutionExport class="DataObjects" type="PointSet">opt_export</SolutionExport>
<Output class="DataObjects" type="PointSet">optOut</Output>
<Output class="OutStreams" type="Print">opt_export</Output>
</MultiRun>
<IOStep name="print">
<Input class="DataObjects" type="PointSet">opt_export</Input>
<Input class="DataObjects" type="PointSet">optOut</Input>
<Output class="OutStreams" type="Print">opt_export</Output>
<Output class="OutStreams" type="Print">optOut</Output>
<Output class="OutStreams" type="Plot">opt_path</Output>
</IOStep>
</Steps>

<Distributions>
<UniformDiscrete name='uniform_dist_Repl_1'>
<lowerBound>1</lowerBound>
<upperBound>6</upperBound>
<strategy>withReplacement</strategy>
</UniformDiscrete>
<UniformDiscrete name='uniform_dist_woRepl_1'>
<lowerBound>1</lowerBound>
<upperBound>6</upperBound>
<strategy>withoutReplacement</strategy>
</UniformDiscrete>
</Distributions>

<Optimizers>
<GeneticAlgorithm name="GAopt">
<samplerInit>
<limit>10</limit>
<initialSeed>42</initialSeed>
<writeSteps>every</writeSteps>
<type>max</type>
</samplerInit>

<GAparams>
<populationSize>10</populationSize>
<parentSelection>rouletteWheel</parentSelection>
<reproduction>
<crossover type="onePointCrossover">
<crossoverProb>0.8</crossoverProb>
</crossover>
<mutation type="swapMutator">
<mutationProb>0.9</mutationProb>
</mutation>
</reproduction>
<fitness type="invLinear">
<a>2.0</a>
<b>1.0</b>
</fitness>
<survivorSelection>fitnessBased</survivorSelection>
</GAparams>

<convergence>
<AHDp>0.1</AHDp>
</convergence>

<variable name="x1">
<distribution>uniform_dist_Repl_1</distribution>
</variable>

<variable name="x2">
<distribution>uniform_dist_Repl_1</distribution>
</variable>

<variable name="x3">
<distribution>uniform_dist_Repl_1</distribution>
</variable>

<objective>ans</objective>
<TargetEvaluation class="DataObjects" type="PointSet">optOut</TargetEvaluation>
<Sampler class="Samplers" type="CustomSampler">restartSampler</Sampler>
</GeneticAlgorithm>
</Optimizers>

<Samplers>
<CustomSampler name="restartSampler">
<Source class="Files" type="">restart.csv</Source>
<variable name="x1"/>
<variable name="x2"/>
<variable name="x3"/>
</CustomSampler>
</Samplers>

<Models>
<ExternalModel ModuleToLoad="../../../../../AnalyticModels/optimizing/myLocalSum.py" name="myLocalSum" subType="">
<variables>x1,x2,x3,ans</variables>
</ExternalModel>
</Models>

<DataObjects>
<PointSet name="placeholder"/>
<PointSet name="optOut">
<Input>x1,x2,x3</Input>
<Output>ans</Output>
</PointSet>
<PointSet name="opt_export">
<Input>trajID</Input>
<Output>x1,x2,x3,ans,age,batchId,fitness,iteration,accepted,AHDp,conv_AHDp</Output>
</PointSet>
</DataObjects>

<OutStreams>
<Print name="optOut">
<type>csv</type>
<source>optOut</source>
</Print>
<Print name="opt_export">
<type>csv</type>
<source>opt_export</source>
<clusterLabel>trajID</clusterLabel>
</Print>
<Plot name="opt_path" subType="OptPath">
<source>opt_export</source>
<vars>x1,x2,x3,ans</vars>
</Plot>
</OutStreams>
</Simulation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
x1,x2,x3,ans,age,batchId,fitness,iteration,accepted,AHDp,conv_AHDp
6.0,6.0,6.0,36.0,0.0,1.0,72.0,0.0,first,,0.0
2.0,2.0,6.0,24.0,0.0,1.0,48.0,0.0,first,,0.0
4.0,5.0,3.0,23.0,0.0,1.0,46.0,0.0,first,,0.0
3.0,4.0,2.0,17.0,0.0,1.0,34.0,0.0,first,,0.0
2.0,6.0,1.0,17.0,0.0,1.0,34.0,0.0,first,,0.0
1.0,2.0,4.0,17.0,0.0,1.0,34.0,0.0,first,,0.0
2.0,2.0,2.0,12.0,0.0,1.0,24.0,0.0,first,,0.0
4.0,4.0,3.0,21.0,0.0,1.0,42.0,0.0,first,,0.0
2.0,6.0,6.0,32.0,0.0,1.0,64.0,0.0,first,,0.0
2.0,1.0,3.0,13.0,0.0,1.0,26.0,0.0,first,,0.0
3.0,4.0,4.0,23.0,0.0,2.0,46.0,1.0,accepted,2.44051339313,0.0
4.0,2.0,1.0,11.0,0.0,2.0,22.0,1.0,accepted,2.44051339313,0.0
6.0,4.0,4.0,26.0,0.0,2.0,52.0,1.0,accepted,2.44051339313,0.0
3.0,6.0,6.0,33.0,0.0,2.0,66.0,1.0,accepted,2.44051339313,0.0
3.0,4.0,4.0,23.0,0.0,2.0,46.0,1.0,accepted,2.44051339313,0.0
6.0,2.0,2.0,16.0,0.0,2.0,32.0,1.0,accepted,2.44051339313,0.0
6.0,6.0,1.0,21.0,0.0,2.0,42.0,1.0,accepted,2.44051339313,0.0
4.0,2.0,6.0,26.0,0.0,2.0,52.0,1.0,accepted,2.44051339313,0.0
6.0,2.0,1.0,13.0,0.0,2.0,26.0,1.0,accepted,2.44051339313,0.0
4.0,2.0,2.0,14.0,0.0,2.0,28.0,1.0,accepted,2.44051339313,0.0
6.0,6.0,6.0,36.0,0.0,3.0,72.0,2.0,accepted,2.15036705544,0.0
6.0,6.0,3.0,27.0,0.0,3.0,54.0,2.0,accepted,2.15036705544,0.0
1.0,6.0,6.0,31.0,0.0,3.0,62.0,2.0,accepted,2.15036705544,0.0
6.0,6.0,6.0,36.0,0.0,3.0,72.0,2.0,accepted,2.15036705544,0.0
3.0,5.0,6.0,31.0,0.0,3.0,62.0,2.0,accepted,2.15036705544,0.0
4.0,6.0,6.0,34.0,0.0,3.0,68.0,2.0,accepted,2.15036705544,0.0
1.0,6.0,3.0,22.0,0.0,3.0,44.0,2.0,accepted,2.15036705544,0.0
6.0,6.0,6.0,36.0,0.0,3.0,72.0,2.0,accepted,2.15036705544,0.0
3.0,6.0,3.0,24.0,0.0,3.0,48.0,2.0,accepted,2.15036705544,0.0
6.0,5.0,4.0,28.0,0.0,3.0,56.0,2.0,accepted,2.15036705544,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,2.0,6.0,28.0,0.0,4.0,56.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,2.0,6.0,28.0,0.0,4.0,56.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,0.0,4.0,72.0,3.0,accepted,2.98811264988,0.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,accepted,0.0,1.0
6.0,6.0,6.0,36.0,1.0,5.0,72.0,4.0,final,0.0,1.0
11 changes: 11 additions & 0 deletions tests/framework/Optimizers/GeneticAlgorithms/tests
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@
[../]
[../]

[./MaxwReplacementCustomSampler]
type = 'RavenFramework'
input = 'discrete/unconstrained/testGAMaxwRep_custom_sampler.xml'
[./data]
type = OrderedCSV
output = 'discrete/unconstrained/MaxwReplacement_customsampler/opt_export_0.csv'
rel_err = 1e-3
zero_threshold = 1e-5
[../]
[../]

[./MaxwReplacementDiffDist]
type = 'RavenFramework'
input = 'discrete/unconstrained/testGAMaxwRepDifferentDist.xml'
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/ROM/TimeSeries/ARMA/tests
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@
[./InterpolatedMaxCycles]
type = 'RavenFramework'
input = 'interpolated_maxcycles.xml'
csv = 'InterpolatedMaxCycles/synthetic.csv InterpolatedMaxCycles/resynthetic.csv'
output = 'InterpolatedMaxCycles/synthetic.csv InterpolatedMaxCycles/resynthetic.csv'
#not consistent, see 1351
rel_err = 10000.0
#rel_err = 10000.0
[../]

[./CloudPlot]
Expand Down