Determine cardinlity of sets in GAMS - modeling

How can I determine the cardinality of a set in GAMS? Is there a keyword for this in GAMS?
Suppose I have the following declaration:
SET y /1 * 10/ ;
I would like later in the model to make the asignment
x = cardinality_of_y;
How can I write this in GAMS?

The operator card() does the job.

Related

Achieving a tunable parameter in OpenModelica

I have the following Modelica code
model RocketCar
Real x;
Real v;
input Real u(min = -1, max = 1);
parameter Real h;
equation
der(x) = h*v;
der(v) = h*u;
end RocketCar;
meant to model the infamous rocket car problem, which I would like to transform into an FMU.
I transform the file using OMShell:
>>> loadFile("RocketCar.mo")
true
>> translateModelFMU(RocketCar)
"/path/to/RocketCar.fmu"
The resulting FMU lists the variable as
<ScalarVariable
name="h"
valueReference="10"
variability="fixed"
causality="parameter"
initial="exact">
<Real start="0.0"/>
</ScalarVariable>
i.e., as a fixed parameter. I would like to change the Modelica code to obtain a tunable parameter in the resulting FMU.
Note that a similar question has been asked and answered already. The suggested solution was to add annotation (Evaluate=false) to the definition of the parameter. However, this answer seems to be specific to Dymola. The suggested annotation seems to have no effect regarding the resulting FMU. Is there an OpenModelica variant of the annotation to achieve the same effect?

Discretizing PDE in space for use with modelica

I am currently doing a course called "Modeling of dynamic systems" and have been given the task of modeling a warm water tank in modelica with a distributed temperature description.
Most of the tasks have gone well, and my group is left with the task of introducing the heat flux due to buoyancy effects into the model. Here is where we get stuck.
the equation given is this:
Given PDE
But how do we discretize this into something we can use in modelica?
The discretized version we ended up with was this:
(Qd_pp_b[k+1] - Qd_pp_b[k]) / h_dz = -K_b *(T[k+1] - 2 * T[k] + T[k-1]) / h_dz^2
where Qd_pp_b is the left-hand side variable, ie the heat flux, k is the current slice of the tank and T is the temperature in the slices.
Are we on the right path? or completely wrong?
This doesn't seem to be a differential equation (as is) so this does not make sense without surrounding problem. For the second derivative you should always create auxiliary variables and for each partial derivative a separate equation. I added dummy values for parameters and dummy equations for T[k]. This can be simulated, is this about what you expected?
model test
constant Integer n = 10;
Real[n] Qd_pp_b;
Real[n] dT;
Real[n] T;
parameter Real K_b = 1;
equation
for k in 1:n loop
der(Qd_pp_b[k]) = -K_b *der(dT[k]);
der(T[k]) = dT[k];
T[k] = sin(time+k);
end for;
end test;

Error in caret-svm - "NAs are not allowed in subscripted assignments"

experts. I am a beginner to R. I am trying to use caret-SVM to make classification. The kernel is svmPoly.
First, I used the default parameters to train the model with leave-one-out cross-validation
The code is :
ctrl <- trainControl(method = "LOOCV",
classProbs = T,
savePredictions = T,
repeats = 1)
modelFit <- train(group~.,data=table_svm,method="svmPoly",
preProc = c("center","scale"),
trControl = ctrl)
The best accuracy is 80%. And the final values used for the model were degree = 1, scale = 0.1 and C = 1 .
Second, I tried to tune the parameters.
The code is:
grid_svmpoly=expand.grid(degree=c(1:11),scale=seq(0,5,length.out=25),C=10^c(0:4))
modelFit_tune <- train(group~.,data=table_svm,method="svmPoly",
preProc = c("center","scale"),
tuneGrid=grid_svmpoly,
trControl = ctrl)
I got an error message: Error in { :
task 264 failed - "NAs are not allowed in subscripted assignments"
I checked the data and found no NA.
There must be some NA inside the data-set. I am not new to this but not much expert. To ensure there is no NA inside first convert data-set into matrix format using:
x <- data.matrix(dataframe)
then use which() function which very handy in this case:
which(is.na(x)==T)
I hope this will help you finding the answer. The values will be in row wise order.
Let me know if this resolve your query.

Choco solver constraint/variable definition

I'm trying to port a minizinc model in choco. I know how to define variables and other basic stuff but despite having read the tutorial and some code examples I've some trouble defining some non trivial constraints.
Could someone give me some advice how to translate the following code (just z) in a choco solver style?
array[1..n,1..n] of int: c;
array[1..n] of var 0..10: next;
var 0..sum(c): z = sum(i in 1..n)(c[i,next[i]]);
Thanks!
I believe you know how to post a sum constraint so the non trivial part lies in the c[i,next[i]] which retrieves the integer in matrix c at row i and column next[i]. The problem is that next[i] is a variable so you cannot use it directly to access a (Java) array.
You need to use the element constraint (that is also in minizinc):
/**
* Creates an element constraint: value = table[index]
*
* #param value an integer variable taking its value in table
* #param table an array of integer values
* #param index an integer variable representing the value of value in table
*/
default Constraint element(IntVar value, int[] table, IntVar index)
As you work with a matrix, you need to do that for each row and then post a sum on them.
Note also that in Java, array cells are accessed from 0 to n-1 (in minizinc it is from 1 to n), so you may need to update the model accordingly or use an offset.
Hope this helps
https://www.cosling.com/

Set arbitrary base of logarithm in gnuplot

I would like to ask how can I set arbitrary base of logarithm in gnuplot (I would need f(x)=x^{1+9log2(x)} function to plot).
It seems like there are only builtin functions for the natural log and log10. But you can easily change the base of the logarithm.
log_b(x) = log_k(x) / log_k(b)
Thus, you can rewrite your formula as
f(x) = x**(1 + 9 * log(x)/log(2))
To expand upon the answer of tobias_k, you can create a user-defined function which takes the base as a secondary argument:
logb(x, base) = log(x)/log(base)
And then rewrite your formulas as:
f(x) = x**(1 + 9 * logb(x, 2))

Resources