Repository navigation
Expand file tree
/
Copy pathtesting.py
More file actions
167 lines (138 loc) · 5.17 KB
/
Copy pathtesting.py
File metadata and controls
167 lines (138 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from framework import *
from pebble_game import *
from constructive_pebble_game import *
from nose.tools import ok_
import numpy as np
from scipy.optimize import minimize
from scipy.optimize import linprog
# initialise the seed for reproducibility
# np.random.seed(102)
# fw = make_nice_fw(20, 1)
# # fw = create_reduced_fw(20, 1)
# flag, comps = pebble_game(fw)
# draw_comps(fw, comps)
# xs1 = np.arange(4, 12, 1.0)
# ys1 = np.arange(4, 12, 2.0)
# ys2 = np.arange(5, 12, 2.0)
# xs2 = np.arange(4.5, 12.5, 1.0)
# xy1 = np.array(np.meshgrid(xs1, ys1)).T
# xy2 = np.array(np.meshgrid(xs2, ys2)).T
# positions = np.concatenate((xy1,xy2)).reshape((-1,2))
# nodes = list(range(len(positions)))
# edges = delaunay_to_edges(Delaunay(positions))
# fw = create_framework(nodes, edges, positions)
# draw_framework(fw)
xs = np.arange(4,12,1)
ys = np.arange(4,12,1)
positions = np.array(np.meshgrid(xs,ys)).T.reshape((-1,2))
nodes = list(range(len(positions)))
edges = delaunay_to_edges(Delaunay(positions))
fw = create_framework(nodes, edges, positions)
draw_framework(fw)
source = (17,18)
target = (53,54)
# fw.add_edges_from([source, target])
# fw = add_lengths_and_stiffs(fw)
fw = tune_network(fw, source, target, nstars=[1.0])
tensions = [0]*len(fw.edges)
edge_dict = {edge: i for i, edge in enumerate(fw.edges)}
tensions[edge_dict[source]] = 1
strains = exts_to_strains(fw, extensions(fw, tensions))
draw_strains(fw, strains, ghost=True)
s_max = strains[edge_dict[source]]
N = 30
u0 = np.zeros(len(fw.nodes) * 2)
for i in range(N):
strain_val = 0.4 *s_max * (i/(N-1))
print("target strain val",strain_val)
constraints = {"type":"eq", "fun":source_strain, "args":(fw, source, strain_val)}
mind = minimize(energy, u0, args=(fw), constraints=constraints)
# print("minimized energy",mind.fun)
print("minimiser:",mind)
# use the new positions as the starting guess for the next round of optimisation
u0 = mind.x
draw_framework(update_pos(fw, mind.x), filename="anim5/anim_"+str(i)+".png",ghost=True)
plt.close()
print("drawn",i+1,"images of",N)
# flag, comps = pebble_game(fw)
# draw_comps(fw,comps)
# test of creating a dictionary to keep track of edges
# edge_dict = {edge: i for i, edge in enumerate(fw.edges)}
# # modifying the framework to change two bonds to ghost bonds (source and target)
# source = (9, 12)
# target = (16, 17)
# fw.edges[source]["lam"] = 0
# fw.edges[target]["lam"] = 0
# tensions = [0]*len(fw.edges)
# tensions[edge_dict[source]] = 1
# strains = exts_to_strains(fw, extensions(fw, tensions))
# print("initial strain ratio:",strains[edge_dict[target]]/strains[edge_dict[source]])
# draw_strains(fw, strains, ghost=True)
# # aiming for proportional movement of (16,17) when (9,12) moves
# nstars = [1.0]
# # calculating ns test
# it = 0
# min_cost = np.inf
# while min_cost > 0.0001 and it < 10000:
# costs = []
# exts_list = all_extensions(fw, tensions)
# # for exts in exts_list:
# # strains = exts_to_strains(fw, exts)
# # ns = [strains[edge_dict[(16,17)]] / strains[edge_dict[(9,12)]]]
# # costs.append(cost_f(ns, nstars))
# for i, exts in enumerate(exts_list):
# strains = exts_to_strains(fw, exts)
# ns = [strains[edge_dict[target]] / strains[edge_dict[source]]]
# costs.append(cost_f(ns, nstars))
# min_cost = min(costs)
# index_to_remove = costs.index(min_cost)
# edge_to_remove = list(fw.edges)[index_to_remove]
# fw.edges[edge_to_remove]["lam"] = 0
# print("iteration:",it,"cost:",min_cost,"removed:",edge_to_remove)
# it+=1
# strains = exts_to_strains(fw, extensions(fw, tensions))
# draw_strains(fw, strains, ghost=True)
# # s_max = 1.2
# # for i in range(20):
# # strain_val = s_max * (i/4)
# # constraints = {"type":"eq", "fun":source_strain, "args":(fw, source, strain_val)}
# # u0 = np.zeros(len(fw.nodes) * 2)
# # mind = minimize(energy, u0, args=(fw), constraints=constraints)
# # draw_framework(update_pos(fw, mind.x), filename="anim/anim_"+str(i)+".png",ghost=True)
# # print("drawn",i+1,"images of",20)
# # triangle = create_framework([0,1,2], [(0,1), (0,2), (1,2)], [(1,0), (3,0), (2,2)])
# # draw_framework(triangle)
# # f = np.zeros(2*len(triangle.nodes))
# # f[0] = 1
# # f[2] = -1
# # Rold = rig_mat(triangle,2)
# # R = create_augmented_rigidity_matrix(triangle,2)
# # print(R)
# # print("NEW:",np.array([-1/2, 0, 0,0,0,0]).dot(R))
# # print("OLD:",np.array([-1/2, 0, 0]).dot(Rold))
# # print(R.dot(R.T))
# # draw_strains(triangle, f)
# # shape = create_framework([0,1,2,3,4], [(0,1), (0,2), (1,2),(3,4), (2,3), (2,4),(1,4)], [(1,0), (3,0), (2,2),(1,3),(3,3)])
# # draw_framework(shape)
# # f = np.zeros(2*len(shape.nodes))
# # f[0] = 0
# # f[1] = 0
# # f[2] = 3
# # f[4] = -1
# # f[5] = 1.5
# # draw_strains(shape, f)
# # square = create_framework([0,1,2,3], [(0,1), (0,2), (3,2),(3,1)], [(1,0), (3,0), (1,2),(3,2)])
# # draw_framework(square)
# # f = np.zeros(2*len(square.nodes))
# # f[0] = 0.1
# # f[1] = 0.1
# # f[2] = -0.1
# # f[3] = -0.1
# # f[4] =
# # f[5] = 1.5
# # draw_tensions(square, f)
# # triangle = add_lengths(triangle)
# # tensions = [0]*len(triangle.edges)
# # tensions[0] = 1
# # tensions[1] = -1
# # print(extensions(triangle, tensions))