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 optimizer writeStep final (if optimization ends for reaching the limit number of iterations) #2387

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions ravenframework/Models/Code.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def evaluateSample(self, myInput, samplerType, kwargs):
sampleDirectory = os.path.join(os.getcwd(),metaData['subDirectory'])
localenv = dict(os.environ)
localenv['PWD'] = str(sampleDirectory)
os.makedirs(os.path.dirname(os.path.join(sampleDirectory,codeLogFile)), exist_ok=True)
outFileObject = open(os.path.join(sampleDirectory,codeLogFile), 'w', bufferSize)

found = False
Expand Down
2 changes: 2 additions & 0 deletions ravenframework/OutStreams/PrintInterfaces/FilePrint.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ def run(self):
if self.options['type'] == 'csv':
filename = dictOptions['filenameroot']
rlzIndex = self.indexPrinted.get(filename,0)
if rlzIndex and rlzIndex >= len(self.sourceData[index]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure if these two lines are needed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is needed when the printing of the dataset is not finished (e.g. point set) and this is triggered in the multi run step (right before exiting the step). We check here the index and the length in case the printing is finished before reaching the ned of the step

continue
dictOptions['firstIndex'] = rlzIndex
# clusterLabel lets the user print a point set as if it were a history, with input decided by clusterLabel
if 'clusterLabel' in self.options:
Expand Down
5 changes: 5 additions & 0 deletions ravenframework/Steps/MultiRun.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ def _localTakeAstepRun(self, inDictionary):
# END while loop that runs the step iterations (collection and submission-for-DET)
# if any collected runs failed, let the sampler treat them appropriately, and any other closing-out actions
sampler.finalizeSampler(self.failedRuns)
for myLambda, outIndex in self._outputCollectionLambda:
if isinstance(outputs[outIndex], OutStreamEntity):
myLambda([None,outputs[outIndex]])
self.raiseAMessage(f'Finalized output "{inDictionary["Output"][outIndex].name}"')
Copy link
Collaborator

Choose a reason for hiding this comment

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

@alfoa The proposed changes can resolve the issue. However, it can be confusing since these lines are almost similar to collection part in the same function. Could you provide more details why previous collection can not collect the final solution? Is it possible to make some modifications inside the optimizer to enable it? I have two concerns for the proposed approach:

  1. Two collections in the same function, which make it very confusing. Either add more explanations or find a way to avoid it.
  2. It is also very confusing in the FilePrint.py, since the new added lines to check the rlzIndex seem very unnecessary. I see the changes make the final collection possible, but it is really hard to understand why these lines are needed unless the developers fully understand the collections in the steps.

Copy link
Collaborator Author

@alfoa alfoa Nov 4, 2024

Choose a reason for hiding this comment

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

The lines in the FilePrint are not unnecessary. They are required when the collection in triggered on data objects that are not "collected/created" by the Optimizers.

Basically, the "SolutionExport" in the Optimizers is "updated" with the final solution after the collection is triggered (at the begin of the processing of the "last job"). So the Outstream is not invoked before exiting the Multirun loop.

This modification was the "minimal viable" solution to trigger an out stream call at the end of the multi run.

Maybe another approach could be to split the calls to the output collection:

  • Right after a Job is finished for data objects (DataObjects/Databases)
  • After the call to the finalizeActualSampling for OutStreams


alfoa marked this conversation as resolved.
Show resolved Hide resolved

def _addNewRuns(self, sampler, model, inputs, outputs, jobHandler, inDictionary, verbose=True):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
The objective variable is ans. The problem in unconstrained, it is a maximization problem, and the sampling is from discrete variables without replacement.
The convergence criterion used is the (p-Average Hausdorff Distance between populations) AHDp.
</description>
<revisions>
<revision author="alfoa" date="2024-10-21">Modified writeSteps to ``final'' to makes sure the Outstream is executed and the final solution is always written. Closes #2386</revision>
alfoa marked this conversation as resolved.
Show resolved Hide resolved
</revisions>
<analytic>
This test uses myLocalSum's analytic objective function.
</analytic>
Expand Down
Loading