Learn R Programming

CHNOSZ (version 1.0.0)

findit: Gridded Search to Optimize Objective Functions

Description

Use a gridded search to find a combination of one or more of chemical activities of basis species, temperature and/or pressure that maximize or minimize a objective function of the metastable equilibrium chemical activities of the species of interest.

Usage

findit (lims = list(), objective = "CV", niter = NULL, iprotein = NULL, 
    plot.it = TRUE, T = 25, P = "Psat", res = NULL, labcex = 0.6, 
    loga2 = NULL, loga.balance = 0, rat = NULL,
    balance = NULL, normalize = FALSE)
  ## S3 method for class 'findit':
plot(x, which=NULL, mar=c(3.5,5,2,2), xlab="iteration", ...)

Arguments

lims
list, specification of search limits
objective
character, name of objective function to optimize
niter
numeric, number of iterations
res
numeric, grid resolution (number of points on one edge)
iprotein
numeric, indices of proteins
plot.it
logical, make a plot?
T
numeric, temperature
P
numeric, pressure; or character, "Psat"
labcex
numeric, character expansion for plot labels
loga2
numeric, reference logarithms of activity of species
loga.balance
numeric, logarithm of total activity of balanced quantity (passed to diagram)
rat
numeric, ratio of edge length in successive iterations
balance
character or numeric, balanced quantity (passed to diagram)
normalize
logical, normalize chemical formulas by the balance vector? (passed to diagram)
x
list, object of class findit
which
numeric, which of the parameters to plot
mar
numeric, plot margin specification
xlab
character, x-axis label
...
additional arguments passed to plot

Value

  • findit returns a list having class findit with elements value (values of the parameters, and value of the objective function, at each iteration), lolim (lower limits of the parameters) and hilim (upper limits of the parameters).

Details

findit implements a gridded optimization to find the minimum or maximum value of an objective function. The variables are one or more of the chemical activities, temperature and/or pressure whose ranges are listed in lims. Generally, the system (basis species and species of interest) must be set up before calling this function. If iprotein is supplied, indicating a set of proteins to use in the calculation, the definition of the species is not required. lims is a list, each element of which is vector having a name that is the formula of one of the basis species, T or P and a pair of values indicating the range of the named parameter. The values are the logarithms of activities of the basis species, or temperature or pressure (in the user's units; see util.units). If either T or P is missing from the list in lims, the calculations are performed at isothermal and/or isobaric conditions indicated by T and P arguments.

Taking $nd$ as the number of dimensions (number of variables in lims), default values of niter and res come from the following table. These settings have been selected to be able to run the function quickly in the higher dimensions. Detailed studies of a system might have to use more iterations and/or higher resolutions.

rrrrr{ nd niter res grid points (res^nd) rat 1 4 128 128 0.7 2 6 64 4096 0.7 3 6 16 4096 0.8 4 8 8 4096 0.9 5 12 6 7776 0.9 6 12 4 4096 0.95 7 12 4 16384 0.95 }

The function performs niter iterations. At first, the limits of the parameters given in lims define the extent of a $nd$-dimensional box around the space of interest. The value of objective is calculated at each of the $res^{nd}$ grid points and and optimum value located (see revisit and optimal.index). In the next iteration the new search box is centered on the location of the optimum value, and the edges are shrunk so their length is rat * the length in the previous step. If the limits of any of the parameters extend beyond those in lims, they are pushed in to fit (preserving the difference between them).

plot.findit plots the values of the parameters and the objective function as a function of the number of iterations.

See Also

demos("findit") and tests/test-findit.R for more examples.

Examples

Run this code
data(thermo)
## an inorganic example: sulfur species
basis("CHNOS+")
basis("pH", 5)
species(c("H2S", "S2-2", "S3-2", "S2O3-2", "S2O4-2", "S3O6-2",
  "S5O6-2", "S2O6-2", "HSO3-", "SO2", "HSO4-"))
# to minimize the standard deviations of the 
# logarithms of activity the species
objective <- "SD"
# the variables we are interested in
vars <- list(O2=c(-50, -15), pH=c(0, 14), T=c(275, 375))
# optimize logfO2 at constant T and pH
f1 <- findit(vars[1], objective, T=325, P=350, niter=3)
title("S.D. of equilibrium log activities of sulfur species")
# optimize logfO2 and pH at constant T
f2 <- findit(vars[1:2], objective, T=325, P=350, res=16, niter=5)
title("S.D. of equilibrium log activities of sulfur species")
# optimize logfO2, pH and T (at constant P ...)
f3 <- findit(vars, objective, P=350, res=10, niter=10)
title("S.D. of equilibrium log activities of sulfur species")
# the results
print(f1.out <- sapply(f1$value, tail, 1))
print(f2.out <- sapply(f2$value, tail, 1))
print(f3.out <- sapply(f3$value, tail, 1))
# with more variables, we should find a greater degree of optimization
stopifnot(f2.out["SD"] < f1.out["SD"])
stopifnot(f3.out["SD"] < f2.out["SD"])

Run the code above in your browser using DataLab