# source:coopr.colin/stable/2.3/examples/colin/colin/example_fn3@2353

Last change on this file since 2353 was 2184, checked in by wehart, 11 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_value: a quadratic function of real, integer and binary vars, with
8#
9# nonlinear_constraint_values: two constraints, which are respectively
10#       quadratic functions of the real and integer vars
11#
12
13import sys
14import random
15from coopr.colin import *
16
17class Problem(OptProblem):
18
19    def __init__(self):
20        self.response_types = [response_enum.FunctionValue, \
21                                response_enum.NonlinearConstraintValues]
22
23    def function_value(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        val = val + random.uniform(-1,1)
32        return val
33
34    def nonlinear_constraint_values(self, point):
35        val=[]
36        tmp=0
37        for i in range(0,len(point.reals)):
38            tmp = tmp + (point.reals[i] - 10.0)*(point.reals[i] - 10.0)
39        val.append(tmp)
40        tmp=0
41        for i in range(0,len(point.ints)):
42            tmp = tmp + (point.ints[i] - 10)*(point.ints[i] - 10)
43        val.append(tmp)
44        return val
45
46
47instance = Problem()
48instance.main(sys.argv)
Note: See TracBrowser for help on using the repository browser.