---
title: "Inspecting the errorlocate Mixed Integer Program"
output: 
  rmarkdown::html_vignette:
    df_print: kable
vignette: >
  %\VignetteIndexEntry{Inspecting the errorlocate Mixed Integer Program}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
bibliography: 
- references.bib
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(errorlocate)
```

## Intro

Errorlocate uses the linear, categorical and conditional rules from a rules set formulated with R package [`validate`](https://cran.r-project.org/package=validate), to create a Mixed Integer Problem.

For most users the details of the translation are not relevant and hidden in `locate_errors`.
Often the number of errors found and the processing time are much more relevant parameters.

In a few cases, you may run into a problems with your error localization problem:

a)  The processing time of (some of the records) of `locate_errors` is high.
b)  `locate_errors` missed an obvious error.
c)  `locate_errors` indicates that it did not find a valid solution (for some records) .

Problem a. can be addressed by using the parallel argument of `locate_errors` (and `replace_errors`).
Problem b can be due to that `error_locate` ignores non-linear rules, and therefore
is not able to deduce the errors, because it only takes linear, categorical and 
conditional rules into a account. 

There may also be problems with your rules set. Problems set may be mitigated 
by using the [`validatetools`](https://cran.r-project.org/package=validate) 
package that can detect conflicting and redundant rules and has methods to 
simplify your rule set.

If you want to dive deep into the mixed integer problem that is created 
by `error_locate` you can use the `inspect_mip` function.

## A bit of theory

In the following sections an example is given of how linear, categorical and 
conditional rules are written as Mixed Integer Problems. First let's see 
how these rules in validator can be formally defined.

## Formal description

### Rule $r_i(\mathbf{x})$

Each translatable rule $r_i(\mathbf{x})$ can be written as a disjunction of 
atomic clauses $C_i^j(x)$: it is a function $r_i$ that operates on (some of) the values 
of record $\mathbf{x} = (x_1, \ldots, x_n)$ and is `TRUE` (valid) or `FALSE` 
(not valid)

$$
r_i(\mathbf{x}) = \bigvee_j C_i^j(\mathbf{x})
$$

with each atomic clause:

$$
C_i^j(\mathbf{x}) = \left\{
  \begin{array}{l}
 \mathbf{a}^T\mathbf{x} \leq b \\
  \mathbf{a}^T\mathbf{x} = b \\
  x_j \in F_{ij}  \textrm{with } F_{ij} \subseteq D_j \\
  x_j \not\in F_{ij} \textrm{with } F_{ij} \subseteq D_j \\
  \end{array}
\right.
$$

Each linear, categorical or conditional rule $r_i$ can be written in this form.

### Example 1

```{r}
rules <- validator(example_1 = if (income > 0) age >= 16)
rules$exprs()
```

So the rule `if (income > 0) age >= 16` can be written as (`income <= 0` OR `age >=16`)

### Example 2

```{r}
rules <- validator(example_2 = if (has_house == "yes") income >= 1000)
rules$exprs()
```

So the rule `if (has_house == "yes") income >= 1000)` can be written as (`has_house != "yes"` OR `age >=1000`)


## Rule system:

The rules form a system $R(\mathbf{x})$:

$$
R(\mathbf{x}) = \bigwedge_i r_i
$$ which means that all rules $r_i$ must be valid. If $R(\mathbf{x})$ is true for record $\mathbf{x}$, then the record is valid, otherwise one (or more) of the rules is violated.

## Mixed Integer Programming to FH

Each rule set $R(\mathbf{x})$ can be translated into a mip problem and solved.

$$
\begin{array}{r}
  \textrm{Minimize } f(\mathbf{x}) = 0; \\
  \textrm{s.t. }\mathbf{Rx} \leq \mathbf{d} \\
\end{array}
$$ - $f(\mathbf{x})$ is the (weighted) number of changed variable: $\delta_i \in {0,1}$

$$
f(\mathbf{x}) = \sum_{i=1}^N w_i \delta_i
$$

-   $\mathbf{R}$ contains rules:

    -   $\mathbf{R}_H(\mathbf{x}) \leq \mathbf{d}_H$ that were specified with `validate`/`validator`

    -   $\mathbf{R}_0(\mathbf{x}, \mathbf{\delta}) \leq \mathbf{d}_0$ : soft constraints that try fix the current record of $\mathbf{x}$ to the observed values.

## Enter `inspect_mip`:

### Linear rules

Most users will use the function `locate_errors` to find errors. 
The function `inspect_mip` works exactly same, except that it operates on 
just one record in stead of a whole `data.frame`. The result of `inspect_mip` 
is a mip object, that is not yet executed and can be inspected.

```{r, df_print = "kable"}
rules <- validator( r1 = age >= 18
                  , r2 = income >= 0
                  )
data <- data.frame(age = c(12, 35), income = c(2000, -1000))
data
```

So we detect two errors in the dataset:

```{r}
summary(confront(data, rules))
```

```{r}
locate_errors(data, rules)$errors
```

Lets inspect the first record

```{r}
mip <- inspect_mip(data, rules)
```

The `mip` object contains the mip problem before it is executed. 
We can inspect the lp problem, prior to solving it with `lpSolveApi` with

```{r mip1, results='hide'}
# inspect the lp problem (prior to solving it with lpsolveAPI)
lp <- mip$to_lp()
print(lp)
```

```{r, ref.label="mip1", comment= " ", echo=FALSE}
```

Validator rules `r1` and `r2` are encoded in two lines of the model. The values of the current record are encoded as soft constraints in `age_ub`, `age_lb`, `income_lb` and `income_ub`. These constraints try to fix the values of `age` at 12 and `income` at 2000, but can be violated, setting `.delta_age` or `.delta_income` to 1.

For large problems the lp problem can be written to disk for inspection

```{r, eval=FALSE}
mip$write_lp("my_problem.lp")
```

Once we execute the mip project, the lp solver is executed on the problem:

```{r}
res <- mip$execute()
```

Extra arguments are passed through to `lpSolveAPI`. The result object contains several properties:

```{r}
names(res)
```

`res$solution` indicates of a solution was found

```{r}
res$solution
```

`res$s` indicates the `lpSolveAPI` status, what kind of solution was found.

```{r}
res$s
```

`res$errors` indicates which fields/values are deemed erroneous:

```{r}
res$errors
```

`res$values` contains the values for the valid solution that has been found by the lpsolver: 

```{r}
res$values
```
Note that the solver has found that setting `age` from 12 to 18 gives a valid 
solution. `.delta_age = 1` indicates that `age` contained an error.

The result object `res` also contains an `lp` object after optimization. This 
object can be further investigated using `lpSolveAPI` functions.

```{r mip2, results="hide"}
# lp problem after solving
res$lp
```

```{r, ref.label="mip2", comment=" ", echo=FALSE}
```

Note that the lp problem has been simplified. For example the single 
variable constraints,the lp problem/object after solving shows that the solver 
has optimized some of the rules: it has moved rule `r1` and `r2` into the 
`Lower boundary` conditions. It also removed `age_lb` because that was 
superfluous with respect to the boundary conditions.

### Categorical rule

In categorical rules, each category is coded in a separate column/mip variable: 
e.g. if we have a `working` variable, with two categories ("job", "retired"), the mip problem is encoded as follows:

```{r}
rules <- validator( r1 = working %in% c("job","retired")                  )
data <- data.frame(working="?")
```

```{r, echo=FALSE}
knitr::kable(data)
```

```{r mip3, results="hide"}
mip <- inspect_mip(data, rules)
mip$to_lp()
```

```{r, ref.label="mip3", comment=" ", echo=FALSE}
```

Row `r1` indicates that either `working:job` or `working:retired` must be true. The `Kind` row (`SOS`) indicates that these variables share the same switch, only one of them can be set.

```{r}
mip$execute()$values
```

#### Multiple categories:

With categorical variables it is also possible to specify `if-then` rules. These are encoded as one mip rule:

```{r}
rules <- validator( r1 = if (voted == TRUE) adult == TRUE)
data <- data.frame(voted = TRUE, adult = FALSE)
```

```{r, echo=FALSE}
knitr::kable(data)
```

```{r mip4, results="hide"}
mip <- inspect_mip(data, rules)
mip$to_lp()
```

```{r, ref.label="mip4", comment=" ", echo=FALSE}
```

```{r}
mip$execute()$values
```

### Conditional rule

```{r}
rules <- validator( r1 = if (income > 0) age >= 16)
data <- data.frame(age = 12, income = 2000)
```

```{r, echo = FALSE}
knitr::kable(data)
```

```{r}
mip <- inspect_mip(data, rules)
```

`errorlocate` encodes this rule into multiple rules
(as noted in the theoretical section above), so rule `r1` is chopped into
1 rule + 2 sub rules: 

`r1: if (income > 0) age >= 16`:

- `r1._lin1: if (r1._lin1 == FALSE) income <= 0`

- `r1._lin2: if (r1._lin2 == FALSE) age >= 16`

- `r1: r1._lin1 == FALSE | r1._lin2 == FALSE`

This can be seen with:

```{r}
mip$mip_rules()
```

The resulting lp model is:

```{r mip6, results="hide"}
mip$to_lp()
```
```{r, ref.label="mip6", comment=" ", echo=FALSE}
```

```{r}
mip$execute()$values
```

### Alltogether:

This works together with categorical, linear and conditional rules.

```{r}
rules <- validator( r1 = working %in% c("no_job", "job","retired")
                  , r2 = if (age < 12) working == "no_job"
                  , r3 = if (working == "retired") age > 50
                  , r4 = age >=0
                  )

data <- data.frame(age = 12, working="retired")
mip <- inspect_mip(data, rules)
mip$execute()$errors
```

### Weights

The weights for each variable are normally set to 1, and `errorlocate` adds some random remainder to the weights: so the solutions are unique and reproducible (using `set.seed`).

```{r}
set.seed(42)
rules <- validator( r1 = if (voted == TRUE) adult == TRUE)
data <- data.frame(voted = TRUE, adult = FALSE)
mip <- inspect_mip(data, rules, weight = c(voted = 3, adult=1))
```

`$objective` contains the generated weights:

```{r}
mip$objective
```

These are assigned to the `delta` variables in the objective function of the mip.

```{r mip7, results="hide"}
mip$to_lp()
```

```{r, ref.label="mip7", comment=" ", echo=FALSE}
```
