This is a follow up to my previous post here
I am trying to run a function from Python using wolframclient
. The input arguments are passed from Python as Python datatypes.
The following is saved in test.m file (for Mathematica script please check)
solutioncheck[edges_, vd_, vl_, ew_] := (
edges = UndirectedEdge @@@ edges; vcoords = List @@ vd;
ew = Normal @ KeyMap[UndirectedEdge @@ # &,ew];
g3d = Graph3D[vl, edges, VertexCoordinates -> vcoords, EdgeWeight->ew, VertexLabels->Placed["Name",Center],
EdgeLabels->{e_:>Placed["EdgeWeight",Center]}, VertexSize->.5, BaseStyle->16];
vars3d = Array[Through[{x, y, z}@#] &, Length@vd];
\[Lambda] = 1/100.; lbnd = 0; ubnd = 500;
obj3d = Total[(Norm[vars3d[[First@#]] - vars3d[[Last@#]]] - # /. (Rule @@@ ew))^2 & /@ EdgeList[g3d]] + \[Lambda] * Total[Norm /@ (vars3d - Values@vd)];
solution3d = Last @ Minimize[{obj3d, And @@ Thread[lbnd <= Join @@ vars3d <= ubnd]}, Join @@ vars3d];
edgeLengths3d = # -> Norm[vars3d[[First@#]] - vars3d[[Last@#]]] /.solution3d & /@ EdgeList[g3d];
ResourceFunction["PrettyGrid"][{#, # /. ew, # /. edgeLengths3d} & /@EdgeList[g3d], "ColumnHeadings" -> {"edge", "EdgeWeight", "Edge Length"}];
z1 = Values[solution3d] // Partition[#, 3] &;
theFile = File["result.txt"];
Export[theFile, z1, "Table"];
)
Using wolframclient
library in Python, I tried
from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr, Global
session = WolframLanguageSession()
edges = [(1, 2), (1, 3), (1, 4), (2, 5), (2, 6), (5, 6), (3, 4), (3, 7), (6, 7), (7, 8), (2, 9)]
vl = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ew = {(1, 2): 49.6, (1, 3): 74.4, (1, 4): 49.6, (2, 5): 37.2, (2, 6): 74.4, (5, 6): 49.6, (3, 4): 37.2, (3, 7): 24.8, (6, 7): 62, (7, 8): 37.2, (2, 9): 24.8}
vd = {1: [75., 25., 0], 2: [115., 45., 0], 3: [10., 5., 0], 4: [45., 0, 0], 5: [90., 60., 0], 6: [45., 55., 0], 7: [0, 25., 0], 8: [10., 50., 0], 9: [115., 25., 0]}
with WolframLanguageSession() as s:
s.evaluate(wl.Get('test.m'))
s.evaluate(Global.solutioncheck(edges, vd, vl, ew))
I am not sure what's wrong in the above, the run doesn't terminate.
Could someone please look into this?
EDIT: For some reason, I think the graph object isn't created
I get the following message when I Print(ew)
A graph object is expected at position 1 in EdgeList[Power[Plus[Times[-1, g3d], Norm[Plus[Part[{{x[1], y[1], z[1]}, {x[2], y[2], z[2]}, {x[3], y[3], z[3]}, {x[4], y[4], z[4]}, {x[5], y[5], z[5]}, {x[6], y[6], z[6]}, {x[7], y[7], z[7]}, {x[8], y[8], z[8]}, {x[9], y[9], z[9]}}, First[g3d]], Times[-1, Part[{{x[1], y[1], z[1]}, {x[2], y[2], z[2]}, {x[3], y[3], z[3]}, {x[4], y[4], z[4]}, {x[5], y[5], z[5]}, {x[6], y[6], z[6]}, {x[7], y[7], z[7]}, {x[8], y[8], z[8]}, {x[9], y[9], z[9]}}, Last[g3d]]]]]], 2]].
Tag Times in Times[500, obj3d, Print] is Protected.
Nonatomic expression expected at position 1 in First[g3d].
The expression First[g3d] cannot be used as a part specification.
Further output of MessageName[Part, pkspec1] will be suppressed during this calculation.
Nonatomic expression expected at position 1 in Last[g3d].
The expression Last[g3d] cannot be used as a part specification.
The expression First[g3d] cannot be used as a part specification.
The expression Last[g3d] cannot be used as a part specification.
A graph object is expected at position 1 in EdgeList[g3d -> Norm[Plus[Part[{{250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}}, First[g3d]], Times[-1, Part[{{250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}, {250, 250, 250}}, Last[g3d]]]]]].
Tag Times in Times[edgeLengths3d, Print, {x[1] -> 250, y[1] -> 250, z[1] -> 250, x[2] -> 250, y[2] -> 250, z[2] -> 250, x[3] -> 250, y[3] -> 250, z[3] -> 250, x[4] -> 250, y[4] -> 250, z[4] -> 250, x[5] -> 250, y[5] -> 250, z[5] -> 250, x[6] -> 250, y[6] -> 250, z[6] -> 250, x[7] -> 250, y[7] -> 250, z[7] -> 250, x[8] -> 250, y[8] -> 250, z[8] -> 250, x[9] -> 250, y[9] -> 250, z[9] -> 250}] is Protected.
{edgeLengths3d} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.
A graph object is expected at position 1 in EdgeList[{g3d, g3d, ReplaceAll[g3d, edgeLengths3d]}].
Further output of MessageName[EdgeList, graph] will be suppressed during this calculation.
A graph object is expected at position 1 in EdgeList[{Item[edge, Alignment -> {Center, Baseline}, BaseStyle -> Bold, ItemSize -> {Automatic, 1.5}], Item[EdgeWeight, Alignment -> {Center, Baseline}, BaseStyle -> Bold, ItemSize -> {Automatic, 1.5}], Item[Edge Length, Alignment -> {Center, Baseline}, BaseStyle -> Bold, ItemSize -> {Automatic, 1.5}]}, {g3d, g3d, ReplaceAll[g3d, edgeLengths3d]}].
First argument ... is not a string
. Youredges, vd ,vl, ew
are already in list/association forms at the point of calling the function so you don't needImportString
and you can remove the entire line{edges,vl,ew,vd}=ImportString[#,"PythonExpression"]&/@{edges,vl,ew,vd};
. $\endgroup$solution3d
appearing on a line starting withedgeLengths3d = ...
as a replacement rule. Yet you haven't even defined solution3d yet, which appears later on the line startingsolution3d = Last @ Minimize[ ...
. Also it has the same name as the function it is in, so you should change that. AlsoedgeLengths3d
gets defined twice. $\endgroup$