Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – a constrained optimization problem


The following example is adapted from a textbook on operations research:

#Ronald L. Rardin. "Optimization in Operations Research."  
# Prentice-Hall, Upper Saddle River, NJ, 1998.  Example 14.3, p. 792

# Global constants
d1 = 2.5
d2 = 40
t1 = 0.6
t2 = 1.0
p0 = 200

initial_guess = [20, 500]

def x3(x):
    return 36.25 * (d2 - x[0]) * (t2 - t1) / t1 * log(x[1] / p0)
def x4(x):
    return 348300 * (d2 - x[0]) * (t2 - t1)/ x[1]
    
def f(x):
    return 61.8 + 5.72 * x[0] + 0.0175 * x3(x)^0.85 + \
        0.0094 * x4(x)^0.75 + 0.006 * t1 * x3(x)
    
c_1 = lambda p: p[0]
c_2 = lambda p: p[1]
c_3 = lambda p: t2 * p[0] - d1 * t1 - d2 * (t2 - t1)
c_4 = lambda p: p[1] - p0

(x1, x2) = minimize_constrained(f, [c_1,c_2,c_3,c_4], initial_guess)

print('x1 = {0}'.format(x1))
print('x2 = {0}'.format(x2))
print('x3 = {0}'.format(x3([x1,x2])))
print('x4 = {0}'.format(x4([x1,x2])))
print('x5 = {0}'.format(f([x1,x2])))

The fitted values are:

What just...