I need to place the wells evenly (i.e. specify their number and coordinates) inside the contour
the block, observing the minimum and maximum distances between wells, the uniformity
of the placement of wells and the density of the placement of wells. The uniformity of well placement is determined through the coefficient of uniformity of placement R,
which must be minimized to increase uniformity. How can I minimize the coefficient of uniformity of placement R?
I wrote a code that iterates over the well coordinates. Maybe there are more optimal algorithms for finding the minimum?
My code.
import json
import numpy as np
from matplotlib.path import Path
def load_data(filename):
with open(filename, 'r') as file:
data = json.load(file)
return data
def calculate_area(coords):
x = [p['x'] for p in coords]
y = [p['y'] for p in coords]
return 0.5 * np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))
def get_bounding_box(coords):
x_min = min(coord['x'] for coord in coords)
x_max = max(coord['x'] for coord in coords)
y_min = min(coord['y'] for coord in coords)
y_max = max(coord['y'] for coord in coords)
return x_min, x_max, y_min, y_max
def point_in_polygon(point, polygon):
path = Path(polygon)
return path.contains_point(point)
def plan_wells(coords, target_density):
x_min, x_max, y_min, y_max = get_bounding_box(coords)
num_wells = int(calculate_area(coords) * target_density)
wells = []
k = 0
while (k==0):
while len(wells) < num_wells:
x = np.random.uniform(x_min, x_max)
y = np.random.uniform(y_min, y_max)
if point_in_polygon((x, y), [(coord['x'], coord['y']) for coord in coords]):
wells.append({'x': x, 'y': y})
for i in range(len(wells)):
nearest_distances = sorted([np.sqrt((wells[i]['x'] - w['x'])**2 + (wells[i]['y'] - w['y'])**2) for w in wells if w != wells[i]])[:3]
if (nearest_distances[0] < rms_min or nearest_distances[0] >= rms_max ):
wells = []
break
else:
k = 1
return wells
def evaluate_wells(wells):
distances = []
for i in range(len(wells)):
nearest_distances = sorted([np.sqrt((wells[i]['x'] - w['x'])**2 + (wells[i]['y'] - w['y'])**2) for w in wells if w != wells[i]])[:3]
if len(nearest_distances) > 0:
average_distance = np.mean(nearest_distances)
distances.append(average_distance)
mean_distance = np.mean(distances)
R = np.sqrt(np.mean((distances - mean_distance)**2))
return R
def minimize_R(coords, target_density, iterations):
best_wells = None
best_R = float('inf')
for _ in range(iterations):
wells = plan_wells(coords, target_density)
R = evaluate_wells(wells)
if R < best_R:
best_R = R
best_wells = wells
return best_wells, best_R
def save_solution(wells, task_number):
solution_data = {
"holeCoords": wells
}
filename = f'solution-{task_number}.json'
with open(filename, 'w', encoding='utf-8') as file:
json.dump(solution_data, file, ensure_ascii=False, indent=4)
print(f"Solution saved to {filename}")
data = load_data('/task-1.json')
rms_min = data['rmsMin']
rms_max = data['rmsMax']
print(rms_min)
area = calculate_area(data['blockContour'])
print(f": {area}")
best_wells, best_R = minimize_R(data['blockContour'], data['targetDensity'], 1)
print(f" R: {best_R}")
save_solution(best_wells, 1)
Владислав is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.