Optimisation example code? #72
-
Many thanks for a nice and helpful project! I have now abandoned my own Python ZOS-API implementation. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @hmartinsson, Good to hear that you find ZOSPy useful! Currently ZOSPy does not offer its own interface for optimizations, so you need to use the ZOS-API interface for that. I agree that this interface is not very straightforward to use. We are therefore working on an optimization submodule for ZOSPy. Until then, we will not include example code for optimizations, as that example code will (hopefully) be obsolete soon. Here is some example code for optimizations: import zospy as zp
zos = zp.ZOS()
oss = zos.connect()
# Show the MFE
oss.MFE.ShowEditor()
# Clear current operands
oss.MFE.DeleteAllRows()
# Get operand 1 as it is initialized as a blank operand
operand_1 = oss.MFE.GetOperandAt(1)
# Optimize the 4th Zernike coefficient
operand_1.ChangeType(zp.constants.Editors.MFE.MeritOperandType.ZERN)
operand_1.GetOperandCell(zp.constants.Editors.MFE.MeritColumn.Param1).IntegerValue = 4
operand_1.GetOperandCell(zp.constants.Editors.MFE.MeritColumn.Param3).IntegerValue = int(zp.constants.Analysis.SampleSizes.S_512x512)
operand_1.GetOperandCell(zp.constants.Editors.MFE.MeritColumn.Param5).IntegerValue = 1
# Set the target value for the 4th Zernike coefficient to 0
operand_1.Target = 0
operand_1.Weight = 1
# Add and configure a second operand
op2 = oss.MFE.AddOperand()
...
# Run optimization
initial_merit_function = oss.MFE.CalculateMeritFunction()
local_optimization = oss.Tools.OpenLocalOptimization()
local_optimization.Algorithm = zp.constants.Tools.Optimization.OptimizationAlgorithm.DampedLeastSquares
local_optimization.Cycles = zp.constants.Tools.Optimization.OptimizationCycles.Automatic
local_optimization.NumberOfCores = 8
local_optimization.RunAndWaitForCompletion()
local_optimization.Close()
final_merit_function = oss.MFE.CalculateMeritFunction() If you are currently trying to run an optimization and don't know where to start or encountered a problem, feel free to ask your questions here and we're happy to help! |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot, @crnh! |
Beta Was this translation helpful? Give feedback.
Hi @hmartinsson,
Good to hear that you find ZOSPy useful! Currently ZOSPy does not offer its own interface for optimizations, so you need to use the ZOS-API interface for that. I agree that this interface is not very straightforward to use. We are therefore working on an optimization submodule for ZOSPy. Until then, we will not include example code for optimizations, as that example code will (hopefully) be obsolete soon.
Here is some example code for optimizations: