File: t_Dlib_global.py

package info (click to toggle)
openturns 1.24-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,204 kB
  • sloc: cpp: 256,662; python: 63,381; ansic: 4,414; javascript: 406; sh: 180; xml: 164; yacc: 123; makefile: 98; lex: 55
file content (56 lines) | stat: -rwxr-xr-x 2,119 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python

import openturns as ot


def printResults(result, problemName):
    print(f"*** {problemName} completed:")
    print(f"      -- Optimal point = {result.getOptimalPoint()}")
    print(f"      -- Optimal value = {result.getOptimalValue()}")
    print(f"      -- Iteration number = {result.getIterationNumber()}")
    print(f"      -- Evaluation number = {result.getCallsNumber()}")
    print(f"      -- Absolute error = {result.getAbsoluteError():.6e}")
    print(f"      -- Relative error = {result.getRelativeError():.6e}")
    print(f"      -- Residual error = {result.getResidualError():.6e}")
    print(f"      -- Constraint error = {result.getConstraintError():.6e}")


# Define the problems based on Rastrigin function
rastrigin = ot.SymbolicFunction(
    ["x1", "x2"], ["20 + x1^2 - 10*cos(2*pi_*x1) + x2^2 - 10*cos(2*pi_*x2)"]
)

unboundedProblem = ot.OptimizationProblem(rastrigin)

notConstrainingBounds = ot.Interval([-5.0, -5.0], [3.0, 2.0])
notConstrainingBoundsProblem = ot.OptimizationProblem(
    rastrigin, ot.Function(), ot.Function(), notConstrainingBounds
)

constrainingBounds = ot.Interval([-1.0, -2.0], [5.0, -0.5])
constrainingBoundsProblem = ot.OptimizationProblem(
    rastrigin, ot.Function(), ot.Function(), constrainingBounds
)

boundedPref = [0.0, -1.0]
unboundedPref = [0.0, 0.0]

# GLOBAL ALGORITHM

# Non-contraining bounds Global
notConstrainingBoundsGlobal = ot.Dlib(notConstrainingBoundsProblem, "global")
notConstrainingBoundsGlobal.setStartingPoint([0.0] * 2)
notConstrainingBoundsGlobal.setMaximumCallsNumber(300)
notConstrainingBoundsGlobal.run()
printResults(notConstrainingBoundsGlobal.getResult(), "Non-constraining bounds Global")

# Contraining bounds Global
constrainingBoundsGlobal = ot.Dlib(constrainingBoundsProblem, "global")
constrainingBoundsGlobal.setMaximumCallsNumber(300)
constrainingBoundsGlobal.setStartingPoint([0.0] * 2)
constrainingBoundsGlobal.run()
printResults(constrainingBoundsGlobal.getResult(), "Constraining bounds Global")

assert (
    notConstrainingBoundsGlobal.getResult().getOptimalValue()[0] < 4.0
), "optimum not found"