Skip to content

Commit bb9a251

Browse files
author
niharika2999
committed
Script updated with Linear indices function for Twist prototype
1 parent daee413 commit bb9a251

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

pysensors/optimizers/_gqr.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def fit(
8484
# Norm of each column
8585
dlens = np.sqrt(np.sum(np.abs(r) ** 2, axis=0))
8686
dlens_updated = f_region(self.constrainedIndices,dlens,p,j, self.nConstrainedSensors) #Handling constrained region sensor placement problem
87-
87+
8888
# Choose pivot
8989
i_piv = np.argmax(dlens_updated)
9090

@@ -151,7 +151,16 @@ def f_region(lin_idx, dlens, piv, j, const_sensors):
151151
dlens[didx] = 0
152152
return dlens
153153

154-
def getConstraindSensorsIndices(xmin, xmax, ymin, ymax, all_sensors):
154+
# a = np.isin(piv[j],lin_idx)
155+
156+
# if np.count_nonzero(a) < const_sensors:
157+
# dlens = dlens
158+
# else:
159+
# didx = np.isin(piv[j:],lin_idx,invert=False)
160+
# dlens[didx] = 0
161+
# return dlens
162+
163+
def getConstraindSensorsIndices(xmin, xmax, ymin, ymax, nx, ny, all_sensors):
155164
"""
156165
Function for mapping constrained sensor locations on the grid with the column indices of the basis_matrix.
157166
@@ -175,7 +184,7 @@ def getConstraindSensorsIndices(xmin, xmax, ymin, ymax, all_sensors):
175184
"""
176185
n_features = len(all_sensors)
177186
imageSize = int(np.sqrt(n_features))
178-
a = np.unravel_index(all_sensors, (imageSize,imageSize))
187+
a = np.unravel_index(all_sensors, (nx,ny))
179188
constrained_sensorsx = []
180189
constrained_sensorsy = []
181190
for i in range(n_features):
@@ -190,7 +199,17 @@ def getConstraindSensorsIndices(xmin, xmax, ymin, ymax, all_sensors):
190199
if len(constrained_sensorsx) == 0: ##Check to handle condition when number of sensors in the constrained region = 0
191200
idx_constrained = []
192201
else:
193-
idx_constrained = np.ravel_multi_index(constrained_sensors_tuple, (imageSize,imageSize))
202+
idx_constrained = np.ravel_multi_index(constrained_sensors_tuple, (nx,ny))
203+
return idx_constrained
204+
205+
def getConstrainedSensorsIndicesLinear(xmin,xmax,ymin,ymax,df):
206+
x = df['X (m)'].to_numpy()
207+
n_features = x.shape[0]
208+
y = df['Y (m)'].to_numpy()
209+
idx_constrained = []
210+
for i in range(n_features):
211+
if (x[i] >= xmin and x[i] <= xmax) and (y[i] >= ymin and y[i] <= ymax):
212+
idx_constrained.append(i)
194213
return idx_constrained
195214

196215
def boxConstraints(position,lowerBound,upperBound,):
@@ -222,6 +241,8 @@ def functionalConstraint(position, func_response,func_input, freeTerm):
222241
n_row, n_col = 2, 3
223242
n_components = n_row * n_col
224243
image_shape = (64, 64)
244+
nx = 64
245+
ny = 64
225246

226247
def plot_gallery(title, images, n_col=n_col, n_row=n_row, cmap=plt.cm.gray):
227248
'''Function for plotting faces'''
@@ -241,8 +262,8 @@ def plot_gallery(title, images, n_col=n_col, n_row=n_row, cmap=plt.cm.gray):
241262

242263
#Find all sensor locations using built in QR optimizer
243264
max_const_sensors = 230
244-
n_const_sensors = 7
245-
n_sensors = 50
265+
n_const_sensors = 2
266+
n_sensors = 200
246267
optimizer = ps.optimizers.QR()
247268
model = ps.SSPOR(optimizer=optimizer, n_sensors=n_sensors)
248269
model.fit(X)
@@ -254,7 +275,12 @@ def plot_gallery(title, images, n_col=n_col, n_row=n_row, cmap=plt.cm.gray):
254275
xmax = 40
255276
ymin = 25
256277
ymax = 45
257-
sensors_constrained = getConstraindSensorsIndices(xmin,xmax,ymin,ymax,all_sensors) #Constrained column indices
278+
sensors_constrained = getConstraindSensorsIndices(xmin,xmax,ymin,ymax,nx,ny,all_sensors) #Constrained column indices
279+
280+
# didx = np.isin(all_sensors,sensors_constrained,invert=False)
281+
# const_index = np.nonzero(didx)
282+
# j =
283+
258284

259285
##Plotting the constrained region
260286
# ax = plt.subplot()

0 commit comments

Comments
 (0)