Automation in Optimization

Venkadesh Murugan, Engineer

Automation in Optimization

Hello 
Currently am trying to create the VBA code in Optimization topic. I found the following code in the help document

Sub CATMain()

' Set the CATIA popup file alerts to False
' It prevents to stop the macro at each alert during its execution
CATIA.DisplayFileAlerts = False

' Retrieve your active document - CATIA is your application
' You get the active document by using the ActiveDocument property
' on your application object
Dim oActiveDoc As Document
Set oActiveDoc = CATIA.ActiveDocument

' Check whether the document is a CATPart
If (InStr(oActiveDoc.Name, ".CATPart")) <> 0 Then

' Retrieve the collection object which contains
' all the document relations.
' The statements below are only valid when the active
' document is a CATPart
Dim oRelations As Relations
Set oRelations = oActiveDoc.Part.Relations

' Retrieve the collection object which contains
' all the document parameters.
Dim oParameters As Parameters
Set oParameters = oActiveDoc.Part.Parameters

' Create Real type parameter as objective to be optimized.
Dim oFx As Parameter
Set oFx = oParameters.CreateReal("Real1", 199)
oFx.Rename "fx"

' Create Real type parameter as free parameter.
Dim oX As Parameter
Set oX = oParameters.CreateReal("Real2", 299)
oX.Rename "x"

' Create a formula to be optimized.
Dim oFormula As Formula
' Set oFormula = oRelations.CreateFormula( _
' "Objective", _
' "This is the objective function.",_
' oFx, _
' "x*x + 8.0")

' Retrieve the collection object which contains
' all the document optimizations.
Dim oOptimizations As Optimizations
Set oOptimizations = oRelations.Optimizations

' Create the optimization feature.
Dim oOptimization1 As Optimization
Set oOptimization1 = oOptimizations.CreateOptimization()
oOptimization1.OptimizationType = catMinimum
oOptimization1.AlgorithmType = catSimulatedAnnealing


'Set up the optimization feature attributes.
oOptimization1.MaxEvalsNb = 300
oOptimization1.UseMaxTime = True
oOptimization1.MaxTime = 2 '2 minutes.
oOptimization1.UseMaxEvalsWoImprovement = True
oOptimization1.MaxEvalsWoImprovement = 20


'Set up the free parameters of the optimization and their steps (this is optional).
'The step helps the algorithm to get an order of magnitude of the changes of values
'acceptable for each free parameters.
Dim oFreeParameters As FreeParameters
Set oFreeParameters = oOptimization1.FreeParameters
oFreeParameters.AddFreeParameter (oX)

'The following is optional, but usually reducing the range of input parameters helps to
'solve the problem faster. In this case there only is one free parameter but optimizations
' can be run with multiple free parameters.
Dim p As FreeParameter
For Each p In oFreeParameters
p.Step = 0.1
p.InfRange = -1000
p.SupRange = 2000
Next

'Set the parameter that must be optimized.
oOptimization1.ObjectiveParameter = oFx
MsgBox "Before optimisation :" & oFx.Name & " = " & oFx.Value & " and " & oX.Name & " = " & oX.Value

'Running the optimization without the progress bar dialog box (False).
oOptimization1.Run False

MsgBox "After optimisation :" & oFx.Name & " = " & oFx.Value & " and " & oX.Name & " = " & oX.Value

' Update the document
CATIA.ActiveDocument.Part.Update

 

Else
MsgBox "The active document must be a CATPart"
End If
End Sub

 

When i run the code, i get an error at the line where the objective parameter is set.

oOptimization1.ObjectiveParameter = oFx

Can anyone help me, what is the issue with the above line. 

 

Thanks & Regards

Venkadesh M

Venkadesh Murugan, Engineer

RE: Automation in Optimization
(in response to Venkadesh Murugan, Engineer)

Hello 

I have found the solution that, I need to set the objective parameter as realparam and to set the parameter.

Thanks 

 

 

Thanks & Regards

Venkadesh M