Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_solution mixing up two variables #244

Closed
hugolarzabal opened this issue Nov 23, 2018 · 3 comments
Closed

get_solution mixing up two variables #244

hugolarzabal opened this issue Nov 23, 2018 · 3 comments
Labels

Comments

@hugolarzabal
Copy link
Contributor

hugolarzabal commented Nov 23, 2018

I have noticed in a few cases where get_solution return strange values.

After investigating a little bit, I realised that it happens when two variables have somewhat similar names.

For example, I have found that in one model, get_solution(solution, s[t,as]) returns the value of s but also the values of another variable named bus[c,cs].
It also happened in another case with variables named "t_start" and "airport_start".

My understanding is that a regex match in get_solution is looking for "s[" and also return "bus[".

@hugolarzabal
Copy link
Contributor Author

I think that I found how to fix it.

In the function extract_solution, there is patter defined as

    instance_pattern <- paste0(var_name,
                               "\\[",
                               paste0(idx_pattern, collapse = ","),
                               "\\]")

We can force the match to be on the complete variable name and not only part of it by adding "^" at the begining of the pattern.

    instance_pattern <- paste0("^",
                                var_name,
                               "\\[",
                               paste0(idx_pattern, collapse = ","),
                               "\\]")

If it doesn't break anything else, I can make a PR with that fix.

@dirkschumacher
Copy link
Owner

dirkschumacher commented Nov 23, 2018

Oh thanks for spotting this. Or we just use exact matching instead of a regex? fixed=TRUE?
If you want to make a PR, please include a minimal test.

Otherwise I will take a look

@hugolarzabal
Copy link
Contributor Author

From grep definition, I find that about fixed = TRUE:

pattern character string containing a regular expression (or character string for fixed = TRUE)

My understanding is that it will look for the exact string instead of a regular expression (for example, it will try to match "\[" instead of "[").

"^" is the simplest way to find the exact match. "^" is the regex symbol to represent the begining of the text ("$" is the end of the text).
For example, using "^a$" will do the exact match with "a".

In that case, we do not need the "$" since a variable name will always end with "]".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants