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

Fix defect ensemble model (with Code) and genetic algorithm #2317

Merged
merged 19 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions ravenframework/JobHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def reAddJob(self, runner):
runner.trackTime('queue')
self.__submittedJobs.append(runner.identifier)

def addClientJob(self, args, functionToRun, identifier, metadata=None, uniqueHandler="any"):
def addClientJob(self, args, functionToRun, identifier, metadata=None, uniqueHandler="any", groupInfo = None):
"""
Method to add an internal run (function execution), without consuming
resources (free spots). This can be used for client handling (see
Expand All @@ -719,11 +719,19 @@ def addClientJob(self, args, functionToRun, identifier, metadata=None, uniqueHan
this runner. For example, if present, to retrieve this runner using the
method jobHandler.getFinished, the uniqueHandler needs to be provided.
If uniqueHandler == 'any', every "client" can get this runner.
@ In, groupInfo, dict, optional, {id:string, size:int}.
- "id": it is a special keyword attached to
this runner to identify that this runner belongs to a special set of runs that need to be
grouped together (all will be retrievable only when all the runs ended).
- "size", number of runs in this group self.__batching
NOTE: If the "size" of the group is only set the first time a job of this group is added.
Consequentially the size is immutable
@ Out, None
"""
self.addJob(args, functionToRun, identifier, metadata,
forceUseThreads = True, uniqueHandler = uniqueHandler,
clientQueue = True)
clientQueue = True, groupInfo = groupInfo)


def addFinishedJob(self, data, metadata=None, uniqueHandler="any", profile=False):
"""
Expand Down
9 changes: 8 additions & 1 deletion ravenframework/Models/EnsembleModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,14 @@ def submit(self,myInput,samplerType,jobHandler,**kwargs):
uniqueHandler=uniqueHandler, forceUseThreads=forceThreads,
groupInfo={'id': kwargs['batchInfo']['batchId'], 'size': nRuns} if batchMode else None)
else:
jobHandler.addClientJob((self, myInput, samplerType, kwargs), self.__class__.evaluateSample, prefix, kwargs)
# for parallel strategy 2, the ensemble model works as a step => it needs the jobHandler
kw['jobHandler'] = jobHandler
# for parallel strategy 2, we need to make sure that the batchMode is set to False in the inner runs since only the
# ensemble model evaluation should be batched (THIS IS REQUIRED because the CODE does not submit runs like the other models)
kw['batchMode'] = False
jobHandler.addClientJob((self, myInput, samplerType, kw), self.__class__.evaluateSample, prefix, metadata=metadata,
uniqueHandler=uniqueHandler,
groupInfo={'id': kwargs['batchInfo']['batchId'], 'size': nRuns} if batchMode else None)

def __retrieveDependentOutput(self,modelIn,listOfOutputs, typeOutputs):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2017 Battelle Energy Alliance, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def run(self, Input):
self.sum = self.A + self.B + self.C + self.D
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright Nucube Energy, Inc.

def evaluate(self):
return self.decay_A+0.0001


Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?xml version="1.0" ?>
<Simulation verbosity="debug">
<TestInfo>
<name>framework/Optimizers/GeneticAlgorithms.GAwithEnsembleModelIncludingCode</name>
<author>mandd</author>
alfoa marked this conversation as resolved.
Show resolved Hide resolved
<created>2024-05-07</created>
<classesTested>Models.EnsembleModel, Optimizers.GeneticAlgorithm</classesTested>
<description>
This test is aimed to test the usage of the GeneticAlgorithm with an EnsembleModel, including
a model Code. This test has been created to test the modifications performed to address issue
#2304 (see \\url{https://github.com/idaholab/raven/issues/2304}).
</description>
</TestInfo>

<RunInfo>
<JobName>metaModelWithCodeAndFunctionsAndGenetic</JobName>
<Sequence>
optimize
</Sequence>
<WorkingDir>metaModelWithCodeAndFunctionsAndGenetic</WorkingDir>
<batchSize>1</batchSize>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested it with different batch size?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ops. Not yet. let me test it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wangcj05 I fixed the parallel execution and converted the test into a parallel (batchSize > 1) to test it.

Ready for re-review

</RunInfo>

<Files>
<Input name="referenceInput.xml" type="input">referenceInput.xml</Input>
</Files>

<Models>
<Code name="testModel" subType="GenericCode">
<executable>../../../../user_guide/physicalCode/analyticalbateman/AnalyticalDplMain.py</executable>
<clargs arg="python" type="prepend"/>
<clargs arg="" extension=".xml" type="input"/>
<clargs arg=" " extension=".csv" type="output"/>
<prepend>python</prepend>
</Code>
<ExternalModel ModuleToLoad="ABCDsum" name="sumOfABCD" subType="">
<variables>A,B,C,D,sum</variables>
</ExternalModel>
<EnsembleModel name="codeAndExtModel" subType="">
<Model class="Models" type="ExternalModel">
sumOfABCD
<Input class="DataObjects" type="PointSet">inputPlaceHolderForInternalObjects</Input>
<TargetEvaluation class="DataObjects" type="PointSet">sumData</TargetEvaluation>
</Model>
<Model class="Models" type="Code">
testModel
<Input class="Files" type="">referenceInput.xml</Input>
<TargetEvaluation class="DataObjects" type="PointSet">samplesMC</TargetEvaluation>
</Model>
</EnsembleModel>
</Models>

<Distributions>
<Uniform name="sigma">
<lowerBound>0</lowerBound>
<upperBound>1000</upperBound>
</Uniform>
<Uniform name="decayConstant">
<lowerBound>0.00000001</lowerBound>
<upperBound>0.0000001</upperBound>
</Uniform>
</Distributions>

<Optimizers>
<GeneticAlgorithm name="GAopt">
<samplerInit>
<limit>20</limit>
<initialSeed>42</initialSeed>
<writeSteps>every</writeSteps>
</samplerInit>

<GAparams>
<populationSize>2</populationSize>
<parentSelection>rouletteWheel</parentSelection>
<reproduction>
<crossover type="onePointCrossover">
<crossoverProb>0.8</crossoverProb>
</crossover>
<mutation type="swapMutator">
<mutationProb>0.9</mutationProb>
</mutation>
</reproduction>
<fitness type="feasibleFirst"></fitness>
<survivorSelection>fitnessBased</survivorSelection>
</GAparams>

<convergence>
<AHDp>0.0</AHDp>
</convergence>

<variable name="sigma-A">
<distribution>sigma</distribution>
</variable>
<variable name="decay_A">
<distribution>decayConstant</distribution>
</variable>
<variable name="sigma-B">
<distribution>sigma</distribution>
</variable>
<variable name="decay_B">
<function>decayConstantB</function>
</variable>

<objective>sum</objective>
<TargetEvaluation class="DataObjects" type="PointSet">finalResponses</TargetEvaluation>
<Sampler class="Samplers" type="MonteCarlo">MC_samp</Sampler>
</GeneticAlgorithm>
</Optimizers>

<Functions>
<External file="decayConstantB.py" name="decayConstantB">
<variables>decay_A</variables>
</External>
</Functions>

<Samplers>
<MonteCarlo name="MC_samp">
<samplerInit>
<limit>2</limit>
<initialSeed>20021986</initialSeed>
</samplerInit>
<variable name="sigma-A">
<distribution>sigma</distribution>
</variable>
<variable name="decay_A">
<distribution>decayConstant</distribution>
</variable>
<variable name="sigma-B">
<distribution>sigma</distribution>
</variable>
<variable name="decay_B">
<function>decayConstantB</function>
</variable>
</MonteCarlo>
</Samplers>

<Steps>
<MultiRun name="optimize">
<Input class="Files" type="">referenceInput.xml</Input>
<Input class="DataObjects" type="PointSet">inputPlaceHolderForInternalObjects</Input>
<Model class="Models" type="EnsembleModel">codeAndExtModel</Model>
<Optimizer class="Optimizers" type="GeneticAlgorithm">GAopt</Optimizer>
<SolutionExport class="DataObjects" type="PointSet">opt_export</SolutionExport>
<Output class="DataObjects" type="PointSet">finalResponses</Output>
<Output class="OutStreams" type="Print">opt_export</Output>
</MultiRun>
</Steps>

<OutStreams>
<Print name="opt_export">
<type>csv</type>
<source>opt_export</source>
<what>input,output</what>
</Print>
</OutStreams>

<DataObjects>
<PointSet name="samplesMC">
<Input>sigma-A,sigma-B,decay_A,decay_B</Input>
<Output>A,B,C,D</Output>
</PointSet>
<PointSet name="inputPlaceHolderForInternalObjects">
<Input>A,B,C,D</Input>
<Output>OutputPlaceHolder</Output>
</PointSet>
<PointSet name="sumData">
<Input>A,B,C,D</Input>
<Output>sum</Output>
</PointSet>
<PointSet name="finalResponses">
<Input>sigma-A,sigma-B,decay_A,decay_B</Input>
<Output>A,B,C,D,sum</Output>
</PointSet>
<PointSet name="opt_export">
<Input>trajID</Input>
<Output>sigma-A,sigma-B,decay_A,decay_B,sum,age,batchId,fitness,iteration,accepted,AHDp,conv_AHDp</Output>
</PointSet>
</DataObjects>

</Simulation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
trajID,sigma-A,sigma-B,decay_A,decay_B,sum,age,batchId,fitness,iteration,accepted,AHDp,conv_AHDp
0,812.997460787,464.508307042,1.255650268e-08,0.000100012556503,3.15760954156,0,1,-3.15760954156,0,first,,False
0,759.507928453,912.545952926,8.14640085566e-08,0.000100081464009,2.90476176926,0,1,-2.90476176926,0,first,,False
0,912.545952926,812.997460787,1.255650268e-08,0.000100012556503,3.15873736407,0,2,-3.15873736407,1,accepted,299.428022512,False
0,464.508307042,759.507928453,8.14640085566e-08,0.000100081464009,2.83929690449,0,2,-2.83929690449,1,accepted,299.428022512,False
0,794.044539517,912.545952926,5.18057476338e-08,0.000100051805748,2.98393998882,1,3,-2.98393998882,2,accepted,261.653404312,False
0,794.044539517,759.507928453,7.83557135608e-08,0.000100078355714,2.9174761674,1,3,-2.9174761674,2,accepted,261.653404312,False
0,912.545952926,464.508307042,8.14640085566e-08,0.000100081464009,2.93136915288,2,4,-2.93136915288,3,accepted,379.964041465,False
0,759.507928453,759.507928453,8.14640085566e-08,0.000100081464009,2.90491165751,2,4,-2.90491165751,3,accepted,379.964041465,False
0,464.508307042,912.545952926,8.14640085566e-08,0.000100081464009,2.83915821533,3,5,-2.83915821533,4,accepted,154.98621892,False
0,794.044539517,759.507928453,7.83557135608e-08,0.000100078355714,2.9174761674,3,5,-2.9174761674,4,accepted,154.98621892,False
0,912.545952926,464.508307042,8.14640085566e-08,0.000100081464009,2.93136915288,3,6,-2.93136915288,5,accepted,484.155218746,False
0,759.507928453,464.508307042,8.14640085566e-08,0.000100081464009,2.90520071484,3,6,-2.90520071484,5,accepted,484.155218746,False
0,912.545952926,464.508307042,8.14640085566e-08,0.000100081464009,2.93136915288,4,7,-2.93136915288,6,accepted,484.155218746,False
0,759.507928453,464.508307042,8.14640085566e-08,0.000100081464009,2.90520071484,4,7,-2.90520071484,6,accepted,484.155218746,False
0,794.044539517,759.507928453,5.18057476338e-08,0.000100051805748,2.98410104597,5,8,-2.98410104597,7,accepted,329.536232476,False
0,794.044539517,912.545952926,5.18057476338e-08,0.000100051805748,2.98393998882,5,8,-2.98393998882,7,accepted,329.536232476,False
0,794.044539517,759.507928453,5.18057476338e-08,0.000100051805748,2.98410104597,6,9,-2.98410104597,8,accepted,261.553081047,False
0,464.508307042,912.545952926,8.14640085566e-08,0.000100081464009,2.83915821533,6,9,-2.83915821533,8,accepted,261.553081047,False
0,794.044539517,912.545952926,5.18057476338e-08,0.000100051805748,2.98393998882,4,10,-2.98393998882,9,accepted,329.536232476,False
0,794.044539517,912.545952926,5.18057476338e-08,0.000100051805748,2.98393998882,4,10,-2.98393998882,9,accepted,329.536232476,False
0,464.508307042,794.044539517,9.21291357633e-08,0.000100092129136,2.81925891819,5,11,-2.81925891819,10,accepted,118.501413408,False
0,464.508307042,794.044539517,9.21291357633e-08,0.000100092129136,2.81925891819,5,11,-2.81925891819,10,accepted,118.501413408,False
0,464.508307042,912.545952926,8.14640085566e-08,0.000100081464009,2.83915821533,0,12,-2.83915821533,11,accepted,118.501413408,False
0,464.508307042,912.545952926,8.14640085566e-08,0.000100081464009,2.83915821533,0,12,-2.83915821533,11,accepted,118.501413408,False
0,464.508307042,794.044539517,9.21291357633e-08,0.000100092129136,2.81925891819,1,13,-2.81925891819,12,accepted,94.054634152,False
0,464.508307042,912.545952926,8.14640085566e-08,0.000100081464009,2.83915821533,1,13,-2.83915821533,12,accepted,94.054634152,False
0,912.545952926,794.044539517,5.18057476338e-08,0.000100051805748,3.00110960171,2,14,-3.00110960171,13,accepted,448.037645884,False
0,912.545952926,794.044539517,5.18057476338e-08,0.000100051805748,3.00110960171,2,14,-3.00110960171,13,accepted,448.037645884,False
0,912.545952926,794.044539517,5.18057476338e-08,0.000100051805748,3.00110960171,3,15,-3.00110960171,14,accepted,448.037645884,False
0,912.545952926,794.044539517,5.18057476338e-08,0.000100051805748,3.00110960171,3,15,-3.00110960171,14,accepted,448.037645884,False
0,794.044539517,464.508307042,9.21291357633e-08,0.000100092129136,2.89183632446,4,16,-2.89183632446,15,accepted,369.891914497,False
0,464.508307042,794.044539517,9.21291357633e-08,0.000100092129136,2.81925891819,4,16,-2.81925891819,15,accepted,369.891914497,False
0,794.044539517,464.508307042,9.21291357633e-08,0.000100092129136,2.89183632446,3,17,-2.89183632446,16,accepted,466.03460926,False
0,794.044539517,464.508307042,9.21291357633e-08,0.000100092129136,2.89183632446,3,17,-2.89183632446,16,accepted,466.03460926,False
0,794.044539517,464.508307042,9.21291357633e-08,0.000100092129136,2.89183632446,4,18,-2.89183632446,17,accepted,466.03460926,False
0,794.044539517,464.508307042,9.21291357633e-08,0.000100092129136,2.89183632446,4,18,-2.89183632446,17,accepted,466.03460926,False
0,464.508307042,912.545952926,8.14640085566e-08,0.000100081464009,2.83915821533,5,19,-2.83915821533,18,accepted,94.054634152,False
0,464.508307042,794.044539517,9.21291357633e-08,0.000100092129136,2.81925891819,5,19,-2.81925891819,18,accepted,94.054634152,False
10 changes: 10 additions & 0 deletions tests/framework/Optimizers/GeneticAlgorithms/tests
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,14 @@
rel_err = 0.001
[../]
[../]

[./GAwithEnsembleModelIncludingCode]
type = 'RavenFramework'
input = 'continuous/unconstrained/test_ensembleModel_withGA_Code_and_Functions.xml'
[./csv]
type = OrderedCSV
output = 'continuous/unconstrained/metaModelWithCodeAndFunctionsAndGenetic/opt_export.csv'
rel_err = 0.001
[../]
[../]
[]