How to unroll an inner product calculation using julia macro? - macros

Using JuMP I want to make an objective function like this:
#objective(m, Max, 19*run[1] + 15*run[2] + 15*run[3])
... where 19, 15 and 15 are the utilities of activities, and run[i] is a binary JuMP variable describing if the activity should be run or not.
I want to make a macro that generates this objective, so that given a list of utilities=(19, 15, 15) instead of the above, I can write something like this:
#myObjective(m, Max, utilities, run)
and that will function like the objective I typed in above.
(the reasoning is that I don't want to use a quadratic objective, so I can't use a JuMP variable "utility" and let the objective be sum(run[i]*utility[i]). However since since I do know the utilities in advance, a macro can perhaps be written to write a nice linear objective function. For three activities a macro wouldn't help much, but in real problems there will be lots of activities, so it will be helpful.
I've been struggling for a while now and I'm not getting any traction, does anyone know how to easily fix this?
Also any other approaches to solving this problem not using macros would be useful :-)

Unless utilities is a JuMP Variable, you can do
#objective(m, Max, dot(utilities, run)), that's not quadratic

Related

how to understand the linkagemex function inside of the defaule linkage function in matlab

I need to rewrite the linkage function in matlab. Now, as I examine it, I realized there is a method called linkagemex inside of it. But I simply cannot step into this method to see its code. Can anyone help me out with this strange situastion?
function Z= linkage (Y, method, pdistArg, varargin)
Z=linkagemex(Y,method);
PS. I think I am pretty good at learning, but matlab is not so easy to learn. If you have good references to learn it well, feel free to let me know. Thanks very much for your time and attention.
As #m.s. mentions, you've found a call to a MEX function. MEX functions are implemented as C code that is compiled into a function callable by MATLAB.
As you've found, you can't step into this method (as it is compiled C code, not MATLAB code), and you don't have access to the C source code, as it's not supplied with MATLAB.
Normally, you would be at kind of a dead end here. Fortunately, that's not quite the case with linkagemex. You'll notice on line 240 of linkage.m that it actually does a test to see whether linkagemex is present. If it isn't, it instead calls a local subfunction linkageold.
I think you can assume that linkageold does at least roughly the same thing as linkagemex. You may like to test them out with a few suitable input arguments to see if they give the same results. If so, then you should be able to rewrite linkage using the code from linkageold rather than linkagemex.
I'm going to comment more generally, related to your PS. Over the last few days I've been answering a few of your questions - and you do seem like a fast learner. But it's not really that MATLAB is hard to learn - you should realize that what you're attempting (rewriting the clustering behaviour of phytree) is not an easy thing to do for even a very advanced user.
MathWorks write their stuff in a way that makes it (hopefully) easy to use - but not necessarily in a way that makes it easy for users to extend or modify. Sometimes they do things for performance reasons that make it impossible for you to modify, as you've found with linkagemex. In addition, phytree is implemented using an old style of OO programming that is no longer properly documented, so even if you have the code, it's difficult to work out what it even does, unless you happen to have been working with MATLAB for years and remember how the old style worked.
My advice would be that you might find it easier to just implement your own clustering method from scratch, rather than trying to build on top of phytree. There will be a lot of further headaches for you down the road you're on, and mostly what you'll learn is that phytree is implemented in an obscure old-fashioned way. If you take the opportunity to implement your own from scratch, you could instead be learning how to implement things using more modern OO methods, which would be more useful for you in the future.
Your call though, that's just my thoughts. Happy to continue trying to answer questions when I can, if you choose to continue with the phytree route.
You came across a MEX function, which "are dynamically linked subroutines that the MATLAB interpreter loads and executes". Since these subroutines are natively compiled, you cannot step into them. See also the MATLAB documentation about MEX functions.

How to override the help a function in matlab?

I have noticed that when a function is overridden the help fcn is not. I created a custom function using the same name as a built in function. Trying:
which fcn
Yields the right\function.m, but when I use the help function
help fcn
I get the help for the built in function. However, if I instead type
help fcn.m
I get the correct help. This is ambiguous and disturbing and may lead to confusion about which file is the used one. That is why I wonder: is it possible to override the help as well?.
I know that many does not recommend overriding in matlab, but there are times when it may be worth. For this occation it means that I does not need to change all legacy code where the function is used and also it is not good to have too many version of a function (mycdfplot is already taken and some may have local versions as well).
Short answer: NO.
(sorry)
To develop a bit:
If you overload your own function, you can force the help to point to a specific function by playing with the function precedence order and the order of the path. But for Matlab built-in function, they always take precedence over your custom functions.
The only way for the help to reach your custom function is how you described, by specifying .m
From Matlab (2013b) help:
Note: When multiple programs have the same name, the help command
determines which help text to display by applying the rules described
in Function Precedence
Order.
However, if a program has the same name as a MathWorks function, the
Help on Selection option in context menus always displays
documentation for the MathWorks function.

Where can I find good and simple test functions for evolutionary algorithms?

I've started learning evolutionary algorithms (GA, PSO, ...) and I want to implement them in Matlab and play with different parameters to get a hold of the algorithms' structures and how they work.
My problem is, I don't have some simple test functions to use. For example, functions with multiple peaks/valleys, one global minimum and multiple local ones, .... Nothing complicated, just some simple mathematical functions with their formulas.
I can try to make some up with putting some sin/cos/exp together, but it'll take time and is really frustrating!
Anybody knows of a resource (site, book, ...) that have these listed?
Here is a set from our very own #Rody Oldenhuis:
Test functions
You might want to try those in the BBOB benchmark set. There is also some nice accompanying literature to this set in form of the corresponding GECCO workshop.
Some of the classic functions were mentioned by AGS already and include Rastrigin, Rosenbrock and Generalized Rosenbrock, Schwefel, Sphere, Griewank, etc.. We have also implemented these and more in HeuristicLab, so if you want to experiment you can also try that (PSO and GA are included also).

Needed tutorial on FANUC GCODE

G-code is a language for controlling CNC machines (lathes, mills).
I have a university assignment that I need conceptual help with. The part of the job that I am having problems with is writing two short programs in G-code (Fanuc corporation dialect). I have a short list of keywords and an example program, but that doesn't seem to be enough to to learn the language.
From my search of the net, I found that the various dialects of G-code are not compatible. However, I found no one tutorial on this specific one.
Also, automatically generating code for the given problems doesn't seem to be an option, since I suspect I will be asked to explain the inner workings of the programs, when presenting them. Furthermore, teachers at my university seem to insist very strongly on doing things Their way, so ... I guess I'll just have to learn the damn thing.
Q: Where can I find a concise (I want to spend no more than 2-3 days on the whole thing) tutorial for the Fanuc dialect of G-code?
I would suggest to take a look at LinuxCNC command lists here or here, as well as a concise version here on CAMotics page. Some special points for Fanuc-compatible controls (e.g. Haas, Mach3):
You can have comments inside the parentheses. Basically anything inside (...) will not be executed.
You can have parameters / variables. For example you can assign float value of 12.3 to variable slot 101 by #101 = 12.3
You can call those variables, For example X#101 is equal to X12.3
You may have mathematical expressions and then store the value in another variable slot. For example #3 = [#1 + #2], however you can't have X = [#1 + #2] or X[#1 + #2].
You may find more in depth information here in this article by Benjamin Jurke.

How to access an array of structs in simulink?

I have the problem, that I have to access a funktion form a dll in matlab/Simulink in the rtw.
This should work with a s function, but I have the needed parameters in a array of structures organized.
The question is now how I can reach them when I want to call my DLL function?
Or is there a better way (e.g. level 2 Matlab files or something similar)?
The pure simulation (without RTW) worked pretty well with level 2 m files but I am not able to write a tlc file for compiling them. I did not find much on the net and the documentation only about C sources.
Thanks
Christian
For signals in Simulink, what you are asking for is an array of buses. There is similar support for using arrays of structs for parameters. For calling an external function, you might want to look at the legacy code tool. You might also be able to use the MATLAB function block to call your external dll.
In addition to what #MikeT says:
Generating code from Level 2 M-S-Functions is problematic. Read this: http://www.mathworks.co.uk/help/toolbox/simulink/sfg/f7-67622.html#brgscav-1
Also, M-S-functions are generally slow, because they run in the MATLAB interpreter: http://blogs.mathworks.com/seth/2010/10/28/tips-for-simulation-performance/
In the end I coded the problem in C and used an array where I defined to order of the elements. Then I wrote some interface functions to access this "virtual" struct.
This is not very good coding but the easiest way I have found and it is portable.
Thanks