How to set AIMMS to follow a script, so as to keep running for a certain number of times? - aimms

Objective function:
(X*Y1+(1-X)*Y2)*Z
Subject to some constraints:
If x starts from 0 I will have an optimal solution OS1
then I need run again with an increment of 0.01 for x, so if x=0.01 there will be OS2. continuing the process there will be OS1 to OS100.
Is there a way that I can program aimms to run all increments and then select the max among OS1 to OS100?

You can accomplish this as follows:
Create a set "AllSolutions" with 101 elements and index s.
Create a parameter "SolutionObjective(s)"
Create a procedure with the following body:
for s do
x := (ord(s)-1)/card(AllSolutions);
Solve ;
SolutionObjective(s) := ;
endfor;
The parameter SolutionObjective contains the 101 solutions that you are looking for. You can retrieve the max via the following statement:
Max(s, SolutionObjective(s));
The corresponding x is:
x := (ord(argmax(s, SolutionObjective(s)))-1)/100.
Regards,
Peter Nieuwesteeg
Senior AIMMS Specialist.

Related

explanation for this python command in a single line

In a tutorial for developing a processing-plugin for QGIS I found the following Python3-code:
# Compute the number of steps to display within the progress bar and
# get features from source
total = 100.0 / source.featureCount() if source.featureCount() else 0
features = source.getFeatures()
My question: What kind of language construct is this single line:
total = 100.0 / source.featureCount() if source.featureCount() else 0
This looks weird: Frist an assignment , which is followed by an if-else-construct in the same line??
These are called "conditional expressions". From the Python 3 reference:
The expression x if C else y first evaluates the condition, C rather than x. If C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.
You can read it as "return x if C otherwise return y."
When a conditional expression is used in the assignment to a variable, that variable will take on the value returned by that conditional expression.
This concept is usually referred to as a "ternary" in many other languages.

Trying to understand behavior of python scope and global keyword

def func():
def nested():
global x
x = 1
x = 3
nested()
print("func:", x)
x = 2
func()
print("main:", x)
Output:
func: 3
main: 1
I'm new to programming. I want to know where I'm going wrong. As I'm new to stack exchange, please let me know if there are any issues with my question or how to improve it.
The way I am reading this code is:
x is assigned the integer 2.
the func() function is called.
x is assigned the integer 3.
the nested() function is called.
x is declared a global variable? #not really clear of the implications
x is assigned the integer 1.
print("func":x) #because x was made a global variable within nested() I expected the output to be 1.
print("main": x) #I believe this is because x was made a global variable?
I'm not clear on why the output is 3 in the first print command?
In short, there are two different identifiers, x, being referenced here: one at the model level, and a different one local to func.
Your steps should read:
The identifier x at the top level (i.e. the module level) is associated with the value 2.
the func function is called.
The completely different identifier x which is local to func is associated with the value 3.
the nested function is called.
Python is told that, within this scope of nested, the x refers to the top level (module level) x. Note global doesn't mean "next level up", it means "the top/global level" - the same one affected by step 1, not the one affected by step 3.
The global (top level) x is associated with the value 1.
etc.
General hint: Every time you think you need to use global you almost certainly don't.
The question was asked perfectly fine. The reason why is because the global x was declared in the nested. Not in the whole program, so they're 2 different variables.

value of callee and caller when using call by references

I encountered a confusion , when i pass a variable x to variable y by reference then both x and y should now point to same location, but the output that i am getting is not same.
Full detail discussion is here: http://gateoverflow.in/94182/programming-output
I have tried my best to explain the stuff to user but i am still unable to convience him fully, maybe i am lacking some concept.
rough code sample:
var b : int;
procedure M (var a, int)
begin
a= a*a;
print(a);
end;
procedure N
begin
b= b+1;
M(b);
end;
begin
b=12;
N;
print(b);
end;
I assume that as in question it is given that variables are static , so the value of a b should not change from 13 , but the value of a should be 13*13=169 , but my reasoning is counter to what call by reference is about.
pascal code from unauthorized book, please throw some insights.
I had to review scoping terminology. I had myself confused between static and dynamic scoping. Static scoping is used in all modern programming languages. I conclude that both a and b should have a value of 169 at the respective print statements.

Handling nested variable scopes when implementing an interpreter

I'm currently writing an interpreter for a simple programming language and just wanted to ask on the best approach would be to tackle it.
The environment for a program is as follows:
type Env = [[(Var, Int)]]
So I've coded the lookup and update but I'm a bit stuck on how to deal with the the scope for each begin block. An example is shown below:
begin [a,b,c]
read i
n = 1
while i < 0 do
begin
n = 2 * n
i = i - 1
end;
write n
end
From my understanding the scope of the first begin would be [a,b,c,i,n]
and then the second begin would contain [i, n]
therefore the env would be
[ [ ("a",0), (b",0), ("c",0), ("i",3), ("n",2) ], [("n",8), ("i",0) ] ]`
Currently my lookup function returns the first occurrence of a variable, so I'm having problems with the 2nd scope (2nd begin).
I'm not quite sure how I can make the update and lookup function return the value associated with that particular scope.
Basically I have the code working for one begin statement, but I am having issues with 2 or more statements in the sample program.

Convert string to a variable name

I am using R to parse a list of strings in the form:
original_string <- "variable_name=variable_value"
First, I extract the variable name and value from the original string and convert the value to numeric class.
parameter_value <- as.numeric("variable_value")
parameter_name <- "variable_name"
Then, I would like to assign the value to a variable with the same name as the parameter_name string.
variable_name <- parameter_value
What is/are the function(s) for doing this?
assign is what you are looking for.
assign("x", 5)
x
[1] 5
but buyer beware.
See R FAQ 7.21
http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
You can use do.call:
do.call("<-",list(parameter_name, parameter_value))
There is another simple solution found there:
http://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/
To convert a string to a variable:
x <- 42
eval(parse(text = "x"))
[1] 42
And the opposite:
x <- 42
deparse(substitute(x))
[1] "x"
The function you are looking for is get():
assign ("abc",5)
get("abc")
Confirming that the memory address is identical:
getabc <- get("abc")
pryr::address(abc) == pryr::address(getabc)
# [1] TRUE
Reference: R FAQ 7.21 How can I turn a string into a variable?
Use x=as.name("string"). You can use then use x to refer to the variable with name string.
I don't know, if it answers your question correctly.
strsplit to parse your input and, as Greg mentioned, assign to assign the variables.
original_string <- c("x=123", "y=456")
pairs <- strsplit(original_string, "=")
lapply(pairs, function(x) assign(x[1], as.numeric(x[2]), envir = globalenv()))
ls()
assign is good, but I have not found a function for referring back to the variable you've created in an automated script. (as.name seems to work the opposite way). More experienced coders will doubtless have a better solution, but this solution works and is slightly humorous perhaps, in that it gets R to write code for itself to execute.
Say I have just assigned value 5 to x (var.name <- "x"; assign(var.name, 5)) and I want to change the value to 6. If I am writing a script and don't know in advance what the variable name (var.name) will be (which seems to be the point of the assign function), I can't simply put x <- 6 because var.name might have been "y". So I do:
var.name <- "x"
#some other code...
assign(var.name, 5)
#some more code...
#write a script file (1 line in this case) that works with whatever variable name
write(paste0(var.name, " <- 6"), "tmp.R")
#source that script file
source("tmp.R")
#remove the script file for tidiness
file.remove("tmp.R")
x will be changed to 6, and if the variable name was anything other than "x", that variable will similarly have been changed to 6.
I was working with this a few days ago, and noticed that sometimes you will need to use the get() function to print the results of your variable.
ie :
varnames = c('jan', 'feb', 'march')
file_names = list_files('path to multiple csv files saved on drive')
assign(varnames[1], read.csv(file_names[1]) # This will assign the variable
From there, if you try to print the variable varnames[1], it returns 'jan'.
To work around this, you need to do
print(get(varnames[1]))
If you want to convert string to variable inside body of function, but you want to have variable global:
test <- function() {
do.call("<<-",list("vartest","xxx"))
}
test()
vartest
[1] "xxx"
Maybe I didn't understand your problem right, because of the simplicity of your example. To my understanding, you have a series of instructions stored in character vectors, and those instructions are very close to being properly formatted, except that you'd like to cast the right member to numeric.
If my understanding is right, I would like to propose a slightly different approach, that does not rely on splitting your original string, but directly evaluates your instruction (with a little improvement).
original_string <- "variable_name=\"10\"" # Your original instruction, but with an actual numeric on the right, stored as character.
library(magrittr) # Or library(tidyverse), but it seems a bit overkilled if the point is just to import pipe-stream operator
eval(parse(text=paste(eval(original_string), "%>% as.numeric")))
print(variable_name)
#[1] 10
Basically, what we are doing is that we 'improve' your instruction variable_name="10" so that it becomes variable_name="10" %>% as.numeric, which is an equivalent of variable_name=as.numeric("10") with magrittr pipe-stream syntax. Then we evaluate this expression within current environment.
Hope that helps someone who'd wander around here 8 years later ;-)
Other than assign, one other way to assign value to string named object is to access .GlobalEnv directly.
# Equivalent
assign('abc',3)
.GlobalEnv$'abc' = 3
Accessing .GlobalEnv gives some flexibility, and my use case was assigning values to a string-named list. For example,
.GlobalEnv$'x' = list()
.GlobalEnv$'x'[[2]] = 5 # works
var = 'x'
.GlobalEnv[[glue::glue('{var}')]][[2]] = 5 # programmatic names from glue()

Resources