# 3D bin packaging using Convex.jl

**URL:** https://discourse.julialang.org/t/3d-bin-packaging-using-convex-jl/52571
**Category:** Optimization (Mathematical)
**Tags:** optimization
**Created:** [December 29, 2020, 12:36pm UTC](https://discourse.julialang.org/t/3d-bin-packaging-using-convex-jl/52571 "2020-12-29T12:36:08Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![adropintheriver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adropintheriver/32/20634_2.png) [@adropintheriver](https://discourse.julialang.org/u/adropintheriver)
#### Post date: [December 29, 2020, 12:36pm UTC](https://discourse.julialang.org/t/3d-bin-packaging-using-convex-jl/52571/1 "2020-12-29T12:36:08Z")

</div>

**Intent** :Write a convex function that takes a set of points of shape (m,n) basically convexhull points by reading a CAD model.

1. The function must return a Variable(from Convex.jl) of shape/size of the original vector (to be used in optimization later - i have to estimate the coordinates) plus a set of affine constraints to keep it convex.

Here’s the code for the function

```julia
function vrep(v)
    λ = Variable(size(v)[1])
    x = Variable(size(v))
    c = zeros(size(v)[1])
    for i in 1:size(v)[1]
         c = c + λ[i] * v[i]
    end
    add_constraint!(x,v' * x <= sum(x))
    add_constraint!(x,c <= sum(x))
    add_constraint!(λ,λ >= 0)
    add_constraint!(λ,λ <= 1)
    add_constraint!(λ, sum(λ) == 1)
    return x
end

```

1. The idea is to fit together many such vreps of different sizes/shapes in R3 in a given box without any overlaps so to simplify i start with R2 assuming the same should work in R3

```julia
va = [0 0;1 0 ;1 1 ;0 1] # a unit square 
function get_nooverlap_constraint(m, n)
    Am = ones(size(m))
    An = ones(size(n))
    return ((Am' * m) - (An' * n)) >= 1
end
a = vrep(va)
b = vrep(va)

p = minimize(norm(a + b))
# ensure none overlaps 
add_constraint!(p, get_nooverlap_constraint(a,b))

solve!(p, () -> SCS.Optimizer(verbose=false))
print(a.value, b.value)

```

1. Result : When i plot these points, i get the first two points in a single line and the rest three look ok - why is the first point estimated incorrectly? :

```julia
a : [-0.45427274477738133 -0.45418622832194044; 0.2049041263858669 0.20490966184077403; 1.0444644866772845 1.0443668979909329; 0.2049041263168114 0.20490966190984883]
b: [0.7042727350943662 0.7041862185089512; 0.045095874300268665 0.04509033885584125; -0.7944644863584401 -0.7943668978393343; 0.04509587423119381 0.0450903389248964]

```

1. a few questions :  
a. should my objective function be minkowski sum instead?  
b. What is the right way to estimate the b such that it does not overlap - please see my current implementation - **get\_nooverlap\_constraint** I could not get the A’\*n \>= b & A’\*n \<=b constraint working  
c. will this implementation work in R3 because I have more constraints the no overlap, then a few boundary conditions, preference for certain part of the available configuration space, some no-go zones etc.  
please suggest/advice
