Changeset 339
- Timestamp:
- 07/21/10 10:33:45 (6 years ago)
- Location:
- branches/alta/mystic-0.2a1
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alta/mystic-0.2a1/mystic_test_suite.py
r331 r339 4 4 5 5 import unittest 6 from math import * 6 7 7 8 disp = True # Flag for whether to display number of iterations and … … 1469 1470 #--------------------------------------------------------------------------------- 1470 1471 1472 ############################################################################## 1473 1471 1474 class TestVenkataraman91(unittest.TestCase): 1472 1475 """Test the optimization found in Example 9.1, page 442, in … … 1656 1659 # Note: No CRT becaues that requires a solver that uses solver.population. 1657 1660 1661 ############################################################################## 1662 1663 class TestSchwefel(unittest.TestCase): 1664 """Test Schwefel's function in 2 dimensions.""" 1665 1666 def setUp(self): 1667 from test_problems_unconstrained import schwefel 1668 self.costfunction = schwefel 1669 self.ND = 2 1670 self.expected = [420.9687]*self.ND 1671 self.min = [-500.0]*self.ND 1672 self.max = [500.0]*self.ND 1673 self.maxiter = 10000 1674 self.nplaces = -1 # Precision of answer 1675 1676 def _run_solver(self, **kwds): 1677 from mystic import Sow 1678 import numpy 1679 from mystic.tools import random_seed 1680 random_seed(123) 1681 esow = Sow() 1682 ssow = Sow() 1683 1684 solver = self.solver 1685 solver.SetRandomInitialPoints(self.min, self.max) 1686 solver.SetEvaluationLimits(maxiter=self.maxiter) 1687 solver.SetStrictRanges(self.min, self.max) 1688 solver.Solve(self.costfunction, self.term, EvaluationMonitor=esow,\ 1689 StepMonitor=ssow, **kwds) 1690 sol = solver.Solution() 1691 1692 # Verify solution is close to expected 1693 for i in range(len(sol)): 1694 self.assertAlmostEqual(sol[i], self.expected[i], self.nplaces) 1695 1696 if disp: 1697 # Print the number of iterations and function evaluations 1698 iters = len(ssow.x) 1699 func_evals = len(esow.x) 1700 print "\nNumber of iterations = ", iters 1701 print "Number of function evaluations = ", func_evals 1702 1703 def test_DifferentialEvolutionSolver_VTR(self): # Default for this solver 1704 from differential_evolution import DifferentialEvolutionSolver 1705 from mystic.termination import VTR 1706 tol = 1e-7 1707 self.NP = 100 1708 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1709 self.term = VTR()#tol) 1710 from mystic.strategy import Rand1Bin 1711 self._run_solver( strategy=Rand1Bin ) 1712 1713 def test_DifferentialEvolutionSolver_COG(self): 1714 from differential_evolution import DifferentialEvolutionSolver 1715 from mystic.termination import ChangeOverGeneration as COG 1716 tol = 1e-7 1717 self.NP = 100 1718 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1719 self.term = COG()#tol) 1720 from mystic.strategy import Rand1Bin 1721 self._run_solver( strategy=Rand1Bin ) 1722 1723 def test_DifferentialEvolutionSolver_NCOG(self): 1724 from differential_evolution import DifferentialEvolutionSolver 1725 from mystic.termination import NormalizedChangeOverGeneration as NCOG 1726 tol = 1e-7 1727 self.NP = 100 1728 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1729 self.term = NCOG()#tol) 1730 from mystic.strategy import Rand1Bin 1731 self._run_solver( strategy=Rand1Bin ) 1732 1733 def test_DifferentialEvolutionSolver_CRT(self): 1734 from differential_evolution import DifferentialEvolutionSolver 1735 from mystic.termination import CandidateRelativeTolerance as CRT 1736 tol = 1e-7 1737 self.NP = 100 1738 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1739 self.term = CRT()#tol) 1740 from mystic.strategy import Rand1Bin 1741 self._run_solver( strategy=Rand1Bin ) 1742 1743 #-------------------------------------------------------------------------- 1744 1745 def test_DifferentialEvolutionSolver2_VTR(self): # Default for this solver 1746 from differential_evolution import DifferentialEvolutionSolver2 1747 from mystic.termination import VTR 1748 tol = 1e-7 1749 self.NP = 100 1750 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1751 self.term = VTR()#tol) 1752 from mystic.strategy import Rand1Bin 1753 self._run_solver( strategy=Rand1Bin ) 1754 1755 def test_DifferentialEvolutionSolver2_COG(self): 1756 from differential_evolution import DifferentialEvolutionSolver2 1757 from mystic.termination import ChangeOverGeneration as COG 1758 tol = 1e-7 1759 self.NP = 100 1760 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1761 self.term = COG()#tol) 1762 from mystic.strategy import Rand1Bin 1763 self._run_solver( strategy=Rand1Bin ) 1764 1765 def test_DifferentialEvolutionSolver2_NCOG(self): 1766 from differential_evolution import DifferentialEvolutionSolver2 1767 from mystic.termination import NormalizedChangeOverGeneration as NCOG 1768 tol = 1e-7 1769 self.NP = 100 1770 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1771 self.term = NCOG()#tol) 1772 from mystic.strategy import Rand1Bin 1773 self._run_solver( strategy=Rand1Bin ) 1774 1775 def test_DifferentialEvolutionSolver2_CRT(self): 1776 from differential_evolution import DifferentialEvolutionSolver2 1777 from mystic.termination import CandidateRelativeTolerance as CRT 1778 tol = 1e-7 1779 self.NP = 100 1780 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1781 self.term = CRT()#tol) 1782 from mystic.strategy import Rand1Bin 1783 self._run_solver( strategy=Rand1Bin ) 1784 1785 #------------------------------------------------------------------------- 1786 1787 def test_NelderMeadSimplexSolver_CRT(self): # Default for this solver 1788 from scipy_optimize import NelderMeadSimplexSolver 1789 from mystic.termination import CandidateRelativeTolerance as CRT 1790 tol = 1e-7 1791 self.solver = NelderMeadSimplexSolver(self.ND) 1792 self.term = CRT()#tol) 1793 self._run_solver() 1794 1795 def test_NelderMeadSimplexSolver_VTR(self): 1796 from scipy_optimize import NelderMeadSimplexSolver 1797 from mystic.termination import VTR 1798 tol = 1e-7 1799 self.solver = NelderMeadSimplexSolver(self.ND) 1800 self.term = VTR()#tol) 1801 self._run_solver() 1802 1803 def test_NelderMeadSimplexSolver_COG(self): 1804 from scipy_optimize import NelderMeadSimplexSolver 1805 from mystic.termination import ChangeOverGeneration as COG 1806 tol = 1e-7 1807 self.solver = NelderMeadSimplexSolver(self.ND) 1808 self.term = COG()#tol) 1809 self._run_solver() 1810 1811 def test_NelderMeadSimplexSolver_NCOG(self): 1812 from scipy_optimize import NelderMeadSimplexSolver 1813 from mystic.termination import NormalizedChangeOverGeneration as NCOG 1814 tol = 1e-7 1815 self.solver = NelderMeadSimplexSolver(self.ND) 1816 self.term = NCOG()#tol) 1817 self._run_solver() 1818 1819 #-------------------------------------------------------------------------- 1820 1821 def test_PowellDirectionalSolver_NCOG(self): # Default for this solver 1822 from scipy_optimize import PowellDirectionalSolver 1823 from mystic.termination import NormalizedChangeOverGeneration as NCOG 1824 tol = 1e-7 1825 self.solver = PowellDirectionalSolver(self.ND) 1826 self.term = NCOG()#tol) 1827 self._run_solver() 1828 1829 def test_PowellDirectionalSolver_COG(self): 1830 from scipy_optimize import PowellDirectionalSolver 1831 from mystic.termination import ChangeOverGeneration as COG 1832 tol = 1e-7 1833 self.solver = PowellDirectionalSolver(self.ND) 1834 self.term = COG()#tol) 1835 self._run_solver() 1836 1837 def test_PowellDirectionalSolver_VTR(self): 1838 from scipy_optimize import PowellDirectionalSolver 1839 from mystic.termination import VTR 1840 tol = 1e-7 1841 self.solver = PowellDirectionalSolver(self.ND) 1842 self.term = VTR()#tol) 1843 self._run_solver() 1844 1845 # Note: No CRT becaues that requires a solver that uses solver.population. 1846 1847 ############################################################################## 1848 1849 class TestEasom(unittest.TestCase): 1850 """Test Easom's function.""" 1851 1852 def setUp(self): 1853 from test_problems_unconstrained import easom 1854 self.costfunction = easom 1855 self.ND = 2 1856 self.expected = [pi]*self.ND 1857 self.min = [-100.]*self.ND 1858 self.max = [100.]*self.ND 1859 self.maxiter = 10000 1860 self.nplaces = -1 # Precision of answer 1861 1862 def _run_solver(self, **kwds): 1863 from mystic import Sow 1864 import numpy 1865 from mystic.tools import random_seed 1866 random_seed(123) 1867 esow = Sow() 1868 ssow = Sow() 1869 1870 solver = self.solver 1871 solver.SetRandomInitialPoints(self.min, self.max) 1872 solver.SetEvaluationLimits(maxiter=self.maxiter) 1873 solver.SetStrictRanges(self.min, self.max) 1874 solver.Solve(self.costfunction, self.term, EvaluationMonitor=esow,\ 1875 StepMonitor=ssow, **kwds) 1876 sol = solver.Solution() 1877 1878 # Verify solution is close to expected 1879 for i in range(len(sol)): 1880 self.assertAlmostEqual(sol[i], self.expected[i], self.nplaces) 1881 1882 if disp: 1883 # Print the number of iterations and function evaluations 1884 iters = len(ssow.x) 1885 func_evals = len(esow.x) 1886 print "\nNumber of iterations = ", iters 1887 print "Number of function evaluations = ", func_evals 1888 1889 def test_DifferentialEvolutionSolver_VTR(self): # Default for this solver 1890 from differential_evolution import DifferentialEvolutionSolver 1891 from mystic.termination import VTR 1892 tol = 1e-7 1893 self.NP = 100 1894 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1895 self.term = VTR()#tol) 1896 from mystic.strategy import Rand1Bin 1897 self._run_solver( strategy=Rand1Bin ) 1898 1899 def test_DifferentialEvolutionSolver_COG(self): 1900 from differential_evolution import DifferentialEvolutionSolver 1901 from mystic.termination import ChangeOverGeneration as COG 1902 tol = 1e-7 1903 self.NP = 100 1904 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1905 self.term = COG()#tol) 1906 from mystic.strategy import Rand1Bin 1907 self._run_solver( strategy=Rand1Bin ) 1908 1909 def test_DifferentialEvolutionSolver_NCOG(self): 1910 from differential_evolution import DifferentialEvolutionSolver 1911 from mystic.termination import NormalizedChangeOverGeneration as NCOG 1912 tol = 1e-7 1913 self.NP = 100 1914 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1915 self.term = NCOG()#tol) 1916 from mystic.strategy import Rand1Bin 1917 self._run_solver( strategy=Rand1Bin ) 1918 1919 def test_DifferentialEvolutionSolver_CRT(self): 1920 from differential_evolution import DifferentialEvolutionSolver 1921 from mystic.termination import CandidateRelativeTolerance as CRT 1922 tol = 1e-7 1923 self.NP = 100 1924 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 1925 self.term = CRT()#tol) 1926 from mystic.strategy import Rand1Bin 1927 self._run_solver( strategy=Rand1Bin ) 1928 1929 #-------------------------------------------------------------------------- 1930 1931 def test_DifferentialEvolutionSolver2_VTR(self): # Default for this solver 1932 from differential_evolution import DifferentialEvolutionSolver2 1933 from mystic.termination import VTR 1934 tol = 1e-7 1935 self.NP = 100 1936 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1937 self.term = VTR()#tol) 1938 from mystic.strategy import Rand1Bin 1939 self._run_solver( strategy=Rand1Bin ) 1940 1941 def test_DifferentialEvolutionSolver2_COG(self): 1942 from differential_evolution import DifferentialEvolutionSolver2 1943 from mystic.termination import ChangeOverGeneration as COG 1944 tol = 1e-7 1945 self.NP = 100 1946 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1947 self.term = COG()#tol) 1948 from mystic.strategy import Rand1Bin 1949 self._run_solver( strategy=Rand1Bin ) 1950 1951 def test_DifferentialEvolutionSolver2_NCOG(self): 1952 from differential_evolution import DifferentialEvolutionSolver2 1953 from mystic.termination import NormalizedChangeOverGeneration as NCOG 1954 tol = 1e-7 1955 self.NP = 100 1956 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1957 self.term = NCOG()#tol) 1958 from mystic.strategy import Rand1Bin 1959 self._run_solver( strategy=Rand1Bin ) 1960 1961 def test_DifferentialEvolutionSolver2_CRT(self): 1962 from differential_evolution import DifferentialEvolutionSolver2 1963 from mystic.termination import CandidateRelativeTolerance as CRT 1964 tol = 1e-7 1965 self.NP = 100 1966 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 1967 self.term = CRT()#tol) 1968 from mystic.strategy import Rand1Bin 1969 self._run_solver( strategy=Rand1Bin ) 1970 1971 #------------------------------------------------------------------------- 1972 1973 def test_NelderMeadSimplexSolver_CRT(self): # Default for this solver 1974 from scipy_optimize import NelderMeadSimplexSolver 1975 from mystic.termination import CandidateRelativeTolerance as CRT 1976 tol = 1e-7 1977 self.solver = NelderMeadSimplexSolver(self.ND) 1978 self.term = CRT()#tol) 1979 self._run_solver() 1980 1981 def test_NelderMeadSimplexSolver_VTR(self): 1982 from scipy_optimize import NelderMeadSimplexSolver 1983 from mystic.termination import VTR 1984 tol = 1e-7 1985 self.solver = NelderMeadSimplexSolver(self.ND) 1986 self.term = VTR()#tol) 1987 self._run_solver() 1988 1989 def test_NelderMeadSimplexSolver_COG(self): 1990 from scipy_optimize import NelderMeadSimplexSolver 1991 from mystic.termination import ChangeOverGeneration as COG 1992 tol = 1e-7 1993 self.solver = NelderMeadSimplexSolver(self.ND) 1994 self.term = COG()#tol) 1995 self._run_solver() 1996 1997 def test_NelderMeadSimplexSolver_NCOG(self): 1998 from scipy_optimize import NelderMeadSimplexSolver 1999 from mystic.termination import NormalizedChangeOverGeneration as NCOG 2000 tol = 1e-7 2001 self.solver = NelderMeadSimplexSolver(self.ND) 2002 self.term = NCOG()#tol) 2003 self._run_solver() 2004 2005 #-------------------------------------------------------------------------- 2006 2007 def test_PowellDirectionalSolver_NCOG(self): # Default for this solver 2008 from scipy_optimize import PowellDirectionalSolver 2009 from mystic.termination import NormalizedChangeOverGeneration as NCOG 2010 tol = 1e-7 2011 self.solver = PowellDirectionalSolver(self.ND) 2012 self.term = NCOG()#tol) 2013 self._run_solver() 2014 2015 def test_PowellDirectionalSolver_COG(self): 2016 from scipy_optimize import PowellDirectionalSolver 2017 from mystic.termination import ChangeOverGeneration as COG 2018 tol = 1e-7 2019 self.solver = PowellDirectionalSolver(self.ND) 2020 self.term = COG()#tol) 2021 self._run_solver() 2022 2023 def test_PowellDirectionalSolver_VTR(self): 2024 from scipy_optimize import PowellDirectionalSolver 2025 from mystic.termination import VTR 2026 tol = 1e-7 2027 self.solver = PowellDirectionalSolver(self.ND) 2028 self.term = VTR()#tol) 2029 self._run_solver() 2030 2031 # Note: No CRT becaues that requires a solver that uses solver.population. 2032 2033 ############################################################################## 2034 2035 class TestRotatedEllipsoid(unittest.TestCase): 2036 """Test the rotated ellipsoid function in 2 dimensions.""" 2037 2038 def setUp(self): 2039 from test_problems_unconstrained import rotated_ellipsoid 2040 self.costfunction = rotated_ellipsoid 2041 self.ND = 2 2042 self.expected = [0.]*self.ND 2043 self.min = [-65.536]*self.ND 2044 self.max = [65.536]*self.ND 2045 self.maxiter = 10000 2046 self.nplaces = -1 # Precision of answer 2047 2048 def _run_solver(self, **kwds): 2049 from mystic import Sow 2050 import numpy 2051 from mystic.tools import random_seed 2052 random_seed(123) 2053 esow = Sow() 2054 ssow = Sow() 2055 2056 solver = self.solver 2057 solver.SetRandomInitialPoints(self.min, self.max) 2058 solver.SetEvaluationLimits(maxiter=self.maxiter) 2059 solver.SetStrictRanges(self.min, self.max) 2060 solver.Solve(self.costfunction, self.term, EvaluationMonitor=esow,\ 2061 StepMonitor=ssow, **kwds) 2062 sol = solver.Solution() 2063 2064 # Verify solution is close to expected 2065 for i in range(len(sol)): 2066 self.assertAlmostEqual(sol[i], self.expected[i], self.nplaces) 2067 2068 if disp: 2069 # Print the number of iterations and function evaluations 2070 iters = len(ssow.x) 2071 func_evals = len(esow.x) 2072 print "\nNumber of iterations = ", iters 2073 print "Number of function evaluations = ", func_evals 2074 2075 def test_DifferentialEvolutionSolver_VTR(self): # Default for this solver 2076 from differential_evolution import DifferentialEvolutionSolver 2077 from mystic.termination import VTR 2078 tol = 1e-7 2079 self.NP = 100 2080 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 2081 self.term = VTR()#tol) 2082 from mystic.strategy import Rand1Bin 2083 self._run_solver( strategy=Rand1Bin ) 2084 2085 def test_DifferentialEvolutionSolver_COG(self): 2086 from differential_evolution import DifferentialEvolutionSolver 2087 from mystic.termination import ChangeOverGeneration as COG 2088 tol = 1e-7 2089 self.NP = 100 2090 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 2091 self.term = COG()#tol) 2092 from mystic.strategy import Rand1Bin 2093 self._run_solver( strategy=Rand1Bin ) 2094 2095 def test_DifferentialEvolutionSolver_NCOG(self): 2096 from differential_evolution import DifferentialEvolutionSolver 2097 from mystic.termination import NormalizedChangeOverGeneration as NCOG 2098 tol = 1e-7 2099 self.NP = 100 2100 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 2101 self.term = NCOG()#tol) 2102 from mystic.strategy import Rand1Bin 2103 self._run_solver( strategy=Rand1Bin ) 2104 2105 def test_DifferentialEvolutionSolver_CRT(self): 2106 from differential_evolution import DifferentialEvolutionSolver 2107 from mystic.termination import CandidateRelativeTolerance as CRT 2108 tol = 1e-7 2109 self.NP = 100 2110 self.solver = DifferentialEvolutionSolver(self.ND, self.NP) 2111 self.term = CRT()#tol) 2112 from mystic.strategy import Rand1Bin 2113 self._run_solver( strategy=Rand1Bin ) 2114 2115 #-------------------------------------------------------------------------- 2116 2117 def test_DifferentialEvolutionSolver2_VTR(self): # Default for this solver 2118 from differential_evolution import DifferentialEvolutionSolver2 2119 from mystic.termination import VTR 2120 tol = 1e-7 2121 self.NP = 100 2122 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 2123 self.term = VTR()#tol) 2124 from mystic.strategy import Rand1Bin 2125 self._run_solver( strategy=Rand1Bin ) 2126 2127 def test_DifferentialEvolutionSolver2_COG(self): 2128 from differential_evolution import DifferentialEvolutionSolver2 2129 from mystic.termination import ChangeOverGeneration as COG 2130 tol = 1e-7 2131 self.NP = 100 2132 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 2133 self.term = COG()#tol) 2134 from mystic.strategy import Rand1Bin 2135 self._run_solver( strategy=Rand1Bin ) 2136 2137 def test_DifferentialEvolutionSolver2_NCOG(self): 2138 from differential_evolution import DifferentialEvolutionSolver2 2139 from mystic.termination import NormalizedChangeOverGeneration as NCOG 2140 tol = 1e-7 2141 self.NP = 100 2142 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 2143 self.term = NCOG()#tol) 2144 from mystic.strategy import Rand1Bin 2145 self._run_solver( strategy=Rand1Bin ) 2146 2147 def test_DifferentialEvolutionSolver2_CRT(self): 2148 from differential_evolution import DifferentialEvolutionSolver2 2149 from mystic.termination import CandidateRelativeTolerance as CRT 2150 tol = 1e-7 2151 self.NP = 100 2152 self.solver = DifferentialEvolutionSolver2(self.ND, self.NP) 2153 self.term = CRT()#tol) 2154 from mystic.strategy import Rand1Bin 2155 self._run_solver( strategy=Rand1Bin ) 2156 2157 #------------------------------------------------------------------------- 2158 2159 def test_NelderMeadSimplexSolver_CRT(self): # Default for this solver 2160 from scipy_optimize import NelderMeadSimplexSolver 2161 from mystic.termination import CandidateRelativeTolerance as CRT 2162 tol = 1e-7 2163 self.solver = NelderMeadSimplexSolver(self.ND) 2164 self.term = CRT()#tol) 2165 self._run_solver() 2166 2167 def test_NelderMeadSimplexSolver_VTR(self): 2168 from scipy_optimize import NelderMeadSimplexSolver 2169 from mystic.termination import VTR 2170 tol = 1e-7 2171 self.solver = NelderMeadSimplexSolver(self.ND) 2172 self.term = VTR()#tol) 2173 self._run_solver() 2174 2175 def test_NelderMeadSimplexSolver_COG(self): 2176 from scipy_optimize import NelderMeadSimplexSolver 2177 from mystic.termination import ChangeOverGeneration as COG 2178 tol = 1e-7 2179 self.solver = NelderMeadSimplexSolver(self.ND) 2180 self.term = COG()#tol) 2181 self._run_solver() 2182 2183 def test_NelderMeadSimplexSolver_NCOG(self): 2184 from scipy_optimize import NelderMeadSimplexSolver 2185 from mystic.termination import NormalizedChangeOverGeneration as NCOG 2186 tol = 1e-7 2187 self.solver = NelderMeadSimplexSolver(self.ND) 2188 self.term = NCOG()#tol) 2189 self._run_solver() 2190 2191 #-------------------------------------------------------------------------- 2192 2193 def test_PowellDirectionalSolver_NCOG(self): # Default for this solver 2194 from scipy_optimize import PowellDirectionalSolver 2195 from mystic.termination import NormalizedChangeOverGeneration as NCOG 2196 tol = 1e-7 2197 self.solver = PowellDirectionalSolver(self.ND) 2198 self.term = NCOG()#tol) 2199 self._run_solver() 2200 2201 def test_PowellDirectionalSolver_COG(self): 2202 from scipy_optimize import PowellDirectionalSolver 2203 from mystic.termination import ChangeOverGeneration as COG 2204 tol = 1e-7 2205 self.solver = PowellDirectionalSolver(self.ND) 2206 self.term = COG()#tol) 2207 self._run_solver() 2208 2209 def test_PowellDirectionalSolver_VTR(self): 2210 from scipy_optimize import PowellDirectionalSolver 2211 from mystic.termination import VTR 2212 tol = 1e-7 2213 self.solver = PowellDirectionalSolver(self.ND) 2214 self.term = VTR()#tol) 2215 self._run_solver() 2216 2217 # Note: No CRT becaues that requires a solver that uses solver.population. 2218 2219 1658 2220 #--------------------------------------------------------------------------------- 1659 2221 … … 1667 2229 suite7 = unittest.TestLoader().loadTestsFromTestCase(TestGriewangk) 1668 2230 suite8 = unittest.TestLoader().loadTestsFromTestCase(TestPeaks) 1669 suite9 = unittest.TestLoader().loadTestsFromTestCase(TestVenkataraman91) 2231 suite9 = unittest.TestLoader().loadTestsFromTestCase(TestVenkataraman91) 2232 suite10 = unittest.TestLoader().loadTestsFromTestCase(TestSchwefel) 2233 suite11 = unittest.TestLoader().loadTestsFromTestCase(TestEasom) 2234 suite12 = unittest.TestLoader().loadTestsFromTestCase(TestRotatedEllipsoid) 1670 2235 # Comment out suites in the list below to test specific test cost functions only 1671 2236 # (Testing all the problems will take some time) 1672 2237 allsuites = unittest.TestSuite([suite1, suite2, suite3, suite4, suite5, \ 1673 suite6, suite7, suite8, suite9]) 2238 suite6, suite7, suite8, suite9, suite10, \ 2239 suite11, suite12]) 1674 2240 unittest.TextTestRunner(verbosity=2).run(allsuites) -
branches/alta/mystic-0.2a1/test_constraints_cvxopt_qp.py
r338 r339 49 49 print soln.xf 50 50 Solution: {x1: array([ 0.2500267]), x2: array([ 0.7499733])} 51 52 Scipy.optimize.fmin_slsqp code and results: 53 ------------------------------------------- 54 from scipy.optimize import fmin_slsqp 55 def func(x): 56 x1 = x[0] 57 x2 = x[1] 58 return 2.*x1**2 + x2**2 + x1*x2 + x1 + x2 59 60 def ineqcon1(x): 61 return x[0] 62 63 def ineqcon2(x): 64 return x[1] 65 66 def eqcon1(x): 67 return x[0] + x[1] - 1.0 68 69 x0 = [1., 1.] 70 71 print fmin_slsqp(func, x0, eqcons=[eqcon1], ieqcons=[ineqcon1, ineqcon2]) 72 Solution: [ 0.25 0.75] 51 73 """ 52 74 … … 75 97 # Wrap the constraints to the cost function 76 98 from constraint_tools import wrap_constraints_to_cost 77 wrapped_costfunc = wrap_constraints_to_cost(constraints_string, ndim, costfunc) 99 wrapped_costfunc = wrap_constraints_to_cost(constraints_string, ndim, costfunc,\ 100 penalty=1e7) 78 101 79 102 # Solve the constrained optimization problem with a one-line interface -
branches/alta/mystic-0.2a1/test_problems_unconstrained.py
r338 r339 96 96 inverted for minimization.' - http://www.geatbx.com/docu/fcnindex-01.html 97 97 Global minimum f(x1, x2) = -1 at (x1, x2) = (pi, pi) 98 xi in -100, 100. 2-dimensional."""98 xi in [-100, 100]. 2-dimensional.""" 99 99 x1 = x[0] 100 100 x2 = x[1] 101 return -cos(x1)*cos(x2)*exp(-(x1-pi)**2 +(x2 - pi)**2)101 return -cos(x1)*cos(x2)*exp(-(x1-pi)**2 - (x2 - pi)**2) 102 102 103 103 #print "Easom:", easom([pi, pi]) 104 104 105 def goldstein_price(x): 106 """Goldstein-Price function. Global minimum f(x1, x2) = 3 at 107 (x1, x2) = (0, -1) 108 x1, x2 in [-2., 2.] 109 2-dimensional. http://www.geatbx.com/docu/fcnindex-01.html""" 110 x1 = x[0] 111 x2 = x[1] 112 return (1.+(x1+x2+1.)**2*(19.-14.*x1+3.*x1**2-14.*x2+6.*x1*x2+3.*x2**2))*\ 113 (30.+(2.*x1-3.*x2)**2*(18.-32.*x1+12.*x1**2+48.*x2-36.*x1*x2+\ 114 27.*x2**2)) 115 116 #print "Goldstein-Price:", goldstein_price([0., -1.]) 117 118 def rotated_ellipsoid(x): 119 """'An extension of the axis parallel hyper-ellipsoid is Schwefel's 120 function1.2. With respect to the coordinate axes, this function produces 121 rotated hyper-ellipsoids. It is continuous, convex and unimodal.' 122 - http://www.geatbx.com/docu/fcnindex-01.html 123 xi in [-65.536, 65.536]. n-dimensional. 124 global minimum f(x)=0 at xi=0.""" 125 result = 0. 126 for i in range(len(x)): 127 s = 0. 128 for j in range(i+1): 129 s += x[j] 130 result += s**2 131 return result 132 133 #print "Rotated Ellipsoid:", rotated_ellipsoid([0., 0.]) 134
Note: See TracChangeset
for help on using the changeset viewer.