# Cost Vector Formulation-Shipment Cost Minimization

**URL:** <https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 2, 2018, 10:09am UTC](https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085 "2018-11-02T10:09:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas9087](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vikas9087/32/9002_2.png) [@vikas9087](https://discourse.julialang.org/u/vikas9087)\
**Post date:** [November 2, 2018, 10:09am UTC](https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085/1 "2018-11-02T10:09:19Z")

</div>

Hi All,

I am trying to solve shipment cost minimization. I am facing problem for creating Cost vector. Need your help to create Cost vector from a CSV file. Problem is as below

# of suppliers 100 (let us denote them as “i”)

# of plants 250 (let us denote then as “j”)

I need to make cost vector Cij for unit cost of shipment from “i” supplier to “j” plant.

The data is given in a row form in a CSV file. For your reference I am including the sample file.

Much Thanks.  
VG

 ![Sample%20Data](https://global.discourse-cdn.com/julialang/original/3X/c/0/c0b0039e0dc28fa59ffa20a484f51c0309dad4be.jpeg)

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 2, 2018, 10:45am UTC](https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085/2 "2018-11-02T10:45:07Z")

</div>

Something like this, maybe?

```julia
using CSVFiles, DataFrames

df = DataFrame(load("input.csv"))
nsups = length(unique(df[1:end, :Supplier]))
nplants = length(unique(df[1:end, :Plants]))

costmatrix = permutedims(reshape(df[1:end,:Cost], nplants, nsups), (2,1))

```

Note that I renamed the header of your data’s third column to just “Cost”.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 2, 2018, 10:59am UTC](https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085/3 "2018-11-02T10:59:00Z")

</div>

It looks like it will bug out though if not all suppliers have all plants

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 2, 2018, 11:06am UTC](https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085/4 "2018-11-02T11:06:27Z")

</div>

That’s correct. In the general case, you could initialize the cost matrix with zeros and fill it with a loop.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 2, 2018, 11:24am UTC](https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085/5 "2018-11-02T11:24:44Z")

</div>

Zeros may also be a bit of a hassle amongst cost values, as you’d probably want to find minimums and you might miss the freebies

`costmat = Array{Union{Float64, Symbol}}(undef, nplants, nsups); costmat .= :Soldout`

and maybe

```julia
function lookup_tab(dcol)
  xkeys = unique(dcol); ##df[1:end, :Supplier]
  xtab = Dict();
  [xtab[xkey] = xval for (xval, xkey) in enumerate(xkeys)];
  return xtab;
  end;
```

---

<div class="post-metadata">

**Author:** ![vikas9087](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vikas9087/32/9002_2.png) [@vikas9087](https://discourse.julialang.org/u/vikas9087)\
**Post date:** [November 15, 2018, 4:59am UTC](https://discourse.julialang.org/t/cost-vector-formulation-shipment-cost-minimization/17085/6 "2018-11-15T04:59:43Z")

</div>

Thanks a lot. It worked.

But I couldn’t understand if not all suppliers have plants then how to resolve that bug? (I am a beginner at this so has very limited exposure to Julia as well as programming)
