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

SimulatedAnnealing Discrete Variables #2312

Merged
merged 26 commits into from
May 20, 2024

Conversation

alfoa
Copy link
Collaborator

@alfoa alfoa commented Apr 30, 2024


Pull Request Description

What issue does this change request address? (Use "#" before the issue to link it, i.e., #42.)

Closes #2305

What are the significant changes in functionality due to this change request?

Addition of the support for simulated annealing of discrete variables.
In order to minimize the modifications, the discrete variables are treated using cdf sampling and inverseCDF transformation.


For Change Control Board: Change Request Review

The following review must be completed by an authorized member of the Change Control Board.

  • 1. Review all computer code.
  • 2. If any changes occur to the input syntax, there must be an accompanying change to the user manual and xsd schema. If the input syntax change deprecates existing input files, a conversion script needs to be added (see Conversion Scripts).
  • 3. Make sure the Python code and commenting standards are respected (camelBack, etc.) - See on the wiki for details.
  • 4. Automated Tests should pass, including run_tests, pylint, manual building and xsd tests. If there are changes to Simulation.py or JobHandler.py the qsub tests must pass.
  • 5. If significant functionality is added, there must be tests added to check this. Tests should cover all possible options. Multiple short tests are preferred over one large test. If new development on the internal JobHandler parallel system is performed, a cluster test must be added setting, in XML block, the node <internalParallel> to True.
  • 6. If the change modifies or adds a requirement or a requirement based test case, the Change Control Board's Chair or designee also needs to approve the change. The requirements and the requirements test shall be in sync.
  • 7. The merge request must reference an issue. If the issue is closed, the issue close checklist shall be done.
  • 8. If an analytic test is changed/added is the the analytic documentation updated/added?
  • 9. If any test used as a basis for documentation examples (currently found in raven/tests/framework/user_guide and raven/docs/workshop) have been changed, the associated documentation must be reviewed and assured the text matches the example.

@moosebuild
Copy link

Job Precheck on a794170 : invalidated by @joshua-cogliati-inl

failed because of hpcgitlab being down

@alfoa alfoa changed the title Alfoa/simulated annealing discrete variables SimulatedAnnealing Discrete Variables May 7, 2024
@alfoa alfoa requested review from wangcj05 and Jimmy-INL May 7, 2024 23:14
@alfoa alfoa mentioned this pull request May 7, 2024
9 tasks
@alfoa
Copy link
Collaborator Author

alfoa commented May 8, 2024

@Jimmy-INL @wangcj05 this is ready for review

Copy link
Collaborator

@wangcj05 wangcj05 left a comment

Choose a reason for hiding this comment

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

@alfoa I have a couple of comments for you to consider. Thanks for the updates regarding the normalized and unnormalized data. It is really confusing in our current code. Some suggestions here:

  1. It seems some of the discrete handling can be moved to Optimizer base class
  2. We need a better document for the data we store in the self. variables. For example, self._optPointHistory stores the normalized data. it is really not clear when our 'rlz' are normalized or not. Again, thanks for you update of our docstring to make it more clear.

@@ -1614,6 +1622,11 @@ def getInputSpecification(cls):
StatePartInput = InputData.parameterInputFactory("state", contentType=InputTypes.FloatType)
StatePartInput.addParam("outcome", InputTypes.FloatOrStringType, True)
inputSpecification.addSub(StatePartInput, InputData.Quantity.one_to_infinity)
inputSpecification.addSub(InputData.parameterInputFactory("rtol",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please update user manual to reflect 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.

done

val = init[var]
values[var] = self.distDict[var].ppf(self.distDict[var].cdf(val))
if not np.isclose(initialValues[traj][var], values[var], 1e-4):
self.raiseAWarning(f"Traj: {traj}. Variable {var} is associated with a discrete distribution. The inputted initial value {initialValues[traj][var]} "
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe define a general tolerance variable for 1e-4.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using the utils.isClose now

Comment on lines 253 to 270

initialValues = self._initialValues
for traj, init in enumerate(self._initialValues):
self._submitRun(init,traj,self.getIteration(traj))
values = {}
init = self.denormalizeData(init)
initialValues[traj] = self.denormalizeData(initialValues[traj])
for var in init:
if var in self.toBeSampled and self.distDict[var].distType == distType.discrete:
val = init[var]
values[var] = self.distDict[var].ppf(self.distDict[var].cdf(val))
if not np.isclose(initialValues[traj][var], values[var], 1e-4):
self.raiseAWarning(f"Traj: {traj}. Variable {var} is associated with a discrete distribution. The inputted initial value {initialValues[traj][var]} "
f"is not among available discrete values. Closest value is {values[var]}")
else:
values[var] = init[var]
values = self.normalizeData(values)

self._submitRun(values,traj,self.getIteration(traj))
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems to me these lines can be moved to Optimizer base class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agree. This probably would require a general "re-design" of the optimizer for discrete variables (with exceptions for not-discrete optimizers (such as the gradient descend))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would open an issue to track this in case the RAVEN team is willing to go in that direction

Comment on lines 272 to 274
# queue up the first run for each trajectory
#for traj, init in enumerate(self._initialValues):
# self._submitRun(init,traj,self.getIteration(traj))
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

Comment on lines 743 to 748
nextNeighbour[var] = rlzNormalized[var] + delta[i]
nextNeighbour[var] = 0 if nextNeighbour[var] < 0 else nextNeighbour[var]
nextNeighbour[var] = 1 if nextNeighbour[var] > 1 else nextNeighbour[var]
if self.distDict[var].distType == distType.discrete:
val = nextNeighbour[var]
nextNeighbour[var] = self.distDict[var].cdf(self.distDict[var].ppf(val))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need to denormalize the data before you compute ppf?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Theoretically the normalization is performed between 0-1 and it implicitly maps to a uniform distribution. I can add the denormalization in order to avoid that, if in the future the normalization strategy got changed, this would result in a diff

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

@alfoa
Copy link
Collaborator Author

alfoa commented May 14, 2024

@wangcj05 all tests fail now for the issue seen in #2317 with cvxpy

@moosebuild
Copy link

All jobs on 28624b8 : invalidated by @wangcj05

<d>1.0</d>
</cauchy>
<veryfast>
<c>0.5</c>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

modifed to reflect the test name "VeryFast" was supposed to be tested here

@alfoa alfoa requested a review from wangcj05 May 16, 2024 15:09
@moosebuild
Copy link

Job Mingw Test on de67311 : invalidated by @alfoa

fetch failure

@moosebuild
Copy link

Job Test qsubs sawtooth on de67311 : invalidated by @alfoa

Copy link
Collaborator

@wangcj05 wangcj05 left a comment

Choose a reason for hiding this comment

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

minor comment.

Comment on lines 4 to 12
<name>framework/Optimizers/SimulatedAnnealing/discrete.FuncionallyConstrainedSA</name>
<author>MohammadAbdo</author>
<created>2020-02-05</created>
<classesTested>Optimizer</classesTested>
<description>
This test uses a multidimensional linear function such that the trajectory must pass through
a functional constraint to reach the optimal point.
The test is designed by @talbpaul and is applied to simulated annealing.
</description>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you update the test description here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

…nallyConstrainedSA/test_funcConstrSimulatedAnnealing.xml
@alfoa alfoa requested a review from wangcj05 May 16, 2024 21:56
Copy link
Collaborator

@wangcj05 wangcj05 left a comment

Choose a reason for hiding this comment

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

changes are good.

@moosebuild
Copy link

Job Mingw Test on 44192fa : invalidated by @wangcj05

@wangcj05
Copy link
Collaborator

Changes are good. PR checklists are good

@wangcj05 wangcj05 merged commit 5c29164 into devel May 20, 2024
12 checks passed
@wangcj05 wangcj05 deleted the alfoa/simulatedAnnealingDiscreteVariables branch May 20, 2024 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TASK] Simulated Annealing Discrete Optimization not supported
3 participants