Message Boards Message Boards

Deploy a custom function to the cloud

Posted 11 years ago

Hi there! I've just discovered the Wolfram Cloud service and I wanted to deploy a custom function to test the service but I've got some problems.

Here's what I'm trying:

optimize[d_, p_] := Module[{n, m, q, k, A, b, result},
    n=Length[d]; 
    m=Dimensions[p][[2]]; 
    vec[m_]:=Flatten[m[Transpose]]; 
    mat[v_]:=Partition[v,n][Transpose]; 
    j[k_]:=ConstantArray[1,k]; 
    q=j[m]; 
    k=vec[p (d[TensorProduct]q)]; 
    A=ArrayFlatten[{IdentityMatrix[m][TensorProduct]j[n]}]; 
    b=j[m][TensorProduct]{1,0}; 
    result = mat[LinearProgramming[k,A,b]]; 
    result 
]; 
CloudDeploy[APIFunction[{"d" -> "List", "p" -> "List"}, optimize[#d, #p] &, "String"]]

I'm using List as a parameter because d and p are like

d={7,5,11,3};
p=({{5,2,5,3,7,1,1,5,2,5,3,7,1,1},{2,8,3,2,7,2,3,2,8,3,2,7,2,3},{3,6,4,5,9,1,1,3,6,4,5,9,1,1},{4,6,2,8,14,8,4,4,6,2,8,14,8,4}});

Now, the function works fine in my local notebook and hitting evaluate actually makes the deploy.

The problem is that the API doesn't return what I expect.

While I expect a string that contains the result matrix, it outputs:

Transpose[LinearProgramming[LinearProgramming[Transpose[{{{5,2,5,3,7,1,1,5,2,5,3,7,1,1},{2,8,3,2,7,2,3,2,8,3,2,7,2,3},{3,6,4,5,9,1,1,3,6,4,5,9,1,1},{4,6,2,8,14,8,4,4,6,2,8,14,8,4}} {{7,5,11,3}} ? ConstantArray[1, {1}[[2]]]}]], LinearProgramming[ArrayFlatten[{IdentityMatrix[{1}[[2]]] ? {1}}]], LinearProgramming[ConstantArray[1, {1}[[2]]] ? {1, 0}]]]

and I don't understand why.

Do you know what the problem is?

POSTED BY: Stefano T
4 Replies

The only issue is that "List" is not an allowed type for Interpreter (which is automatically used inside the APIFunction). To make your function work, just use "Expression" instead of "List" in your APIFunction definition.

POSTED BY: Chad Knutson

Are you sure that the code fro optimize that is posed above is what you intended? Things like

Flatten[m[Transpose]]

Partition[v,n][Transpose]

and so on do not make syntactic sense. So not surprisingly the example as it is posted above (though it may be different in your notebook and possibly a problem with the markup of the code in this forum) does not work and generates a bunch of expected errors....

POSTED BY: David Reiss
Posted 11 years ago

It's just a copy and paste issue, the code is fine in the online notebook and it works if evaluate it, that's why I don't understand what's wrong. If I write and evaluate this code in my online notebook, it prints out the correct result (keep in mind that copy and paste removes some symbols and replaces them with strings like [Transpose])

optimize[d_, p_] := Module[{n, m, q, k, A, b, result}, 
    n=Length[d]; 
    m=Dimensions[p][[2]]; 
    vec[m_]:=Flatten[m\[Transpose]]; 
    mat[v_]:=Partition[v,n]\[Transpose]; 
    j[k_]:=ConstantArray[1,k]; 
    q=j[m]; k=vec[p (d\[TensorProduct]q)]; 
    A=ArrayFlatten[{IdentityMatrix[m]\[TensorProduct]j[n]}]; 
    b=j[m]\[TensorProduct]{1,0}; 
    result = mat[LinearProgramming[k,A,b]]; 
    result 
]; 
d={7,5,11,3}; 
p=({{5,2,5,3,7,1,1,5,2,5,3,7,1,1},{2,8,3,2,7,2,3,2,8,3,2,7,2,3},{3,6,4,5,9,1,1,3,6,4,5,9,1,1},{4,6,2,8,14,8,4,4,6,2,8,14,8,4}}); 
optimize[d,p]

Exporting it as an API gives the result that I wrote on the first post.

POSTED BY: Stefano T
Posted 11 years ago

This did the trick, thanks for your support!

POSTED BY: Stefano T
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract