File: gplot_test_ffit.py

package info (click to toggle)
mystic 0.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,656 kB
  • sloc: python: 40,894; makefile: 33; sh: 9
file content (57 lines) | stat: -rwxr-xr-x 2,018 bytes parent folder | download
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
57
#!/usr/bin/env python
#
# Author: Patrick Hung (patrickh @caltech)
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 1997-2016 California Institute of Technology.
# Copyright (c) 2016-2024 The Uncertainty Quantification Foundation.
# License: 3-clause BSD.  The full license text is available at:
#  - https://github.com/uqfoundation/mystic/blob/master/LICENSE
"""
Adapted from DETest.py by Patrick Hung

Sets up Storn and Price's Polynomial 'Fitting' Problem.

Exact answer: Chebyshev Polynomial of the first kind. T8(x)

Reference:

[1] Storn, R. and Price, K. Differential Evolution - A Simple and Efficient
Heuristic for Global Optimization over Continuous Spaces. Journal of Global
Optimization 11: 341-359, 1997.
"""
from test_ffit import *

def plot_solution(func):
    try:
        import Gnuplot, numpy
        g = Gnuplot.Gnuplot(debug = 1)
        x = numpy.arange(-1.2, 1.2001, 0.01)
        x2 = numpy.array([-1.0, 1.0])
        p = poly1d(func)
        chebyshev = poly1d(Chebyshev8)
        y = p(x)
        exact = chebyshev(x)
        # g('set style line lw 5')
        g('set xrange[-1.4:1.4]')
        g('set yrange[-2:8]')
        g.plot(Gnuplot.Data(x, y, with_='line', inline=1, title="Storn and Price's Function Fitting problem"), \
               Gnuplot.Data(x, exact, with_='l 4 4'), \
               Gnuplot.Data(x, 0*x-1, with_='l 2 2'), \
               Gnuplot.Data(x2, 0*x2+1, with_='l 2 2'),  \
               Gnuplot.Data([-1.2, -1.2], [-1, 10], with_='l 2 2'),  \
               Gnuplot.Data([1.2, 1.2], [-1, 10], with_='l 2 2'),  \
               Gnuplot.Data([-1.0, -1.0], [1, 10], with_='l 2 2'),  \
               Gnuplot.Data([1.0, 1.0], [1, 10], with_='l 2 2')  \
               )
        getch('Press any key to exit plot')
    except ImportError:
        print("Install Gnuplot and Gnuplot.py for visualization")
        pass


if __name__ == '__main__':
    solution = main()
    print_solution(solution)
    plot_solution(solution)

# end of file