Output the solved time and date in minizinc with assert function - minizinc

Imagine I have below code and it would output the assert statement since the condition is not met.
int: x =40;
var int:y;
constraint y=x;
Now imagine I am getting the value for x from a data file where user can enter any value from their end.
But I am limiting user to enter only positive figures to x.(X cannot be negative figures).
constraint assert(x>=0,"Please enter a positive value to X");
solve satisfy;
Imagine user input -2 so it will output the above statement.I need to print the date and time with above statement.(At least time).Is there any method to do that in minizinc.

(I'll convert my comment as an answer, so you can accept it if you want.)
There is no built-in function in MiniZinc for doing this. A suggestion is that you run this via a wrapper (e.g. the Python MiniZinc package, see below) to catch the error and then print date and time.
The Python MiniZinc package is here: https://minizinc-python.readthedocs.io/en/latest/getting_started.html

Related

Writing function for symbolic and numeric input

I am writing a (rather big) pure function, which shall accept numeric and symbolic input. The problem I am facing is (mostly) with conversion symbolic expressions to logical expression. E.g., When I have a expression like syms x; assume( x, 'positive' ); and a test if( x>0 ); ..., then Matlab throws an error; Either:
Conversion to logical from sym is not possible.
or
Unable to prove 'x > 0' literally. Use 'isAlways' to test the statement mathematically.
depending on the context.
Up to now I solved this problem by rewriting all my ifs to if( isAlways(x>0) ); ... and adding a function isAlways in a folder #double, so that Matlab uses this function when it encounters a double.
The downside of this approach is, that I would have to write a isAlways function for each other type too (single, int8, ... ) (and also functions simplify,...).
Another approach of mine was, to write a function isAlways in the global namespace, and call the builtin isAlways when I encounter a sym. But, isAlways does not seem to be a built in, since Matlab reports
>> builtin('isAlways',sym(1));
Error using builtin
Cannot find builtin function 'isAlways'
Do you have any ideas how to solve this problem in a more elegant way, or at least another way?
Edit: I don't want to change the code of my function much, in particular don't want to add checks like switch class(x); case sym; ... case double;...`, but add "functionality" to Matlab such that it works out of the box for most functions.
In Matlab version approx R2018 I ended up defined a function isAlways in a folder #double etc.
Unfortunately, the behaviour changed around Matlab R2020. Now it is not possible anymore in a reliable way to add functions to classes. Thus, I now define a function isAlways in the global namespace, which gets called for each type which does not have an overload.
This is not the perfect solution, but it works at least.

MIJ: execute imageJ (Fiji) macro until end in Matlab

I am using MIJ to execute an ImageJ macro within Matlab. The macro has to be executed multiple times in a "for" loop. The problem is that Matlab does not wait until the macro ends. Initially I solved the problem with a "while" loop, checking if the "Results" table generated from the macro was empty or not. However, it only solves the problem the first time, then from the second time the "Results" table is not empty anymore.
I also thought about generating a variable at the end of the macro and use it to check if the macro finished, but I don't know how to read it in Matlab.
Do you have any suggestion about how I can solve the problem?
Thanks a lot in advance,
Alessia
Here is an example of my code:
javaaddpath 'C:/Program Files/MATLAB/R2019a_x64/java/ij.jar'
javaaddpath 'C:/Program Files/MATLAB/R2019a_x64/java/mij.jar'
MIJ.start('C:/fiji-win64/Fiji.app/plugins');
IJ=ij.IJ();
macro_path=...
'C:/Macro_waterinoil.ijm';
for pos=1:16
im = mijread(strcat('E:/droplets.tif'));
figure(1)
imshow(im,[0 255])
IJ.runMacroFile(java.lang.String(macro_path));
res_Hough=0;
res_Hough=MIJ.getResultsTable();
while res_Hough==0;
res_Hough=MIJ.getResultsTable();
end
im_res=MIJ.getCurrentImage();
MIJ.run('Clear Results');
MIJ.run('Close All');
end
Edit: Ignore the below, I was on the right track but wasn’t paying as much attention as I should have been. I think this issue is the line right after you set res_Hough to 0 (i.e. res_Hough=MIJ.getResultsTable();). Try deleting it so that the next line is the while loop, then we can check the output and see if the below might also apply
If I am understanding correctly, you are using the while loop to continuously ping until the table is full, then store those values in res_Hough, right? I'm wondering if this is a limitation inherent to ImageJ/FIJI. The reason I think this might be the case is that a very simple explanation for your issue is that the table retains the previous values, and so will always be full after the first loop unless cleared manually somehow. Do you think that could be the case? Perhaps add a print statement after the while loop and see if it prints the same values for the duration of the for loop.
I suppose the next thing to do is for me to actually try to offer a solution regardless of the above. My idea is to try having the while loop check against the previous iteration's table values until they are different, then stash the updated values. Does that make sense? Something like:
while (res_Hough[i]==0) or (res_Hough[i-1]==MIJ.getResultsTable());
res_Hough[i]=MIJ.getResultsTable();
Where i is incremented by the for loop

Variable output hash function

I know that there are hash functions that from a variable length input can give a fixed output. To take the simplest one, using the module of ten no matter how big is the input number I will always get an output between 0 and 9.
I need to do have from an unknown password, a variable length output. My first thought was to use the module, increasing the prim number as much as many digits I need to have as output.
My problems are:
I must handle short passwords as well as I would with long passwords;
I don't know how long should the output be before writing the program, and even though I would know after the user has set the password I may need to change it if he modifies the file.
My first thought was using a simple function and modify it based on my needs.
If I have to hash 123 but I need to have 5 characters as output, that's what I would do:
I add 2 zeros on the right, changing the input to 12300;
I take the lowest 5 digits prime number (10007);
And I then I have my hash doing 12300 % 10007 = 02293.
But since I would probably need output in the order of hundreds if not thousands I'm pretty sure module is not the solution to my problem.
I could also try to create my own hash function, but I have no idea how to verify if it works or if it's trash.
Are there some common solutions in literature for this kind of problem?

Residuals from ARMA estimation in MATLAB

I am trying to use the function armaxfilter from the MFE toolbox, but I get an error:
>> parameters = armaxfilter(y,1,1,1);
??? Error: File: armaxfilter.m Line: 477 Column: 21
Expression or statement is incorrect--possibly unbalanced (, {, or [.
Apparently my code is correct, as can be seen from an example from help:
EXAMPLE:
To fit a standard ARMA(1,1), use
parameters = armaxfilter(y,1,1,1)
Any idea on what is wrong?
In any case, my aim is getting residuals from an ARMA model estimation on a time series, a suggestion on an alternative way would be helpful as well.
Looking at the code (from here) , the issue is probably with the tilde output. If you are using an old version of MATLAB which does not support ~, you may get the error you mention.
There is a simple way to check this. Try out at the command line:
[~,idx] = min(1:10)
If this causes an error, you are using a version of MATLAB which does not support ~. If you want to use that particular code, you will have to either upgrade your MATLAB, or edit all the files such that examples of the tilde are replaced with some sort of dummy variable, e.g.:
[garbage,idx] = min(1:10)
As the error message describes, the problem lies in the armaxfilter.m. You should open that file and see what code is written at the specified line. I am sure you will see a bug in there.

How to get input from user in function

I want to ask about function M-file in MATLAB: if I want input from the user, how do I do that?
Can I write the input directly in the function M-file?
Or do I have to write an input statement in a script file and return to the function file to write another statement?
As Luis mentioned, the input function (documentation) prompts the user and then receives input. As far as I know, it will work fine whether in a function or in a script. If you find that it doesn't, post the code so folks can take a look at it.
input will treat what the user types as if you had typed it at a matlab command line. For example, if your code says
count=input('Enter a count')
and the user enters 3*5, the value of count will be 15. To get exactly "3*5", use input('Prompt','s') instead.