source: coopr.opt/trunk/coopr/opt/tests/colin/test_ps.py @ 1918

Last change on this file since 1918 was 1918, checked in by wehart, 10 years ago

Rework of the SolverResults? object, to provide for a more explicit
definition of solver results, and to support loading of
solver results from YAML-formated data.

File size: 3.5 KB
Line 
1#
2# Unit Tests for coopr.opt.colin.ps
3#
4#
5
6import os
7import sys
8from os.path import abspath, dirname
9cooprdir = dirname(dirname(dirname(dirname(abspath(__file__)))))
10sys.path.insert(0, cooprdir)
11cooprdir += os.sep
12currdir = dirname(abspath(__file__))+os.sep
13
14import unittest
15from nose.tools import nottest
16import xml
17import coopr.opt
18from coopr.opt import ResultsFormat, ProblemFormat
19import pyutilib.services
20import pyutilib.th
21
22
23class TestProblem1(coopr.opt.colin.MixedIntOptProblem):
24
25    def __init__(self):
26        coopr.opt.colin.MixedIntOptProblem.__init__(self)
27        self.real_lower=[0.0, -1.0, 1.0, None]
28        self.real_upper=[None, 0.0, 2.0, -1.0]
29        self.nreal=4
30
31    def function_value(self, point):
32        self.validate(point)
33        return point.reals[0] - point.reals[1] + (point.reals[2]-1.5)**2 + (point.reals[3]+2)**4
34
35
36
37
38class OptPatternSearchDebug(pyutilib.th.TestCase):
39
40    def setUp(self):
41        self.do_setup(False)
42        pyutilib.services.TempfileManager.tempdir = currdir
43
44    def do_setup(self,flag):
45        pyutilib.services.TempfileManager.tempdir = currdir
46        self.ps = coopr.opt.colin.PatternSearch()
47
48    def tearDown(self):
49        pyutilib.services.TempfileManager.clear_tempfiles()
50
51    def test_solve1(self):
52        """ Test PatternSearch - test1.mps """
53        problem=TestProblem1()
54        self.ps.problem=problem
55        self.ps.initial_point = [1.0, -0.5, 2.0, -1.0]
56        self.ps.reset()
57        results = self.ps.solve(logfile=currdir+"test_solve1.log")
58        results.write(filename=currdir+"test_solve1.txt",times=False)
59        self.failUnlessFileEqualsBaseline(currdir+"test_solve1.txt", currdir+"test1_ps.txt")
60        if os.path.exists(currdir+"test_solve1.log"):
61            os.remove(currdir+"test_solve1.log")
62
63    def test_solve2(self):
64        """ Test PatternSearch - test1.mps """
65        problem=TestProblem1()
66        self.ps.problem=problem
67        self.ps.initial_point = [1.0, -0.5, 2.0, -1.0]
68        self.ps.reset()
69        self.ps.min_function_value = 1e-1
70        results = self.ps.solve(logfile=currdir+"test_solve2.log")
71        results.write(filename=currdir+"test_solve2.txt",times=False)
72        self.failUnlessFileEqualsBaseline(currdir+"test_solve2.txt", currdir+"test2_ps.txt")
73        if os.path.exists(currdir+"test_solve2.log"):
74            os.remove(currdir+"test_solve2.log")
75
76    def test_error1(self):
77        """ An error is generated when no initial point is provided """
78        problem=TestProblem1()
79        self.ps.problem=problem
80        try:
81            self.ps.reset()
82            self.fail("Excepted ValueError")
83        except ValueError:
84            pass
85
86    def test_error2(self):
87        """ An error is generated when the initial point is infeasible """
88        problem=TestProblem1()
89        self.ps.problem=problem
90        self.ps.initial_point = [1.0, 0.5, 2.0, -1.0]
91        self.failUnlessEqual(self.ps.feasible(self.ps.initial_point), True)
92        self.real_lower=[0.0, -1.0, 1.0, None]
93        self.real_upper=[None, 0.0, 2.0, -1.0]
94        try:
95            self.ps.reset()
96            self.fail("Excepted ValueError")
97        except ValueError:
98            pass
99        self.ps.initial_point = [-1.0, -0.5, 2.0, -1.0]
100        self.real_lower=[0.0, -1.0, 1.0, None]
101        self.real_upper=[None, 0.0, 2.0, -1.0]
102        try:
103            self.ps.reset()
104            self.fail("Excepted ValueError")
105        except ValueError:
106            pass
107       
108
109if __name__ == "__main__":
110    unittest.main()
111
Note: See TracBrowser for help on using the repository browser.