algebraic_range: Finds the maximum and minimum values for all supplied terms...

Description Usage Arguments Value Examples

Description

Uses the solver to attempt to find a minimum and maximum value for all supplied algebraic terms in the input formula based on the supplied basic variable ranges. If the algebraic terms are complex shapes or step functions, this may not find the absolute minima and maxima and may, instead, find a local minima or maxima.

Usage

1
algebraic_range(base_var_range, algebraic_formula)

Arguments

base_var_range

Range of basic input variables that are contained in the algebraic formula. Column names must match basic variable names in the algebraic formula. Format is a matrix or data frame with column in Input_range matching each column name in StartingMat and column_names, minimum value in first row, and maximum value in second row. Values may also be supplied for some algebraic combinations. If supplied, these will be passed through to the output and will not be solved.

algebraic_formula

A one or two sided formula. Algebraic combinations on the input side of the equation will have ranges found.

Value

Returns a matrix with column names matching algebraic combinations on the input side of formula where row 1 = minimum values and row 2 = maximum values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (base_var_range, algebraic_formula)
{
    algebraic_input_terms <- colnames(attr(terms(algebraic_formula),
        "factors"))
    AlgebraicRange <- matrix(data = 0, nrow = 2, ncol = length(algebraic_input_terms))
    colnames(AlgebraicRange) <- algebraic_input_terms
    rownames(AlgebraicRange) <- c("minimum", "maximum")
    for (i in 1:length(algebraic_input_terms)) {
        transmult <- gsub(":", "*", algebraic_input_terms[i])
        optimexp <- parse(text = transmult)
        base_inputs <- all.vars(optimexp)
        optimfunc <- function(optimvect) {
            basevarmat <- matrix(nrow = 1, ncol = length(base_inputs),
                data = 0)
            basevarmat <- data.frame(basevarmat)
            colnames(basevarmat) <- base_inputs
            basevarmat[1, ] <- optimvect
            output <- eval(optimexp, basevarmat)
            return(output)
        }
        minvect <- as.vector(base_var_range[1, base_inputs])
        maxvect <- as.vector(base_var_range[2, base_inputs])
        AlgebraicRange["minimum", algebraic_input_terms[i]] <- optim(minvect,
            optimfunc, method = "L-BFGS-B", lower = minvect,
            upper = maxvect)$value
        AlgebraicRange["maximum", algebraic_input_terms[i]] <- optim(maxvect,
            optimfunc, method = "L-BFGS-B", lower = minvect,
            upper = maxvect, control = list(fnscale = -1))$value
    }
    return(AlgebraicRange)
  }

taalbrecht/MultiEqOptimizer documentation built on May 31, 2019, 12:51 a.m.