finesse.solutions.base.SolutionSet
¶
Overview
- class finesse.solutions.base.SolutionSet(solutions)¶
Bases:
Set
A SolutionSet is a collection of solution objects that have been generated from running a model. When nested analyses have been used then you must then extract each of the nested solutions. The SolutionSet object makes this easier by collecting a variety of Solutions together so you can select common nested solutions within them, or extract some common attribute.
Take some overly simplistic example here where we have swept some variable and performed some other analysis at each step.
>>> import finesse >>> model = finesse.Model() >>> model.parse(''' ... l l1 P=1 ... pd P l1.p1.o ... xaxis(l1.P, lin, 0, 1, 2, ... pre_step=series( ... minimize(P, l1.P) ... ) ... ) ... ''') >>> >>> sol = model.run() >>> print(sol) - Solution Tree ● xaxis - ArraySolution ╰──○ pre_step ├──○ series │ ╰──○ minimize - OptimizeSolution ├──○ series │ ╰──○ minimize - OptimizeSolution ╰──○ series ╰──○ minimize - OptimizeSolution
Here we have an optimisation solution buried in the
pre_step
events of the axis. We can get them all by simply calling:>>> sol['pre_step', 'series', 'minimize'] <finesse.solutions.base.SolutionSet object at ...>
You can see which solutions you have selected using:
>>> sol['pre_step', 'series', 'minimize'].solutions [<OptimizeSolution of series/xaxis/pre_step/series/minimize @ ... children=0>, <OptimizeSolution of series/xaxis/pre_step/series/minimize @ ... children=0>, <OptimizeSolution of series/xaxis/pre_step/series/minimize @ ... children=0>]
We can select a common attribute from these similar solutions by just acting on the SolutionSet, the attribute request is evaluated on each Solution present in the set and returned. For example, we can get the result attribute from each OptimisationSolution using:
>>> print(sol['pre_step', 'series', 'minimize'].result) [ final_simplex: (array([[0.00e+00], [6.25e-05]]), array([0.00e+00, 6.25e-05])) fun: 0.0 message: 'Optimization terminated successfully.' nfev: 6 nit: 3 status: 0 success: True x: array([0.]) ... ]
The returned attribute request will be a numpy array of objects or numerical values. This means it is possible to easily extract an array of values from a set of nested solutions.
Which returns a tuple of the requested attributes from each of the nested solutions. You can also slice the solution object, which will again returns a reduced SolutionSet:
>>> sol['pre_step', 'series', 'minimize'][::2] <finesse.solutions.base.SolutionSet object at ...>
Each individual solution can be extracted using the SolutionSet.solutions attribute which returns a list of solution you can iterate over.