How to use size() function to get number of cars in a train agent - anylogic

I'm using the railyard library and after a train source block I have a select output that loops back to another flowchart block if the train is greater than 25 cars. For the code of this select output I've tried agent.size() > 25; train.size() > 25; and int size() > 25 but everything comes up with errors. Am I using the wrong naming convention for my agent (and if so where do I find my agent name), or how do I tweak this code to use this function correctly?

Use agent.size()>25 but without the semicolon
This is a condition, not a normal line of code.

Related

Failed to expand block containing connect

I´m trying to set a connection with a conditional on the temperature, to represent a temperature-sensible charging pipe that works inside a stratified heat storage tank, with the following for loop reading the several volumes of the tank,
`for i in 2:nSeg loop
if (sensor_T_inflow.T > vol[i].T) and (sensor_T_inflow.T < vol[i-1].T) then
connect(feedPort, vol[i].ports[3])
annotation (Line(points={{-50,-60},{6,-60},{6,
-16},{16,-16}}, color={0,127,255}));
end if;
end for;`
However I get the errors such as this one for the volume 2:
Failed to expand block containing connect:
if (sensor_T_inflow.T > vol[2].T and sensor_T_inflow.T < vol[1].T) then connect(feedPort, vol[2].ports[3]); end if;
The model contained invalid connect statements.
Check aborted.
This might have a silly solution but I checked the names of all my elements and their temperature properties and they match with this code snippet, and I confirmed the error comes specially from the if conditional. Here is my model If you could take a look at it.
I tried commenting out the conditional and the check was succesful, only that I have a misbalance of about 40 variables to equations. If you got any advice on how to solve those misbalances I´d be much grateful as well.
Thanks a lot in advance.
The connect-statements may not depend on time-varying variables.
(The technical statement is first in the list in https://specification.modelica.org/master/connectors-and-connections.html#restrictions-of-connections-and-connectors )
A solution would be to turn that into an array of valve-components, and have them controlled in a similar way.

How to monitor error on a validation set in Chainer framework?

I am kind of new to Chainer and have written a code which trains a simple feed forward neural network. I have a validation set and a train set and want to test on the validation set on each like 500 iterations and if the results are better I want to save my network weights. Can anyone tell me how can I do that?
Here is my code:
optimizer = optimizers.Adam()
optimizer.setup(model)
updater = training.StandardUpdater(train_iter, optimizer, device=0)
trainer = training.Trainer(updater, (10000, 'epoch'), out='result')
trainer.extend(extensions.Evaluator(validation_iter, model, device=0))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss', 'elapsed_time']))
trainer.run()
Error on validation set
It is reported by Evaluator, and printed by PrintReport. Thus it should be shown with your code above. And to control the frequency of execution of these extentions, you can specify trigger keyword argument in trainer.extend function.
For example, below code specifies printing each 500 iteration.
trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'validation/main/loss', 'elapsed_time']), trigger=(500, 'iteration'))
You can also specify trigger to Evaluator.
Save network weights
You can use snapshot_object extension.
http://docs.chainer.org/en/stable/reference/generated/chainer.training.extensions.snapshot_object.html
It will be invoked every epoch as default.
If you want to invoke it when the loss improves, I think you can set trigger using MinValueTrigger.
http://docs.chainer.org/en/stable/reference/generated/chainer.training.triggers.MinValueTrigger.html

Create ordinal array with multiple groups

I need to categorize a dataset according to different age groups. The categorization depends on whether the Sex is Male or Female. I first subset the data by gender and then use the ordinal function (dataset is from a Matlab example). The following code crashes on the last line when I try to vertically concatenate the subsets:
load hospital;
subset_m=hospital(hospital.Sex=='Male',:);
subset_f=hospital(hospital.Sex=='Female',:);
edges_f=[0 20 max(subset_f.Age)];
edges_m=[0 30 max(subset_m.Age)];
labels_m = {'0-19','20+'};
labels_f = {'0-29','30+'};
subset_m.AgeGroup= ordinal(subset_m.Age,labels_m,[],edges_m);
subset_f.AgeGroup = ordinal(subset_f.Age,labels_f,[],edges_f);
vertcat(subset_m,subset_f);
Error using dataset/vertcat (line 76)
Could not concatenate the dataset variable 'AgeGroup' using VERTCAT.
Caused by:
Error using ordinal/vertcat (line 36)
Ordinal levels and their ordering must be identical.
Edit
It seems that a vital part was missing in the question, here is the answer to the corrected question. You need to use join rather than vertcat, for example:
joinFull = join(subset_f,subset_m,'LeftKeys','LastName','RightKeys','LastName','type','rightouter','mergekeys',true)
Solution of original problem
It seems like you are actually trying to work with the wrong variable. If I change all instances of hospitalCopy into hospital then everything works fine for me.
Perhaps you copied hospital and edited it, thus losing the validity of the input.
If you really need to have hospitalCopy make sure to assign to it directly after load hospital.
If this does not help, try using clear all before running the code and make sure there is no file called 'hospital' in your current directory.

Making connections to SV generated interfaces

I need to make connections to ports of a SystemVerilog interface that have been generated. But I don't know what the instance names of the generated interfaces are, so I can't work out how to connect to them.
e.g., if I generate the code like this:
generate
for (genvar abc_if_inst = 0; abc_if_inst < NUM_ABC; abc_if_inst++)
abc_if if_abc (.clk(clk), .resetn(resetn));
endgenerate
How do I reference the interface signals, e.g. I'm assuming it's something like this:
.port_x (if_abc_GEN_INST_NUM.port_x),
.port_y (if_abc_GEN_INST_NUM.port_y),
It is best to put a begin-end around the content of the for-loop and apply a label. If you not use a label then a automatic label will be added as genblk suffixed with unique id number. Section 27.6 of IEEE Std 1800-2012 goes into detail explain the generate block naming works. Section 27 is all about generate blocks. One example about generate for-loops on page 753.
For your provided code, try:
generate
for (genvar abc_if_inst=0; abc_if_inst<NUM_ABC; abc_if_inst++) begin : mygen
abc_if if_abc (.clk(clk), .resetn(resetn));
end
endgenerate
Then you can connect by as:
.port_x (mygen[0].if_abc.port_x),
.port_y (mygen[0].if_abc.port_y),
// ...
.port_x (mygen[NUM_ABC-1].if_abc.port_x),
.port_y (mygen[NUM_ABC-1].if_abc.port_y),
Note that the index of mygen needs to be a constant, such as a parameter, another genvar, or hard coded value.

How to reciprocate?

Actually I am coding a Matlab simulation where the AnchorID and SourceID will report to eachother. For example if I take an anchor 30 and source 50 it will collect all the agc values between these anchor and source and calculate rssi_dB and display them.Below mentioned is an example of anchor 30 and source id 50
Note: list of anchors ID's and source ID's are same. for e.g. 30 50 55 58 . These ID are both same for anchor and source.
function A30(BlinkSet)
for i=1:length(BlinkSet)
xAnchorID=30;
xSourceID=50;
a=BlinkSet{i}.AnchorID;
b=BlinkSet{i}.SourceID;
if xAnchorID==a && xSourceID==b
xagc=BlinkSet{i}.agc;
rssi_dB(i)=-(33+xagc*(89-33)/(29-1));
end
end
rssi_dB(rssi_dB==0)=[];
rssi_dBm=sum(rssi_dB(:))/length(rssi_dB);
disp([sprintf('The rssi value is %0.0f',rssi_dBm)]);
When I call the function in Matlab command window I get the rssi value of the above function.
Also my task is when I reciprocate the Anchor ID and source ID say Anchor as 50 and source as 30 like the function I have mentioned below I get an error which is mentioned after the function below.
function A50(BlinkSet)
for i=1:length(BlinkSet)
xAnchorID=50;
xSourceID=30;
a=BlinkSet{i}.AnchorID;
b=BlinkSet{i}.SourceID;
if xAnchorID==a && xSourceID==b
xagc=BlinkSet{i}.agc;
rssi_dB(i)=-(33+xagc*(89-33)/(29-1));
end
end
rssi_dB(rssi_dB==0)=[];
rssi_dBm=sum(rssi_dB(:))/length(rssi_dB);
disp([sprintf('The rssi value is %0.0f',rssi_dBm)]);
When I call this function I get an error as
??? Undefined function or variable "rssi_dB".
Error in ==> A50 at 14
rssi_dB(rssi_dB==0)=[];
Error in ==> main_reduced at 26
A50(BlinkSet);
In main function I have coded like this,
%A30(BlinkSet);
A50(BlinkSet);
Any help is highly appreciated.
In both of these functions, you only create the variable rssi_dB if execution enters the if statement within the loop (i.e., if xAnchorID==a && xSourceID==b is at some point true). Clearly, this code is never executed in your A50 function. Without knowing what is in BlinkSet it's a bit difficult to diagnose the exact problem, but this is the cause at least.
As a side note: it's not a good idea to create two separate functions to do this job when their code is almost identical. You should add an input argument to your function that allows it to do the job of both. In this particular case, all that changes is the value of xAnchorID and xSourceID, so you could just pass these in:
function srcToAnchorRssi(BlinkSet, xSourceID, xAnchorID)
% The rest of the function stays the same!
If you want to provide some defaults for these arguments, you can do, e.g.:
if nargin < 3 || isempty(xAnchorID), xAnchorID = 50; end
if nargin < 2 || isempty(xSourceID), xSourceID = 30; end
It's always a good idea to include an isempty in statements of this sort, so that your function supports syntax like myFunction(myArg1, [], myArg3). Also note that the order of the operands to || is crucial; if you did if isempty(theArgument) || nargin < theArgumentNumber and the user did not pass theArgument, then it would error in the isempty because theArgument would not exist as a local variable. We can get around this by swapping the operands' order because MATLAB is smart enough to know it doesn't have to evaluate the right operand if the left operand is true (note that this is also the case in many other programming languages).