7
$\begingroup$

Following my previous topic on how to optimize a complex function, I decided to write a custom algorithm because I need this task to be run locally and on machines with no access to Mathematica.

Unfortunately the performance differences between my java code and the Mathematica's one are really huge and I need another solution.

I've just discovered Wolfram Cloud and this seems the best way to make everything work because I can just execute my Mathematica's notebook, so I subscribed to the free tier to give it a try.

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? Their documentation is not that helpful and I'm quite clueless.

$\endgroup$

1 Answer 1

3
$\begingroup$

Got an answer from the support team and it seems that the type List can't be used but we need to use Expression.

Changing

CloudDeploy[APIFunction[{"d" -> "List", "p" -> "List"}, optimize[#d, #p] &, "String"]]

to

CloudDeploy[APIFunction[{"d" -> "Expression", "p" -> "Expression"}, optimize[#d, #p] &, "String"]]

made everything work.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.