Rails - use object oriented "advanced" - ruby-on-rails-3

I would like to maintain my code DRY, then I want to transform this pseudo-code:
def aMethod
a = aModel.find(2)
b = a.getVariable
a.setVariable = c
end
in something like this
def aMethod
anotherMethod(aModel, getVariable)
end
def anotherMethod(model, var)
a = model.find(2)
b = a.var
a.var = c
end
In my tests, seems that there is no problem for the model, but for the getVariable (i.e. accessing the variable of the model) it doesn't work: undefined local variable or method
Any ideas?

You likely want to use send, if I understand what you're trying to do, e.g.,
def anotherMethod(model, var_sym)
a = model.find(2)
b = a.send(var_sym)
a.send("#{var_sym}=", c)
end
anotherMethod(aModel, :getVariable)
(With the caveat that I don't know what a, b, or c are, or should do, since they're locals in the OP.)

Related

Compact way to save JuMP optimization results in DataFrames

I would like to save all my variables and dual variables of my finished lp-optimization in an efficient manner. My current solution works, but is neither elegant nor suited for larger optimization programs with many variables and constraints because I define and push! every single variable into DataFrames separately. Is there a way to iterate through the variables using all_variables() and all_constraints() for the duals? While iterating, I would like to push the results into DataFrames with the variable index name as columns and save the DataFrame in a Dict().
A conceptual example would be for variables:
Result_vars = Dict()
for vari in all_variables(Model)
Resul_vars["vari"] = DataFrame(data=[indexval(vari),value(vari)],columns=[index(vari),"Value"])
end
An example of the appearance of the declared variable in JuMP and DataFrame:
#variable(Model, p[t=s_time,n=s_n,m=s_m], lower_bound=0,base_name="Expected production")
And Result_vars[p] shall approximately look like:
t,n,m,Value
1,1,1,50
2,1,1,60
3,1,1,145
Presumably, you could go something like:
x = all_variables(model)
DataFrame(
name = variable_name.(x),
Value = value.(x),
)
If you want some structure more complicated, you need to write custom code.
T, N, M, primal_solution = [], [], [], []
for t in s_time, n in s_n, m in s_m
push!(T, t)
push!(N, n)
push!(M, m)
push!(primal_solution, value(p[t, n, m]))
end
DataFrame(t = T, n = N, m = M, Value = primal_solution)
See here for constraints: https://jump.dev/JuMP.jl/stable/constraints/#Accessing-constraints-from-a-model-1. You want something like:
for (F, S) in list_of_constraint_types(model)
for con in all_constraints(model, F, S)
#show dual(con)
end
end
Thanks to Oscar, I have built a solution that could help to automatize the extraction of results.
The solution is build around a naming convention using base_name in the variable definition. One can copy paste the variable definition into base_name followed by :. E.g.:
#variable(Model, p[t=s_time,n=s_n,m=s_m], lower_bound=0,base_name="p[t=s_time,n=s_n,m=s_m]:")
The naming convention and syntax can be changed, comments can e.g. be added, or one can just not define a base_name. The following function divides the base_name into variable name, sets (if needed) and index:
function var_info(vars::VariableRef)
split_conv = [":","]","[",","]
x_str = name(vars)
if occursin(":",x_str)
x_str = replace(x_str, " " => "") #Deletes all spaces
x_name,x_index = split(x_str,split_conv[1]) #splits raw variable name+ sets and index
x_name = replace(x_name, split_conv[2] => "")
x_name,s_set = split(x_name,split_conv[3])#splits raw variable name and sets
x_set = split(s_set,split_conv[4])
x_index = replace(x_index, split_conv[2] => "")
x_index = replace(x_index, split_conv[3] => "")
x_index = split(x_index,split_conv[4])
return (x_name,x_set,x_index)
else
println("Var base_name not properly defined. Special Syntax required in form var[s=set]: ")
end
end
The next functions create the columns and the index values plus columns for the primal solution ("Value").
function create_columns(x)
col_ind=[String(var_info(x)[2][col]) for col in 1:size(var_info(x)[2])[1]]
cols = append!(["Value"],col_ind)
return cols
end
function create_index(x)
col_ind=[String(var_info(x)[3][ind]) for ind in 1:size(var_info(x)[3])[1]]
index = append!([string(value(x))],col_ind)
return index
end
function create_sol_matrix(varss,model)
nested_sol_array=[create_index(xx) for xx in all_variables(model) if varss[1]==var_info(xx)[1]]
sol_array=hcat(nested_sol_array...)
return sol_array
end
Finally, the last function creates the Dict which holds all results of the variables in DataFrames in the previously mentioned style:
function create_var_dict(model)
Variable_dict=Dict(vars[1]
=>DataFrame(Dict(vars[2][1][cols]
=>create_sol_matrix(vars,model)[cols,:] for cols in 1:size(vars[2][1])[1]))
for vars in unique([[String(var_info(x)[1]),[create_columns(x)]] for x in all_variables(model)]))
return Variable_dict
end
When those functions are added to your script, you can simply retrieve all the solutions of the variables after the optimization by calling create_var_dict():
var_dict = create_var_dict(model)
Be aware: they are nested functions. When you change the naming convention, you might have to update the other functions as well. If you add more comments you have to avoid using [, ], and ,.
This solution is obviously far from optimal. I believe there could be a more efficient solution falling back to MOI.

Define multiple variables in a condition in ST

I am currently programming/simulating a small plant in CODESYS.
I have several outputs (that correspond to engines) that I need to test several times, so I want to create a condition that incorporates this test so I dont need to write the entire condition.
For instance, i have the condition that verifies if
A=TRUE AND B=TRUE AND C=TRUE AND D=TRUE
Can I create a condition like "verify engine" to use each time ?
Thank you
There are many ways to do this (if I understood you correctly).
Here are two ways for example:
1. Create a variable that has the condition result and use the variable. You have to assign the variable at beginning, and then you can use the variable instead of that long code.
VAR
EnginesOK : BOOL;
END_VAR
//Check engines
EnginesOK := (A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE);
//.. Later ..
IF EnginesOK THEN
//Do something
END_IF
2. Create a function, for example F_VerifyEngines that contains checks and returns the state as BOOL. Note: In this example A,B,C and D need to be global variables. You could also pass them as parameters for the function.
FUNCTION F_VerifyEngines : BOOL
VAR_INPUT
//Add A,B,C,D here if needed
END_VAR
VAR
END_VAR
//Return the result
F_VerifyEngines := (A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE);
Then you can use the function in code:
IF F_VerifyEngines() THEN
//Do something
END_IF
The 2nd way is probably the one you were thinking.
By the way, there is no need to write A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE, in my opinion, it's more clear to read when you use A AND B AND C AND D instead.

How to create a "single dispatch, object-oriented Class" in julia that behaves like a standard Java Class with public / private fields and methods

I read in a book that "you can't create traditional 'classes' in julia with single-dispatch-style methods like obj.myfunc()" ... and I thought that sounded more like a challenge than a fact.
So here's my JavaClass type with public / private fields and methods just for the sheer shock and horror factor of having something ugly like this in Julia, after all the trouble the devs have gone to to avoid it:
type JavaClass
# Public fields
name::String
# Public methods
getName::Function
setName::Function
getX::Function
getY::Function
setX::Function
setY::Function
# Primary Constructor - "through Whom all things were made."
function JavaClass(namearg::String, xarg::Int64, yarg::Int64)
# Private fields - implemented as "closed" variables
x = xarg
y = yarg
# Private methods used for "overloading"
setY(yarg::Int64) = (y = yarg; return nothing)
setY(yarg::Float64) = (y = Int64(yarg * 1000); return nothing)
# Construct object
this = new()
this.name = namearg
this.getName = () -> this.name
this.setName = (name::String) -> (this.name = name; return nothing)
this.getX = () -> x
this.getY = () -> y
this.setX = (xarg::Int64) -> (x = xarg; return nothing)
this.setY = (yarg) -> setY(yarg) #Select appropriate overloaded method
# Return constructed object
return this
end
# a secondary (inner) constructor
JavaClass(namearg::String) = JavaClass(namearg, 0,0)
end
Example use:
julia> a = JavaClass("John", 10, 20);
julia> a.name # public
"John"
julia> a.name = "Jim";
julia> a.getName()
"Jim"
julia> a.setName("Jack")
julia> a.getName()
"Jack"
julia> a.x # private, cannot access
ERROR: type JavaClass has no field x
julia> a.getX()
10
julia> a.setX(11)
julia> a.getX()
11
julia> a.setY(2) # "single-dispatch" call to Int overloaded method
julia> a.getY()
2
julia> a.setY(2.0)
julia> a.getY() # "single-dispatch" call to Float overloaded method
2000
julia> b = JavaClass("Jill"); # secondary constructor
julia> b.getX()
0
Essentially, the constructor becomes a closure, which is how one creates "private" fields and methods / overloading.
Any thoughts? (other than "OMG Why??? Why would you do this??")
Any other approaches?
Any scenarios you could envisage where this might fail spectacularly?
While of course this isn't the idiomatic way to create objects and methods in julia, there's nothing horribly wrong with it either. In any language with closures you can define your own "object systems" like this, for example see the many object systems that have been developed within Scheme.
In julia v0.5 there is an especially slick way to do this due to the fact that closures represent their captured variables as object fields automatically. For example:
julia> function Person(name, age)
getName() = name
getAge() = age
getOlder() = (age+=1)
()->(getName;getAge;getOlder)
end
Person (generic function with 1 method)
julia> o = Person("bob", 26)
(::#3) (generic function with 1 method)
julia> o.getName()
"bob"
julia> o.getAge()
26
julia> o.getOlder()
27
julia> o.getAge()
27
It's weird that you have to return a function to do this, but there it is. This benefits from many optimizations like the language figuring out precise field types for you, so in some cases we can even inline these "method calls". Another cool feature is that the bottom line of the function controls which fields are "public"; anything listed there will become a field of the object. In this case you get only the methods, and not the name and age variables. But if you added name to the list then you'd be able to do o.name as well. And of course the methods are also multi-methods; you can add multiple definitions for getOlder etc. and it will work like you expect.
Jeff Bezanson's answer is quite good, but as alluded to in the comments, fields may get boxed, which is quite annoying.
There is a superior solution to this problem.
Alternative 1 (basically the same approach as presented in the question):
# Julia
mutable struct ExampleClass
field_0
field_1
method_0
method_1
method_2
function ExampleClass(field_0, field_1)
this = new()
this.field_0 = field_0
this.field_1 = field_1
this.method_0 = function()
return this.field_0 * this.field_1
end
this.method_1 = function(n)
return (this.field_0 + this.field_1) * n
end
this.method_2 = function(val_0, val_1)
this.field_0 = val_0
this.field_1 = val_1
end
return this
end
end
ex = ExampleClass(10, 11)
ex.method_0()
ex.method_1(1)
ex.method_2(20, 22)
Alternative 2:
mutable struct ExampleClass
field_0
field_1
function ExampleClass(field_0, field_1)
this = new()
this.field_0 = field_0
this.field_1 = field_1
return this
end
end
function Base.getproperty(this::ExampleClass, s::Symbol)
if s == :method_0
function()
return this.field_0 * this.field_1
end
elseif s == :method_1
function(n)
return (this.field_0 + this.field_1) * n
end
elseif s == :method_2
function(val_0, val_1)
this.field_0 = val_0
this.field_1 = val_1
end
else
getfield(this, s)
end
end
ex = ExampleClass(10, 11)
ex.method_0()
ex.method_1(1)
ex.method_2(20, 22)
Alternative 1 looks better, but alternative 2 performs better.
I created a more in depth analysis of this matter and you may check it here: https://acmion.com/blog/programming/2021-05-29-julia-oop/

Matlab's arrayfun for uniform output of class objects

I need to build an array of objects of class ID using arrayfun:
% ID.m
classdef ID < handle
properties
id
end
methods
function obj = ID(id)
obj.id = id;
end
end
end
But get an error:
>> ids = 1:5;
>> s = arrayfun(#(id) ID(id), ids)
??? Error using ==> arrayfun
ID output type is not currently implemented.
I can build it alternatively in a loop:
s = [];
for k = 1 : length(ids)
s = cat(1, s, ID(ids(k)));
end
but what is wrong with this usage of arrayfun?
Edit (clarification of the question): The question is not how to workaround the problem (there are several solutions), but why the simple syntax s = arrayfun(#(id) ID(id), ids); doesn't work. Thanks.
Perhaps the easiest is to use cellfun, or force arrayfun to return a cell array by setting the 'UniformOutput' option. Then you can convert this cell array to an array of obects (same as using cat above).
s = arrayfun(#(x) ID(x), ids, 'UniformOutput', false);
s = [s{:}];
You are asking arrayfun to do something it isn't built to do.
The output from arrayfun must be:
scalar values (numeric, logical, character, or structure) or cell
arrays.
Objects don't count as any of the scalar types, which is why the "workarounds" all involve using a cell array as the output. One thing to try is using cell2mat to convert the output to your desired form; it can be done in one line. (I haven't tested it though.)
s = cell2mat(arrayfun(#(id) ID(id), ids,'UniformOutput',false));
This is how I would create an array of objects:
s = ID.empty(0,5);
for i=5:-1:1
s(i) = ID(i);
end
It is always a good idea to provide a "default constructor" with no arguments, or at least use default values:
classdef ID < handle
properties
id
end
methods
function obj = ID(id)
if nargin<1, id = 0; end
obj.id = id;
end
end
end

overriding a method in R, using NextMethod

how dows this work in R...
I am using a package (zoo 1.6-4) that defines a S3 class for time series sets.
I am writing a derived class where I want to override a few methods and can't get past this one:[.zoo!
in my derived class rows are indexed by timestamp, like in zoo, but differently from zoo, I allow only POSIXct values in the index. my users will be selecting columns all of the time, while slicing series only occasionally so I want to offer obj[name] instead of obj[, name].
my objects have class c("delftfews", "zoo").
but...
how do I override a method?
I tried this:
"[.delftfews" <- function(x, i, j, drop=TRUE, ...) {
if (missing(i)) return(NextMethod())
if (all(class(i) == "character") && missing(j)) {
return(NextMethod('[', x=x, i=1:NROW(x), j=i, drop=drop, ...))
}
NextMethod()
}
but I get this error: Error in rval[i, j, drop = drop., ...] : incorrect number of dimensions.
I have solved by editing the source from zoo: I removed those ..., but I don't get why that works. anybody can explain what is going on here?
The problem is that with the above definition of [.delftfews this code:
library(zoo)
z <- structure(zoo(cbind(a = 1:3, b = 4:6)), class = c("delftfews", "zoo"))
z["a"]
# generates this call: `[.zoo`(x = 1:6, i = 1:3, j = "a", drop = TRUE, z, "a")
Your code does work as is if you write the call like this:
z[j = "a"]
# generates this call: `[.zoo`(x = z, j = "a")
I think what you want is to change the relevant line in [.delftfews to this:
return(NextMethod(.Generic, object = x, i = 1:NROW(x), drop = drop))
# z["a"] now generates this call: `[.zoo`(x = z, i = 1:3, j = "a", drop = TRUE)
A point of clarification: allowing only POSIXct index values does not allow indexing columns by name only. I'm not sure how you arrived at that conclusion.
You're overriding zoo correctly, but I think you misunderstand NextMethod. The error is caused by if (missing(i)) return(NextMethod()), which calls [.zoo if i is missing, but [.zoo requires i because zoo's internal data structure is a matrix. Something like this should work:
if (missing(i)) i <- 1:NROW(x)
though I'm not sure if you have to explicitly pass this new i to NextMethod...
You may be interested in the xts package, if you haven't already taken a look at it.