OKlibrary  0.2.1.6
Minimisation.mac
Go to the documentation of this file.
00001 /* Oliver Kullmann, 23.1.2008 (Swansea) */
00002 /* Copyright 2008 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00022 /* ******************************
00023    * Constrained minimisation    *
00024    ******************************
00025 */
00026 
00027 /* Given a function f in n arguments and a list "bounds" of length n with
00028    elements [xi_begin, xi_end], run through all x-values where each
00029    interval is devided into N parts, and determine the minimum amongst
00030    all function-values.
00031    Returns a pair [f(x_min), x_min].
00032 */
00033 
00034 monitorcheck_min_scanning(name) := 
00035  if oklib_monitor and length(bounds) > 0 then (
00036   print(sconcat("M[",name,"]:"), 
00037     "Entry (", orig_n, "dimensions, N =", N, ")."))$
00038 monitorcheck_min_scanning_loop(name) := if oklib_monitor then (
00039   if orig_n - n <= oklib_monitor_level - 1 then (
00040     print(sconcat("M[",name,"]: LOOP "), 
00041       "dim:", n, ", x:", float(x), ", best:", float(best), 
00042       ", cur_min:", float(cur_min))))$
00043 
00044 kill(f)$
00045 min_scanning(f, bounds, N) := block([orig_n : length(bounds)],
00046   monitorcheck_min_scanning("min_scanning"),
00047   min_scanning_aux(f, bounds, N))$
00048 min_scanning_aux(f, bounds, N) := block([n : length(bounds)],
00049   if n = 0 then return([apply(f,[]),[]]),
00050   block([begin : bounds[1][1], end : bounds[1][2], cur_min : inf, delta, best],
00051    delta : (end - begin) / N,
00052    for i : 0 thru N do block(
00053     [x : begin + i * delta, val],
00054      val : min_scanning_aux(
00055              buildq([x,f],lambda([[y]],apply(f,cons(x,y)))), 
00056              rest(bounds), 
00057              N),
00058      if val[1] < cur_min then (
00059        cur_min : val[1], best : cons(x, val[2])
00060      ),
00061      monitorcheck_min_scanning_loop("min_scanning")
00062    ),
00063   return([cur_min, best])))$
00064   
00065