本文整理汇总了Python中utils.Utils.get_grid_indices方法的典型用法代码示例。如果您正苦于以下问题:Python Utils.get_grid_indices方法的具体用法?Python Utils.get_grid_indices怎么用?Python Utils.get_grid_indices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.Utils
的用法示例。
在下文中一共展示了Utils.get_grid_indices方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: display_LP_problem
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import get_grid_indices [as 别名]
def display_LP_problem(self, coef_matrix, vector):
grid_indices = Utils.get_grid_indices(self.no_points_per_axis, False)
no_rows = coef_matrix.size[0]
no_cols = coef_matrix.size[1]
no_d_constraints = (no_rows - 2 * self.no_vars) / 2
print 'LP problem:'
for row in range(no_rows):
non_zeros_indices = [(row + col * no_rows) for col in range(no_cols) \
if coef_matrix[row + col * no_rows] != 0.0]
# List of type [(value, true_row, true_col)], where value is either 1 or -1.
true_non_zero_indices = [(coef_matrix[non_zeros_index], row, non_zeros_index / no_rows)\
for non_zeros_index in non_zeros_indices]
terms = []
for true_non_zero_index in true_non_zero_indices:
is_one = true_non_zero_index[0] == 1.0
tup = grid_indices[true_non_zero_index[2]]
term = 'h_' + ','.join([str(t) for t in tup])
if not is_one:
term = '- ' + term
terms.append(term)
if len(true_non_zero_indices) == 1:
if row < self.no_vars:
print str(-vector[row + self.no_vars]) + \
' <= ' + terms[0] + ' <= ' + str(vector[row])
else:
if row < no_rows - no_d_constraints:
print str(-vector[row + no_d_constraints]) + \
' <= ' + terms[1] + ' ' + terms[0] + ' <= ' + str(vector[row])
示例2: plot_3D_objects_for_2D_case
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import get_grid_indices [as 别名]
def plot_3D_objects_for_2D_case(self):
if self.n != 2:
print 'Plotting is only available for 2D domain.'
return
grid_points = Utils.get_grid_indices(self.no_points_per_axis, False)
x, y = [list(tup) for tup in zip(*grid_points)]
x = [self.grid_info[0][index] for index in x]
y = [self.grid_info[1][index] for index in y]
min_z = self.min_heights.flatten()
max_z = self.max_heights.flatten()
fig = plt.figure(figsize=(26.5, 12.12), facecolor='#34495e')
fig.canvas.set_window_title('Consistency')
ax = fig.gca(projection='3d')
ax.set_axis_bgcolor('#34495e')
ax.view_init(elev=30, azim=-45)
# Do not use Delaunay triangulation. Instead, generate the labels of the triangles in
# counter-clockwise fashion and supply there labels to the plot_trisurf function call.
x_dim = self.no_points_per_axis[0]
y_dim = self.no_points_per_axis[1]
triangles = Utils.get_triangulation(x_dim, y_dim)
ax.plot_trisurf(x, y, min_z, cmap='Blues', linewidth=0.5, antialiased=False,
triangles=triangles)
ax.plot_trisurf(x, y, max_z, cmap='Reds', linewidth=0.5, antialiased=False,
triangles=triangles)
least_surface = mpatches.Patch(color='#3498db', label='Least Surface')
greatest_surface = mpatches.Patch(color='#e74c3c', label='Greatest Surface')
legend_handles = [greatest_surface, least_surface]
if self.plot_random_heights:
ax.plot_trisurf(x, y, self.random_heights.flatten(), cmap='Greens', linewidth=0.5,
antialiased=False, triangles=triangles)
original_surface = mpatches.Patch(color='#27ae60', label='Original Surface')
legend_handles = [greatest_surface, original_surface, least_surface]
plt.legend(handles=legend_handles)
ax.set_xlabel('\n\nX axis (%d points)' % x_dim, color='white')
ax.set_ylabel('\n\nY axis (%d points)' % y_dim, color='white')
ax.set_zlabel('\n\nZ axis', color='white')
ax.set_zlim(min(min_z) - 1.0, max(max_z) + 1.0)
ax.tick_params(axis='x', colors='white', labelsize=16)
ax.tick_params(axis='y', colors='white', labelsize=16)
ax.tick_params(axis='z', colors='white', labelsize=16)
if x_dim > 10 and y_dim > 10:
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1])
plt.yticks([0, 0.2, 0.4, 0.6, 0.8, 1])
else:
plt.xticks(np.around(self.grid_info[0], 2))
plt.yticks(np.around(self.grid_info[1], 2))
plt.show()
示例3: init_random_heights
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import get_grid_indices [as 别名]
def init_random_heights(self):
if self.from_poly:
poly = self.build_random_polynomial()
grid_indices = Utils.get_grid_indices(self.no_points_per_axis, ignore_last=False)
# Parallelise the initialisation of random values derived from the polynomial.
pool = multiprocessing.Pool(multiprocessing.cpu_count())
coords = [Utils.convert_grid_index_to_coord(grid_index, self.grid_info)
for grid_index in grid_indices]
self.random_heights = np.array(pool.map(poly.eval, coords),
dtype=float).reshape(self.no_points_per_axis)
pool.close()
pool.join()
else:
self.random_heights = np.random.uniform(RANDOM_LOWER_BOUND,
RANDOM_UPPER_BOUND,
self.no_points_per_axis)
示例4: build_constraints_from_function_info
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import get_grid_indices [as 别名]
def build_constraints_from_function_info(self):
# Construct bounds of the type: c_ij- <= h_st <= c_ij+, s = i,i+1, t = j,j+1.
l_b_constraints = np.empty(self.no_points_per_axis)
u_b_constraints = np.empty(self.no_points_per_axis)
l_b_constraints.fill(np.float('-inf'))
u_b_constraints.fill(np.float('inf'))
# Derive the constraints on the heights based on the adjacent subrectangles.
indices_allowing_neighbours = Utils.get_grid_indices(self.no_points_per_axis, True)
for grid_index in indices_allowing_neighbours:
next_grid_indices = Utils.get_grid_indices_neighbours(grid_index)
l_b_function_value = self.function_info[grid_index][0]
u_b_function_value = self.function_info[grid_index][1]
for index in next_grid_indices:
l_b_constraints[index] = max(l_b_constraints[index], l_b_function_value)
u_b_constraints[index] = min(u_b_constraints[index], u_b_function_value)
return (l_b_constraints, u_b_constraints)
示例5: build_constraints_from_derivative_info
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import get_grid_indices [as 别名]
def build_constraints_from_derivative_info(self):
# Construct bounds of the type: b_ij_1- <= (h_i+1,j - h_ij) / (p_i+1 - p_i) <= b_ij_1+.
indices_allowing_neighbours = Utils.get_grid_indices(self.no_points_per_axis, True)
sub_hyper_rect_end_points = Utils.get_sub_hyper_rect_end_points(self.n)
l_b_constraints = []
u_b_constraints = []
# Along each partial derivative and for all possible triangulations within a hyper-rectangle
# ensure that we construct the tightest bounds for consecutive differences of heigths.
for ith_partial in range(self.n):
partial_l_b_constraints = np.empty(
Utils.get_dimension_for_row_vector(self.no_points_per_axis, ith_partial))
partial_u_b_constraints = np.empty(
Utils.get_dimension_for_row_vector(self.no_points_per_axis, ith_partial))
partial_l_b_constraints.fill(float('-inf'))
partial_u_b_constraints.fill(float('inf'))
for grid_index in indices_allowing_neighbours:
next_grid_indices = Utils.get_grid_indices_neighbours(grid_index)
for end_points in sub_hyper_rect_end_points[ith_partial]:
next_index = next_grid_indices[end_points[1]]
curr_index = next_grid_indices[end_points[0]]
next_coord = Utils.convert_grid_index_to_coord(next_index, self.grid_info)
curr_coord = Utils.convert_grid_index_to_coord(curr_index, self.grid_info)
grid_diff = next_coord[ith_partial] - curr_coord[ith_partial]
partial_l_b_constraints[curr_index] = max(partial_l_b_constraints[curr_index], \
grid_diff * self.derivative_info[grid_index][ith_partial][0])
partial_u_b_constraints[curr_index] = min(partial_u_b_constraints[curr_index], \
grid_diff * self.derivative_info[grid_index][ith_partial][1])
l_b_constraints.extend(partial_l_b_constraints.flatten().tolist())
u_b_constraints.extend(partial_u_b_constraints.flatten().tolist())
return (l_b_constraints, u_b_constraints)
示例6: generate_flat_info
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import get_grid_indices [as 别名]
def generate_flat_info(self, is_function_info):
# Reverse engineer the LP algorithm to construct either consistent or inconsistent input.
flat_info = []
grid_indices = Utils.get_grid_indices(self.no_points_per_axis, ignore_last=False)
# In order to generate inconsistent input, change a random partial derivative at a random
# grid point so that the derivative is always strictly greater than the gradient within the
# corresponding hyper-rectangle. This is the minimal change we can make to get inconsistent
# input.
if not self.is_cons_input and self.function_info is not None:
interior_grid_indices = Utils.get_grid_indices(self.no_points_per_axis,
ignore_last=True)
random_index_in_list = np.random.randint(0, len(interior_grid_indices))
random_interior_grid_index = interior_grid_indices[random_index_in_list]
random_partial_derivative = np.random.randint(0, self.n)
for grid_index in grid_indices:
if is_function_info:
f_interval = (0.0, 0.0)
f_value = self.random_heights[grid_index]
# Ensure that we generate wide enough intervals that contain all adjacent points.
if not Utils.is_border_index(grid_index, self.no_points_per_axis):
grid_indices_neighbours = Utils.get_grid_indices_neighbours(grid_index)
min_h, max_h = float('inf'), float('-inf')
for next_grid_index in grid_indices_neighbours:
f_value_neighbour = self.random_heights[next_grid_index]
min_h, max_h = min(min_h, f_value_neighbour), max(max_h, f_value_neighbour)
if self.is_cons_input:
# To guarantee consistency, the interval for function information at this
# particulat grid point must include at least [min_h, max_h].
# Use the epsilon to vary this interval and to allow a greater distance
# between the minimal and maximal surfaces.
f_interval = (min_h - self.eps, max_h + self.eps)
else:
# For inconsistent input, simply take the least box containing the generated
# heights.
f_interval = (min_h, max_h)
flat_info.append(f_interval)
else:
d_values = [(0.0, 0.0)] * self.n
if not Utils.is_border_index(grid_index, self.no_points_per_axis):
sub_hyper_rect_end_points = Utils.get_sub_hyper_rect_end_points(self.n)
# Along each partial derivative and for all parallel edges within the grid,
# compute the minimum and maximum gradients based on the given heights.
for ith_partial in range(self.n):
# First compute the grid difference.
next_index = Utils.get_next_grid_index(grid_index, ith_partial)
next_coord = Utils.convert_grid_index_to_coord(next_index, self.grid_info)
curr_coord = Utils.convert_grid_index_to_coord(grid_index, self.grid_info)
grid_diff = next_coord[ith_partial] - curr_coord[ith_partial]
next_grid_indices = Utils.get_grid_indices_neighbours(grid_index)
min_b, max_b = float('inf'), float('-inf')
for end_points in sub_hyper_rect_end_points[ith_partial]:
next_index = next_grid_indices[end_points[1]]
curr_index = next_grid_indices[end_points[0]]
f_value_next = self.random_heights[next_index]
f_value_curr = self.random_heights[curr_index]
gradient = (f_value_next - f_value_curr) / grid_diff
min_b, max_b = min(min_b, gradient), max(max_b, gradient)
if self.is_cons_input:
d_values[ith_partial] = (min_b - self.eps, max_b + self.eps)
else:
# Make the minimal change to guarantee inconsistent input.
if random_interior_grid_index == grid_index and \
random_partial_derivative == ith_partial:
f_lower = self.function_info[random_interior_grid_index][0]
f_upper = self.function_info[random_interior_grid_index][1]
max_slope = abs(f_upper - f_lower) / grid_diff
# Assign the inconsistent interval.
d_values[ith_partial] = (max_slope + OFFSET, max_slope + 5 * OFFSET)
else:
d_values[ith_partial] = (min_b, max_b)
flat_info.append(tuple(d_values))
return flat_info