This is a follow up to my question posted here I am trying to run the code posted here as an answer in the above-mentioned link here from Python
I tried,
from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr, Global
edges = [(1, 2), (1, 3), (1, 4), (2, 5), (2, 6), (5, 6), (3, 4), (3, 7), (6, 7), (7, 8), (2, 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}
with WolframLanguageSession() as s:
s.evaluate(wl.Get('wolfram_input.m'))
s.evaluate(Global.solution3d(edges, ew))
wolfram_input.m contains Mathematica expressions
solution3d[uedges_, edgeweight_] := Module[{
edges = UndirectedEdge @@@ uedges;
ew = KeyMap[UndirectedEdge @@ # &, edgeweight];
verts, graph, dmat, hmat, dist2mat, bmat, dim, newcoords, newLayout, EdgeLabels, VertexSize, vars, weights, objective,
min, vals, newercoords},
verts = Union[Flatten[edges /. UndirectedEdge -> List]];
graph = Graph3D[verts, edges, EdgeWeight -> ew, VertexLabels -> Placed["Name", Center],
EdgeLabels -> {e_ :> Placed["EdgeWeight", Center]}, VertexSize -> .3, VertexStyle -> Red];
dmat = GraphDistanceMatrix[graph];
DistanceMatrixDimensionReduce[(dmat_)?MatrixQ, dim_: 3] :=
With[{len = Length[dmat]},
Module[{diffs, dist2mat, onevec, hmat, bmat, uu, ww, vv},
onevec = ConstantArray[{1}, len];
hmat = IdentityMatrix[len] - onevec.Transpose[onevec]/len;
dist2mat = -dmat/2;
bmat = hmat.dist2mat.hmat; {uu, ww, vv} =
SingularValueDecomposition[bmat, dim]; uu.Sqrt[ww]] /;
dim <= Length[dmat[[1]]] && MatchQ[Flatten[dmat], {_Real ..}]];
newcoords = DistanceMatrixDimensionReduce[dmat];
newLayout = Graph[verts, edges, VertexCoordinates -> vcoords2, EdgeWeight -> ew,
VertexLabels -> Placed["Name", Center];
EdgeLabels -> {e_ :> Placed["EdgeWeight", Center]};
VertexSize -> .3, VertexStyle -> Red];
vars = Array[xyz, {Length[verts], 3}];
weights = Normal[WeightedAdjacencyMatrix[graph]];
objective =
Sum[If[weights[[i, j]] >
0, ((vars[[i]] - vars[[j]]).(vars[[i]] - vars[[j]]) -
weights[[i, j]]^2)^2, 0], {i, Length[weights] - 1}, {j, i + 1,
Length[weights]}];
{min, vals} =
FindMinimum[objective,
Flatten[MapThread[List, {vars, newcoords}, 2], 1]];
Print[min];
newercoords = vars /. vals;
]
I think there is a problem with the local variable specification.
Local variable specification {CompoundExpression[Set[edges, Apply[UndirectedEdge, {{1, 2}, {1, 3},
{1, 4}, {2, 5}, {2, 6}, {5, 6}, {3, 4}, {3, 7}, {6, 7}, {7, 8}, {2, 9}}, {1}]], Set[ew,
KeyMap[Function[Apply[UndirectedEdge, Slot[1]]], Association[{{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}]]], verts], graph, dmat, hmat, dist2mat, bmat, dim, newcoords, newLayout,
EdgeLabels, VertexSize, vars, weights, objective, min, vals, newercoords} contains
CompoundExpression[Set[edges, Apply[UndirectedEdge, {{1, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}, {5, 6}, {3,
4}, {3, 7}, {6, 7}, {7, 8}, {2, 9}}, {1}]], Set[ew, KeyMap[Function[Apply[UndirectedEdge, Slot[1]]],
Association[{{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}]]], verts], which is
not a symbol or an assignment to a symbol.
Could someone help me with this?
EDIT:
solution3d[uedges_, edgeweight_] := Module[{
edges = UndirectedEdge @@@ uedges,
ew = KeyMap[UndirectedEdge @@ # &, edgeweight],
verts = Union[Flatten[edges /. UndirectedEdge -> List]],
graph, dmat, newcoords, vcoords3, newLayout, vars, weights,
objective, min, vals, newercoords, xyz},
graph =
Graph3D[verts, edges, EdgeWeight -> ew,
VertexLabels -> Placed["Name", Center],
EdgeLabels -> {e_ :> Placed["EdgeWeight", Center]},
VertexSize -> .3, VertexStyle -> Red];
dmat = GraphDistanceMatrix[graph];
DistanceMatrixDimensionReduce[(dmat_)?MatrixQ, dim_: 3] :=
With[{len = Length[dmat]},
Module[{diffs, dist2mat, onevec, hmat, bmat, uu, ww, vv},
onevec = ConstantArray[{1}, len];
hmat = IdentityMatrix[len] - onevec.Transpose[onevec]/len;
dist2mat = -dmat/2;
bmat = hmat.dist2mat.hmat; {uu, ww, vv} =
SingularValueDecomposition[bmat, dim]; uu.Sqrt[ww]] /;
dim <= Length[dmat[[1]]] && MatchQ[Flatten[dmat], {Real ..}]];
newcoords = DistanceMatrixDimensionReduce[dmat];
newLayout =
Graph[verts, edges, VertexCoordinates -> newcoords,
EdgeWeight -> ew, VertexLabels -> Placed["Name", Center],
EdgeLabels -> {e :> Placed["EdgeWeight", Center]},
VertexSize -> .3, VertexStyle -> Red];
vars = Array[xyz, {Length[verts], 3}];
weights = Normal[WeightedAdjacencyMatrix[graph]];
objective =
Sum[If[weights[[i, j]] >
0, ((vars[[i]] - vars[[j]]).(vars[[i]] - vars[[j]]) -
weights[[i, j]]^2)^2, 0], {i, Length[weights] - 1}, {j, i + 1,
Length[weights]}];
{min, vals} =
FindMinimum[objective,
Flatten[MapThread[List, {vars, newcoords}, 2], 1]];
newercoords = vars /. vals;
vcoords3 = MapIndexed[#2[[1]] -> # &, newercoords];
newLayout =
Graph3D[verts, edges, VertexCoordinates -> vcoords3,
EdgeWeight -> ew, VertexLabels -> Placed["Name", Center],
EdgeLabels -> {e_ :> Placed["EdgeWeight", Center]},
VertexSize -> .3, VertexStyle -> Red];
Return[<|"newcoords" -> newcoords|>];
]
I have tried to correct the syntax errors in the code. But the run is not successful yet. I have added the output logs here.
I look forward to suggestions that will help me in resolving these errors.
Print(min)at the end - you needPrint[min];(a semicolon and square brackets are both necessary). – flinty Oct 04 '20 at 15:53Modulevariable specifications, e.g. the variable here:edges = UndirectedEdge @@@ uedges;– b3m2a1 Oct 04 '20 at 16:55