def currencytest(): """find the smallest number of whole currency units no more than 3 cents less than $1199.03 (within 1% of optimal)""" # set the goal in dollars, and discrepancy allowed goalamount = 1199.03 error_dollars = 0.03 # create variables for the model v = VariableNamer() dollar = v.dollar; franc = v.franc; yen = v.yen pound = v.pound; mark = v.mark; peseta = v.peseta yuan = v.yuan; ruble = v.ruble; candollar = v.candollar diff = v.diff; currencyvalue = v.currencyvalue # jun 5 1998, conversion rates yendollar = 1/138.0; poundollar = 1/0.61; francdollar = 1/5.74 markdollar = 1/1.78; pesetadollar = 1/151.0 yuandollar = 1/8.28; rubledollar = 1/6.0; candollardollar = 1/1.46 # define equalities for amount and discrepancy amount = NamedTransform(v, diff = goalamount - currencyvalue, currencyvalue = +dollar +yendollar*yen +poundollar*pound +francdollar* +markdollar*mark +pesetadollar*peseta +yuandollar*yuan +rubledollar*ruble +candollardollar*candollar ) # define error tolerance error_allowed = all_positive(v, error_dollars-diff) constraints = amount & error_allowed print "constraints" print constraints # define the objective: minimize number of whole units units = dollar+yen+pound+franc+mark+peseta+yuan+ruble+candollar # minimize (s,o) = constraints.nsimplex( -units, integervars= [franc, pound, yen, dollar, mark, peseta, yuan, ruble, candollar], near=0.01) # print a report on minimization sub = s({}) items = sub.items() items.sort() for (a,b) in items: if b>0.01: print "hand the customer %s %s's" % (b,a) total = sub["currencyvalue"] print "total is worth %s dollars" % total print "units", units.evalconst(sub)When evaluated this function prints

hand the customer 1199.00875109 currencyvalue's hand the customer 0.0212489110635 diff's hand the customer 2.0 dollar's hand the customer 730.0 pound's hand the customer 1.0 ruble's hand the customer 1.0 yuan's total is worth 1199.00875109 dollars units 734.0Thus 2 dollars and 730 pounds and 1 ruble and 1 yuan sum to value 1199.0087 which is within 3 cents of 1199.03, and these 734 currency units are within 7 units of the least number of currency units with value within 3 cents less than 1199.03. (In fact rerunning the function with near=0 shows that this is the optimal such number of units. Note that reducing the error tolerance to 1 cent makes it a noticably harder, i.e., slower, problem.) Requirements ============ This software requires Python and the Python Numeric extensions. Easy installations for Python and the Numeric extensions may be obtained via links starting at http://www.python.org for Windows 95, NT and many other platforms. Thanks ====== Many thanks to Guido van Rossum, Jim Hugunin (the primary developers of Python and Numpy respectively) as well as the other contributors to Numpy, especially Dave Ascher and Paul F. Dubois, and to John W. Gregory and Robert Fourer of the Linear Programming FAQ, as well as to George Dantzig pioneer of the simplex method. -- Aaron Watters === Yet what are such gaieties to me Whose thoughts are full of indices and surds? x**2 + 7*x + 53 = 11/3 -- Lewis Carroll From hinsen@cnrs-orleans.fr Fri Jun 5 23:02:08 1998 From: hinsen@cnrs-orleans.fr (hinsen@cnrs-orleans.fr) Date: Fri, 5 Jun 1998 17:02:08 -0500 (CDT) Subject: [Matrix-SIG] Re: [PSA MEMBERS] Numeric Python Set function In-Reply-To: