#. :ref:`Examples with the "3DVAR" algorithm<section_ref_algorithm_3DVAR_examples>`
#. :ref:`Examples with the "Blue" algorithm<section_ref_algorithm_Blue_examples>`
+#. :ref:`Examples with the "DerivativeFreeOptimization" algorithm<section_ref_algorithm_DerivativeFreeOptimization_examples>`
#. :ref:`Examples with the "ExtendedBlue" algorithm<section_ref_algorithm_ExtendedBlue_examples>`
#. :ref:`Examples with the "KalmanFilter" algorithm<section_ref_algorithm_KalmanFilter_examples>`
#. :ref:`Examples with the "NonLinearLeastSquares" algorithm<section_ref_algorithm_NonLinearLeastSquares_examples>`
.. ------------------------------------ ..
.. _section_ref_algorithm_DerivativeFreeOptimization_examples:
+.. include:: snippets/Header2Algo09.rst
+
+.. include:: scripts/simple_DerivativeFreeOptimization.rst
+
+.. literalinclude:: scripts/simple_DerivativeFreeOptimization.py
+
+.. include:: snippets/Header2Algo10.rst
+
+.. literalinclude:: scripts/simple_DerivativeFreeOptimization.res
+ :language: none
+
+.. include:: snippets/Header2Algo11.rst
+
+.. _simple_DerivativeFreeOptimization:
+.. image:: scripts/simple_DerivativeFreeOptimization.png
+ :align: center
+ :width: 90%
+
+.. ------------------------------------ ..
.. include:: snippets/Header2Algo06.rst
- :ref:`section_ref_algorithm_ParticleSwarmOptimization`
print("")
print("Expected theoretical coefficients..:", ravel((2,-1,2)))
print("")
+print("Number of iterations...............:", len(case.get('CurrentState')))
+print("Number of simulations..............:", len(case.get('CurrentState'))*4)
print("Calibration resulting coefficients.:", ravel(case.get('Analysis')[-1]))
#
Xa = case.get('Analysis')[-1]
Expected theoretical coefficients..: [ 2 -1 2]
+Number of iterations...............: 25
+Number of simulations..............: 100
Calibration resulting coefficients.: [ 2. -0.99999992 1.99999987]
--- /dev/null
+# -*- coding: utf-8 -*-
+#
+from numpy import array, ravel
+def QuadFunction( coefficients ):
+ """
+ Quadratic simulation in x: y = a x^2 + b x + c
+ """
+ a, b, c = list(ravel(coefficients))
+ x_points = (-5, 0, 1, 3, 10)
+ y_points = []
+ for x in x_points:
+ y_points.append( a*x*x + b*x + c )
+ return array(y_points)
+#
+Xb = array([1., 1., 1.])
+Yobs = array([57, 2, 3, 17, 192])
+#
+print("Resolution of the calibration problem")
+print("-------------------------------------")
+print("")
+from adao import adaoBuilder
+case = adaoBuilder.New('')
+case.setBackground( Vector = Xb, Stored=True )
+case.setBackgroundError( ScalarSparseMatrix = 1.e6 )
+case.setObservation( Vector = Yobs, Stored=True )
+case.setObservationError( ScalarSparseMatrix = 1. )
+case.setObservationOperator( OneFunction = QuadFunction )
+case.setAlgorithmParameters(
+ Algorithm='DerivativeFreeOptimization',
+ Parameters={
+ 'MaximumNumberOfIterations': 100,
+ 'StoreSupplementaryCalculations': [
+ 'CurrentState',
+ ],
+ },
+ )
+case.setObserver(
+ Info=" Intermediate state at the current iteration:",
+ Template='ValuePrinter',
+ Variable='CurrentState',
+ )
+case.execute()
+print("")
+#
+#-------------------------------------------------------------------------------
+#
+print("Calibration of %i coefficients in a 1D quadratic function on %i measures"%(
+ len(case.get('Background')),
+ len(case.get('Observation')),
+ ))
+print("----------------------------------------------------------------------")
+print("")
+print("Observation vector.................:", ravel(case.get('Observation')))
+print("A priori background state..........:", ravel(case.get('Background')))
+print("")
+print("Expected theoretical coefficients..:", ravel((2,-1,2)))
+print("")
+print("Number of iterations...............:", len(case.get('CurrentState')))
+print("Number of simulations..............:", len(case.get('CurrentState')))
+print("Calibration resulting coefficients.:", ravel(case.get('Analysis')[-1]))
+#
+Xa = case.get('Analysis')[-1]
+import matplotlib.pyplot as plt
+plt.rcParams['figure.figsize'] = (10, 4)
+#
+plt.figure()
+plt.plot((-5,0,1,3,10),QuadFunction(Xb),'b-',label="Simulation at background")
+plt.plot((-5,0,1,3,10),Yobs, 'kX',label='Observation',markersize=10)
+plt.plot((-5,0,1,3,10),QuadFunction(Xa),'r-',label="Simulation at optimum")
+plt.legend()
+plt.title('Coefficients calibration', fontweight='bold')
+plt.xlabel('Arbitrary coordinate')
+plt.ylabel('Observations')
+plt.savefig("simple_DerivativeFreeOptimization.png")
--- /dev/null
+Resolution of the calibration problem
+-------------------------------------
+
+ Intermediate state at the current iteration: [1. 1. 1.]
+ Intermediate state at the current iteration: [2. 1. 1.]
+ Intermediate state at the current iteration: [1. 2. 1.]
+ Intermediate state at the current iteration: [1. 1. 2.]
+ Intermediate state at the current iteration: [0. 1. 1.]
+ Intermediate state at the current iteration: [1. 0. 1.]
+ Intermediate state at the current iteration: [1. 1. 0.]
+ Intermediate state at the current iteration: [1.82475484 1.96682811 1.18582936]
+ Intermediate state at the current iteration: [1.89559338 0.54235283 1.17221593]
+ Intermediate state at the current iteration: [ 1.90222657 -0.20823061 1.83295831]
+ Intermediate state at the current iteration: [ 1.94478151 -0.55541624 2.76978872]
+ Intermediate state at the current iteration: [ 2.04021458 -1.49397981 2.43813988]
+ Intermediate state at the current iteration: [ 2.26677171 -0.58498556 2.84248383]
+ Intermediate state at the current iteration: [ 1.9902328 -1.04021448 2.88338819]
+ Intermediate state at the current iteration: [ 1.98695318 -0.92116383 2.47695648]
+ Intermediate state at the current iteration: [ 1.99320312 -1.02368518 2.64731215]
+ Intermediate state at the current iteration: [ 1.79473809 -0.96379959 2.44856121]
+ Intermediate state at the current iteration: [ 1.98630908 -0.91207212 2.5394595 ]
+ Intermediate state at the current iteration: [ 1.99262279 -0.97073591 2.4801914 ]
+ Intermediate state at the current iteration: [ 1.99434434 -0.99814626 2.51840027]
+ Intermediate state at the current iteration: [ 1.98838712 -0.93500739 2.44986416]
+ Intermediate state at the current iteration: [ 1.99080633 -0.94768294 2.46780354]
+ Intermediate state at the current iteration: [ 2.01588592 -0.97086338 2.47667529]
+ Intermediate state at the current iteration: [ 1.99098142 -0.96519905 2.48111896]
+ Intermediate state at the current iteration: [ 1.99157918 -0.97086422 2.47290017]
+ Intermediate state at the current iteration: [ 1.9929175 -0.98359308 2.45753186]
+ Intermediate state at the current iteration: [ 1.99550241 -1.01497755 2.43286745]
+ Intermediate state at the current iteration: [ 1.99505414 -1.00691754 2.44681334]
+ Intermediate state at the current iteration: [ 1.97993261 -1.01100857 2.43408454]
+ Intermediate state at the current iteration: [ 1.99503312 -1.0022575 2.42298652]
+ Intermediate state at the current iteration: [ 1.99337049 -0.98139127 2.3984825 ]
+ Intermediate state at the current iteration: [ 1.99387512 -0.97303786 2.33457311]
+ Intermediate state at the current iteration: [ 1.99742055 -0.99371791 2.20738214]
+ Intermediate state at the current iteration: [ 2.0002882 -0.98541744 1.96740743]
+ Intermediate state at the current iteration: [ 2.00047429 -0.99646137 2.01501424]
+ Intermediate state at the current iteration: [ 2.0009106 -1.00072301 2.00881512]
+ Intermediate state at the current iteration: [ 1.9909278 -1.00127001 2.00860374]
+ Intermediate state at the current iteration: [ 2.0009174 -1.00688459 2.01669134]
+ Intermediate state at the current iteration: [ 1.99994608 -1.00029476 2.00923274]
+ Intermediate state at the current iteration: [ 2.00031465 -1.00202777 1.98931137]
+ Intermediate state at the current iteration: [ 1.99389877 -1.00336389 2.01658192]
+ Intermediate state at the current iteration: [ 2.00003478 -1.00017674 1.99950089]
+ Intermediate state at the current iteration: [ 1.99970222 -0.99882654 1.99924329]
+ Intermediate state at the current iteration: [ 2.00000228 -0.99960552 1.99820757]
+ Intermediate state at the current iteration: [ 2.00103142 -1.0000571 1.9985047 ]
+ Intermediate state at the current iteration: [ 1.99925894 -1.0009752 1.9986288 ]
+ Intermediate state at the current iteration: [ 1.99998604 -0.99994926 2.00089583]
+ Intermediate state at the current iteration: [ 2.00000344 -0.99996363 1.9994818 ]
+ Intermediate state at the current iteration: [ 1.99980383 -0.99996386 1.9994693 ]
+ Intermediate state at the current iteration: [ 1.99998362 -0.99985392 1.99931575]
+ Intermediate state at the current iteration: [ 2.00000605 -1.00001563 1.9996749 ]
+ Intermediate state at the current iteration: [ 2.00000343 -1.00002045 1.99987483]
+ Intermediate state at the current iteration: [ 2.00000165 -1.0000257 2.00006119]
+ Intermediate state at the current iteration: [ 2.00000167 -1.00001085 1.99996478]
+
+Calibration of 3 coefficients in a 1D quadratic function on 5 measures
+----------------------------------------------------------------------
+
+Observation vector.................: [ 57. 2. 3. 17. 192.]
+A priori background state..........: [1. 1. 1.]
+
+Expected theoretical coefficients..: [ 2 -1 2]
+
+Number of iterations...............: 54
+Number of simulations..............: 54
+Calibration resulting coefficients.: [ 2.00000167 -1.00001085 1.99996478]
--- /dev/null
+.. index:: single: 3DVAR (example)
+
+This example describes the calibration of parameters :math:`\mathbf{x}` of a
+quadratic observation model :math:`H`. This model is here represented as a
+function named ``QuadFunction``. This function get as input the coefficients
+vector :math:`\mathbf{x}`, and return as output the evaluation vector
+:math:`\mathbf{y}` of the quadratic model at the predefined internal control
+points. The calibration is done using an initial coefficient set (background
+state specified by ``Xb`` in the code), and with the information
+:math:`\mathbf{y}^o` (specified by ``Yobs`` in the code) of 5 measures obtained
+in these same internal control points. We set twin experiments (see
+:ref:`section_methodology_twin`) and the measurements are supposed to be
+perfect. We choose to emphasize the observations versus the background by
+setting a great variance for the background error, here of :math:`10^{6}`.
+
+The adjustment is carried out by displaying intermediate results during
+iterative optimization.
print("")
print("Expected theoretical coefficients..:", ravel((2,-1,2)))
print("")
+print("Number of iterations...............:", len(case.get('CurrentState')))
+print("Number of simulations..............:", len(case.get('CurrentState'))*4)
print("Calibration resulting coefficients.:", ravel(case.get('Analysis')[-1]))
#
Xa = case.get('Analysis')[-1]
Expected theoretical coefficients..: [ 2 -1 2]
+Number of iterations...............: 25
+Number of simulations..............: 100
Calibration resulting coefficients.: [ 2. -0.99999995 2.00000015]
#. :ref:`Exemples avec l'algorithme de "3DVAR"<section_ref_algorithm_3DVAR_examples>`
#. :ref:`Exemples avec l'algorithme de "Blue"<section_ref_algorithm_Blue_examples>`
+#. :ref:`Exemples avec l'algorithme de "DerivativeFreeOptimization" algorithm<section_ref_algorithm_DerivativeFreeOptimization_examples>`
#. :ref:`Exemples avec l'algorithme de "ExtendedBlue"<section_ref_algorithm_ExtendedBlue_examples>`
#. :ref:`Exemples avec l'algorithme de "KalmanFilter"<section_ref_algorithm_KalmanFilter_examples>`
#. :ref:`Exemples avec l'algorithme de "NonLinearLeastSquares"<section_ref_algorithm_NonLinearLeastSquares_examples>`
.. ------------------------------------ ..
.. _section_ref_algorithm_DerivativeFreeOptimization_examples:
+.. include:: snippets/Header2Algo09.rst
+
+.. include:: scripts/simple_DerivativeFreeOptimization.rst
+
+.. literalinclude:: scripts/simple_DerivativeFreeOptimization.py
+
+.. include:: snippets/Header2Algo10.rst
+
+.. literalinclude:: scripts/simple_DerivativeFreeOptimization.res
+ :language: none
+
+.. include:: snippets/Header2Algo11.rst
+
+.. _simple_DerivativeFreeOptimization:
+.. image:: scripts/simple_DerivativeFreeOptimization.png
+ :align: center
+ :width: 90%
+
+.. ------------------------------------ ..
.. include:: snippets/Header2Algo06.rst
- :ref:`section_ref_algorithm_ParticleSwarmOptimization`
print("")
print("Coefficients théoriques attendus..:", ravel((2,-1,2)))
print("")
+print("Nombre d'itérations...............:", len(case.get('CurrentState')))
+print("Nombre de simulations.............:", len(case.get('CurrentState'))*4)
print("Coefficients résultants du calage.:", ravel(case.get('Analysis')[-1]))
#
Xa = case.get('Analysis')[-1]
Coefficients théoriques attendus..: [ 2 -1 2]
+Nombre d'itérations...............: 25
+Nombre de simulations.............: 100
Coefficients résultants du calage.: [ 2. -0.99999992 1.99999987]
--- /dev/null
+# -*- coding: utf-8 -*-
+#
+from numpy import array, ravel
+def QuadFunction( coefficients ):
+ """
+ Simulation quadratique aux points x : y = a x^2 + b x + c
+ """
+ a, b, c = list(ravel(coefficients))
+ x_points = (-5, 0, 1, 3, 10)
+ y_points = []
+ for x in x_points:
+ y_points.append( a*x*x + b*x + c )
+ return array(y_points)
+#
+Xb = array([1., 1., 1.])
+Yobs = array([57, 2, 3, 17, 192])
+#
+print("Résolution du problème de calage")
+print("--------------------------------")
+print("")
+from adao import adaoBuilder
+case = adaoBuilder.New('')
+case.setBackground( Vector = Xb, Stored=True )
+case.setBackgroundError( ScalarSparseMatrix = 1.e6 )
+case.setObservation( Vector = Yobs, Stored=True )
+case.setObservationError( ScalarSparseMatrix = 1. )
+case.setObservationOperator( OneFunction = QuadFunction )
+case.setAlgorithmParameters(
+ Algorithm='DerivativeFreeOptimization',
+ Parameters={
+ 'MaximumNumberOfIterations': 100,
+ 'StoreSupplementaryCalculations': [
+ 'CurrentState',
+ ],
+ },
+ )
+case.setObserver(
+ Info=" État intermédiaire en itération courante :",
+ Template='ValuePrinter',
+ Variable='CurrentState',
+ )
+case.execute()
+print("")
+#
+#-------------------------------------------------------------------------------
+#
+print("Calage de %i coefficients pour une forme quadratique 1D sur %i mesures"%(
+ len(case.get('Background')),
+ len(case.get('Observation')),
+ ))
+print("--------------------------------------------------------------------")
+print("")
+print("Vecteur d'observation.............:", ravel(case.get('Observation')))
+print("État d'ébauche a priori...........:", ravel(case.get('Background')))
+print("")
+print("Coefficients théoriques attendus..:", ravel((2,-1,2)))
+print("")
+print("Nombre d'itérations...............:", len(case.get('CurrentState')))
+print("Nombre de simulations.............:", len(case.get('CurrentState')))
+print("Coefficients résultants du calage.:", ravel(case.get('Analysis')[-1]))
+#
+Xa = case.get('Analysis')[-1]
+import matplotlib.pyplot as plt
+plt.rcParams['figure.figsize'] = (10, 4)
+#
+plt.figure()
+plt.plot((-5,0,1,3,10),QuadFunction(Xb),'b-',label="Simulation à l'ébauche")
+plt.plot((-5,0,1,3,10),Yobs, 'kX',label='Observation',markersize=10)
+plt.plot((-5,0,1,3,10),QuadFunction(Xa),'r-',label="Simulation à l'optimum")
+plt.legend()
+plt.title('Calage de coefficients', fontweight='bold')
+plt.xlabel('Coordonnée arbitraire')
+plt.ylabel('Observations')
+plt.savefig("simple_DerivativeFreeOptimization.png")
--- /dev/null
+Résolution du problème de calage
+--------------------------------
+
+ État intermédiaire en itération courante : [1. 1. 1.]
+ État intermédiaire en itération courante : [2. 1. 1.]
+ État intermédiaire en itération courante : [1. 2. 1.]
+ État intermédiaire en itération courante : [1. 1. 2.]
+ État intermédiaire en itération courante : [0. 1. 1.]
+ État intermédiaire en itération courante : [1. 0. 1.]
+ État intermédiaire en itération courante : [1. 1. 0.]
+ État intermédiaire en itération courante : [1.82475484 1.96682811 1.18582936]
+ État intermédiaire en itération courante : [1.89559338 0.54235283 1.17221593]
+ État intermédiaire en itération courante : [ 1.90222657 -0.20823061 1.83295831]
+ État intermédiaire en itération courante : [ 1.94478151 -0.55541624 2.76978872]
+ État intermédiaire en itération courante : [ 2.04021458 -1.49397981 2.43813988]
+ État intermédiaire en itération courante : [ 2.26677171 -0.58498556 2.84248383]
+ État intermédiaire en itération courante : [ 1.9902328 -1.04021448 2.88338819]
+ État intermédiaire en itération courante : [ 1.98695318 -0.92116383 2.47695648]
+ État intermédiaire en itération courante : [ 1.99320312 -1.02368518 2.64731215]
+ État intermédiaire en itération courante : [ 1.79473809 -0.96379959 2.44856121]
+ État intermédiaire en itération courante : [ 1.98630908 -0.91207212 2.5394595 ]
+ État intermédiaire en itération courante : [ 1.99262279 -0.97073591 2.4801914 ]
+ État intermédiaire en itération courante : [ 1.99434434 -0.99814626 2.51840027]
+ État intermédiaire en itération courante : [ 1.98838712 -0.93500739 2.44986416]
+ État intermédiaire en itération courante : [ 1.99080633 -0.94768294 2.46780354]
+ État intermédiaire en itération courante : [ 2.01588592 -0.97086338 2.47667529]
+ État intermédiaire en itération courante : [ 1.99098142 -0.96519905 2.48111896]
+ État intermédiaire en itération courante : [ 1.99157918 -0.97086422 2.47290017]
+ État intermédiaire en itération courante : [ 1.9929175 -0.98359308 2.45753186]
+ État intermédiaire en itération courante : [ 1.99550241 -1.01497755 2.43286745]
+ État intermédiaire en itération courante : [ 1.99505414 -1.00691754 2.44681334]
+ État intermédiaire en itération courante : [ 1.97993261 -1.01100857 2.43408454]
+ État intermédiaire en itération courante : [ 1.99503312 -1.0022575 2.42298652]
+ État intermédiaire en itération courante : [ 1.99337049 -0.98139127 2.3984825 ]
+ État intermédiaire en itération courante : [ 1.99387512 -0.97303786 2.33457311]
+ État intermédiaire en itération courante : [ 1.99742055 -0.99371791 2.20738214]
+ État intermédiaire en itération courante : [ 2.0002882 -0.98541744 1.96740743]
+ État intermédiaire en itération courante : [ 2.00047429 -0.99646137 2.01501424]
+ État intermédiaire en itération courante : [ 2.0009106 -1.00072301 2.00881512]
+ État intermédiaire en itération courante : [ 1.9909278 -1.00127001 2.00860374]
+ État intermédiaire en itération courante : [ 2.0009174 -1.00688459 2.01669134]
+ État intermédiaire en itération courante : [ 1.99994608 -1.00029476 2.00923274]
+ État intermédiaire en itération courante : [ 2.00031465 -1.00202777 1.98931137]
+ État intermédiaire en itération courante : [ 1.99389877 -1.00336389 2.01658192]
+ État intermédiaire en itération courante : [ 2.00003478 -1.00017674 1.99950089]
+ État intermédiaire en itération courante : [ 1.99970222 -0.99882654 1.99924329]
+ État intermédiaire en itération courante : [ 2.00000228 -0.99960552 1.99820757]
+ État intermédiaire en itération courante : [ 2.00103142 -1.0000571 1.9985047 ]
+ État intermédiaire en itération courante : [ 1.99925894 -1.0009752 1.9986288 ]
+ État intermédiaire en itération courante : [ 1.99998604 -0.99994926 2.00089583]
+ État intermédiaire en itération courante : [ 2.00000344 -0.99996363 1.9994818 ]
+ État intermédiaire en itération courante : [ 1.99980383 -0.99996386 1.9994693 ]
+ État intermédiaire en itération courante : [ 1.99998362 -0.99985392 1.99931575]
+ État intermédiaire en itération courante : [ 2.00000605 -1.00001563 1.9996749 ]
+ État intermédiaire en itération courante : [ 2.00000343 -1.00002045 1.99987483]
+ État intermédiaire en itération courante : [ 2.00000165 -1.0000257 2.00006119]
+ État intermédiaire en itération courante : [ 2.00000167 -1.00001085 1.99996478]
+
+Calage de 3 coefficients pour une forme quadratique 1D sur 5 mesures
+--------------------------------------------------------------------
+
+Vecteur d'observation.............: [ 57. 2. 3. 17. 192.]
+État d'ébauche a priori...........: [1. 1. 1.]
+
+Coefficients théoriques attendus..: [ 2 -1 2]
+
+Nombre d'itérations...............: 54
+Nombre de simulations.............: 54
+Coefficients résultants du calage.: [ 2.00000167 -1.00001085 1.99996478]
--- /dev/null
+.. index:: single: 3DVAR (exemple)
+
+Cet exemple décrit le recalage des paramètres :math:`\mathbf{x}` d'un modèle
+d'observation :math:`H` quadratique. Ce modèle est représenté ici comme une
+fonction nommée ``QuadFunction``. Cette fonction accepte en entrée le vecteur
+de coefficients :math:`\mathbf{x}`, et fournit en sortie le vecteur
+:math:`\mathbf{y}` d'évaluation du modèle quadratique aux points de contrôle
+internes prédéfinis dans le modèle. Le calage s'effectue sur la base d'un jeu
+initial de coefficients (état d'ébauche désigné par ``Xb`` dans l'exemple), et
+avec l'information :math:`\mathbf{y}^o` (désignée par ``Yobs`` dans l'exemple)
+de 5 mesures obtenues à ces mêmes points de contrôle internes. On se place en
+expériences jumelles (voir :ref:`section_methodology_twin`) et les mesures sont
+parfaites. On privilégie les observations au détriment de l'ébauche par
+l'indication d'une très importante variance d'erreur d'ébauche, ici de
+:math:`10^{6}`.
+
+L'ajustement s'effectue en affichant des résultats intermédiaires lors de
+l'optimisation itérative.
print("")
print("Coefficients théoriques attendus..:", ravel((2,-1,2)))
print("")
+print("Nombre d'itérations...............:", len(case.get('CurrentState')))
+print("Nombre de simulations.............:", len(case.get('CurrentState'))*4)
print("Coefficients résultants du calage.:", ravel(case.get('Analysis')[-1]))
#
Xa = case.get('Analysis')[-1]
Coefficients théoriques attendus..: [ 2 -1 2]
+Nombre d'itérations...............: 25
+Nombre de simulations.............: 100
Coefficients résultants du calage.: [ 2. -0.99999995 2.00000015]