In Gecode, I am using a merit function to select variables when branching.
In order to compute variable v's merit, I need to access some other variables values, but it looks like at the time the merit function is called, the space variables have not yet been asigned any values :
Exception: IntVar::val: Attempt to access value of unassigned variable.
Am I doing something wrong? Is there a way to access variables values in merit functions?
The problem is that while you are still searching a variable won't just have 1 value, its domain is still larger than 1. This means that there might still be different values that a variable can take. Until there is only one value left in its domain you are not allowed to use the val method.
There are different solutions for this problem depending on how you want to use the value domain:
The best way to test a variable against a single value is using the in method. This method returns true if the value is in the domain of the variable.
To check variables against each other you would generally use the min and max methods to compare their domains.
If the value is only relevant when it is assigned, then you would check that the cardinality (size of the domain) is 1, using the size method, before using the val method.
These are the most general cases, but there are countless ways to interact with variables. Be sure to check the IntVar documentation, where these and all other methods for the IntVar class are described.
Related
I have a really basic question about something that I've never paid much attention to until now:
I noticed that when creating a function (in JS or Python) that uses a variable from the outer scope, the function is not defined using the value of the variable but rather the variable itself. So if I change the value of the variable the function will use the new value.
This is what I mean
let a = 10;
function printA(){
console.log(a);
}
printA(); // => 10
a = 20;
printA(); // => 20
a = 10
def printA():
print(a)
printA() # => 10
a = 20
printA() # => 20
I thought this was only going to work of objects because of the way you can modify an object inside a function but not primitive variables because there's no way to change their value without reasigning them. I guess this is a different story.
What I'm trying to understand is: when typing a variable name is typing its memory address what I'm really doing? Does this happen with all languages?
when I create a function like printA() that uses a variable that is not an argument, is the variable bound forever to the function by its address?
The variable a is "captured" by the function. The specifics of how that happens are usually implementation details and may result in the compiler/interpreter producing code that doesn't much resemble the original.
For instance, in C# (I know, not one of the languages you mentioned, but it's the one I'm most familiar with), the compiler will create a separate hidden class which actually contains fields for the variables that are captured by a lambda or nested function. It then accesses these fields rather than plain variables.
by its address
Variables don't typically have an address. For instance, every time you call a method, it will typically have an "activation record" of some kind created, that will typically contain its variables. But note that these records are not at some fixed location, which is how you can have parallel execution of methods, recursion, etc, without interference. (Some older BASICs did have fixed activation records, which is why they didn't allow for recursion). These activation records may typically be placed on some kind of stack.
But as I say, for captured variables, the compiler will typically need to do even more so that those variables aren't just stored in an activation record, and so that their lifetime is no longer tied to a single call.
Are there any rules (or will I run into any problems) if I name the parameters of a function the same as the variable I will pass into them?
For example in Python:
def foo(param):
pass
param = 2
foo(param)
In the fairly limited programming I've done, I have not ran into any problems doing this. Will I get problems in certain languages? Is this okay to do, or is it a practice to be avoided?
The "problem" in this particular case is that the function parameter name will shadow the outer param variable; i.e. you cannot (implicitly) refer to the global param anymore because inside your function param is defined as a local variable.
But, this is really as it should be. Your function should only worry about the parameters it declares locally, not about implicit global variables. Conversely, a caller of a function should not have to worry about anything that goes on inside a function. Naming a variable the same as a parameter to a function is of no consequence to the caller, and should be of no consequence to the function itself.
So, no, there's absolutely no issue here.
I am seeking a way to acquire all the functions which have been defined in the current scope from lua. Is there a quick way to implement it directly from lua, not from C? The factory functions are preferred to be included.
You can use a hybrid approach:
(1) to get all local variables, you can use debug.getlocal and get the names and values of the variables (see for example the logic in this answer). All function values will have type of the value equal to function (type(value) == 'function'), so you can easily filter based on that condition. The name of the variable will give you the name you are looking for (keep in mind that several names may refer to the same function).
(2) to get all global variables you can iterate over fields in _ENV or _G tables and apply the same filtering logic as in 1.
Note that neither of these methods gives you access to functions stored in table fields or available indirectly through metamethods.
It seems that I can't put a record field as a parameter?
joueurActuel.c1 := predColonne(joueurActuel.c1);
The function:
function predColonne (c : T_Colonne) return T_Colonne;
where T_Colonne is a subtype of Positive.
joueurActuel is an in parameter (joueurActuel : in T_Joueur;) of a function containing the assignment above. T_Joueur is a record.
This is by design. A formal parameter of mode in is a constant view; it cannot be updated within the subprogram body. A constant cannot be the target of an assignment operation. See section 3.3 od Ada Reference Manual, paragraphs 13, 15, 17 and 25 in particular.
So, either you have to store the result of predColonne(joueurActuel.c1) in a local variable, or change joueurActuel into an in out parameter if it's correct from the business logic point of view.
The in mode in joueurActuel : in T_Joueur; is a guarantee you have given to the compiler that you will not update or modify joueurActuel in any way within the procedure where you declared this formal parameter. The fact that it's a record is nothing to do with the problem.
joueurActuel.c1 := predColonne(joueurActuel.c1); is an attempt to modify joueurActuel, despite the guarantee.
The compiler, correctly, rejects it.
If this is really what you want to do, then mode in out will allow it, but first ask yourself it there is a better design. Does the rest of the program need to see the change? If so, then in out is acceptable. Otherwise, copying it to a local variable as Ondrej suggested it, and only modifying the local copy, will work.
i just wrote a m-file with some defined input in which a simulink file is called.
it worked correctly
but when I'm going to define a function based on the same m-file ( so i can give multiple inputs to it) it give's me this error :
""
Invalid matrix-format variable specified as workspace input in 'blocks/From Workspace'. The matrix
must have two dimensions and at least two columns. Complex signals of any data type and non-double
real signals must be in structure format. The first column must contain time values and the
remaining columns the data values.
""
but i'm pretty sure that variable has 2 dimension and has twoo coloumns.
i don't have any idea what to do here.
what can i do here ?
Are you saying the mfile that runs your Simulink simulation works when the mfile is a script, but not when the mfile is a function? If so, this answer may provide some insight. Despite a preference for functions, I use scripts to run Simulink parameter studies - it was just easier to set up.