source: coopr.colin/trunk/examples/colin/colin/example_fn2 @ 2184

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

Renaming this examples directory, and editing
the test scripts.

  • Property svn:executable set to *
File size: 1.5 KB
Line 
1#! /usr/bin/env python
2#
3# A python script that reads an XML input file, computes a simple function,
4# and writes out an XML file.
5#
6# function_values: returns two function values: (1) a quadratic function
7#       of real, integer and binary vars, and (2) the reciprocal of the first
8#       function
9#
10# nonlinear_constraint_values: two constraints, which are respectively
11#       quadratic functions of the real and integer vars
12#
13
14import sys
15from coopr.colin import *
16
17class Problem(OptProblem):
18
19    def __init__(self):
20        self.response_types = [response_enum.FunctionValues, \
21                                response_enum.NonlinearConstraintValues]
22
23    def function_values(self, point):
24        val=0.0
25        for i in range(0,len(point.reals)):
26            val = val + (point.reals[i] - 3.0)*(point.reals[i] - 3.0)
27        for i in range(0,len(point.ints)):
28            val = val + (point.ints[i] - 2)*(point.ints[i] - 2)
29        for i in range(0,len(point.bits)):
30            val = val + (point.bits[i] - 1)*(point.bits[i] - 1)
31        return [val,1/val]
32
33    def nonlinear_constraint_values(self, point):
34        val=[]
35        tmp=0
36        for i in range(0,len(point.reals)):
37            tmp = tmp + (point.reals[i] - 10.0)*(point.reals[i] - 10.0)
38        val.append(tmp)
39        tmp=0
40        for i in range(0,len(point.ints)):
41            tmp = tmp + (point.ints[i] - 10)*(point.ints[i] - 10)
42        val.append(tmp)
43        return val
44
45
46instance = Problem()
47instance.main(sys.argv)
Note: See TracBrowser for help on using the repository browser.