# Minimizing the distance between centers of multiple circles

**URL:** https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843
**Category:** Optimization (Mathematical)
**Tags:** question, jump, optimization
**Created:** [January 12, 2023, 3:02am UTC](https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843 "2023-01-12T03:02:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MadJoe](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@MadJoe](https://discourse.julialang.org/u/MadJoe)
#### Post date: [January 12, 2023, 3:02am UTC](https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843/1 "2023-01-12T03:02:32Z")

</div>

Hello, I am new to optimization and Julia, and I need to minimize the distance between some circles that encircle multiple points in a 2-D plane by moving the centers of the circles. We know where all of the blue spots are, and the circles are all the same size and radius. can somebody help me with this problem?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7b4b7640f6d47379ce44d224be3854ecfe688aea.png)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 12, 2023, 3:24am UTC](https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843/2 "2023-01-12T03:24:22Z")

</div>

> [@MadJoe](#):
>
> I need to minimize the distance between some circles

What does this mean? Minimize the average distance between the circle centers? Or minimize the maximum distance? Or …?

Try to write down your minimization problem as equations: minimizing a specific number (the “objective function”) subject to a set of constraints.

---

<div class="post-metadata">

### Author: ![MadJoe](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@MadJoe](https://discourse.julialang.org/u/MadJoe)
#### Post date: [January 12, 2023, 3:53am UTC](https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843/3 "2023-01-12T03:53:34Z")

</div>

Thank you for your reply, the goal is to change the center of circles to minimize the sum of the distances between all centers something like this:

model = Model()  
variable(model, x[1:n] \>= 0); # x coordinate  
variable(model, y[1:n] \>= 0); # y coordinate  
constraint(model, … )  
objective(model, Min, sum((x.-x’).^2 .+ (y.-y’).^2))

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [January 12, 2023, 4:04am UTC](https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843/4 "2023-01-12T04:04:08Z")

</div>

> [@MadJoe](#):
>
> Thank you for your reply, the goal is to change the center of circles to minimize the sum of the distances between all centers …

Isn’t the solution to that problem to set all circle centers to the same point, so that the sum of distances between each pair of centers is zero?

I don’t quite understand this problem. I don’t see what roles the circles or blue dots are playing.

---

<div class="post-metadata">

### Author: ![MadJoe](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@MadJoe](https://discourse.julialang.org/u/MadJoe)
#### Post date: [January 12, 2023, 5:13am UTC](https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843/5 "2023-01-12T05:13:48Z")

</div>

Thank you John for your reply and sorry if the question is not quite clear, the roles are as follows

The position of all blue dots cannot be changed, nor can the number of dots inside each circle, so a circle can move as long as all of its blue dots remain inside it. For example, changing the center position of the red circle towards other circles reduces the sum of distances between all circles.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/e/fe65877b27b328058198927292932c583d8a9247.png)

---

<div class="post-metadata">

### Author: ![A\_C](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@A\_C](https://discourse.julialang.org/u/A_C)
#### Post date: [January 12, 2023, 5:47am UTC](https://discourse.julialang.org/t/minimizing-the-distance-between-centers-of-multiple-circles/92843/6 "2023-01-12T05:47:14Z")

</div>

If you want a point `P1 : [x1, y1]` inside the circle C with center at `[xc, yc]` and radius `rc`. you can formulate it as a NLConstraint in JuMP.

```julia
@NLConstraint(model, (x1-xc)^2 + (y1-yc)^2 <= rc^2)

```

Add such constraints for all the points and specify the objective as sum of all pairwise distances.
