In [2]:
x = 10


Out[2]:
10

In [4]:
x + 1


Out[4]:
11

In [5]:
else = false


syntax: unexpected "else"
  • Names of variables are in lower case.
  • Word separation can be indicated by underscores ('_'), but use of underscores is discouraged unless the name would be hard to read otherwise.
  • Names of Types and Modules begin with a capital letter and word separation is shown with upper camel case instead of underscores.
  • Names of functions and macros are in lower case, without underscores.
  • Functions that write to their arguments have names that end in !. These are sometimes called "mutating" or "in-place" functions because they are intended to produce changes in their arguments after the function is called, not just return a value.

In [6]:
function f(x, y)
    x + y
end


Out[6]:
f (generic function with 1 method)

In [8]:
f(1,2)


Out[8]:
3

In [9]:
(x,y) = x + y


Out[9]:
∑ (generic function with 1 method)

In [10]:
(2, 3)


Out[10]:
5

In [11]:
function g(x,y)
    return x * y
    x + y
end


Out[11]:
g (generic function with 1 method)

In [12]:
f(x,y) = x + y


Out[12]:
f (generic function with 1 method)

In [13]:
f(2,3)


Out[13]:
5

In [14]:
g(2,3)


Out[14]:
6

In [15]:
1 + 2 + 3


Out[15]:
6

In [16]:
+(1,2,3)


Out[16]:
6

In [17]:
x -> x^2 + 2x - 1


Out[17]:
#3 (generic function with 1 method)

In [18]:
function (x)
           x^2 + 2x - 1
       end


Out[18]:
#5 (generic function with 1 method)

In [19]:
P = download("https://raw.githubusercontent.com/nassarhuda/easy_data/master/programming_languages.csv","programminglanguages.csv")


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   876  100   876    0     0   1386      0 --:--:-- --:--:-- --:--:--  1388
Out[19]:
"programminglanguages.csv"

In [20]:
;ls


julia-with-datascience.ipynb
optimization_example.jl
programminglanguages.csv

In [28]:
using DelimitedFiles
P,H = readdlm("programminglanguages.csv",header=true)


Out[28]:
(Any["1951,Regional" "Assembly" "Language"; "1952,Autocode" "" ""; … ; "2012,Julia" "" ""; "2014,Swift" "" ""], AbstractString["year,language" "" ""])

In [38]:
using Pkg
Pkg.add("Clp")
Pkg.add("Cbc")
Pkg.add("GLPK")


  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
[1mFetching: [========================================>]  100.0 %.0 % Resolving package versions...
 Installed Clp ────────── v0.6.2
 Installed MathProgBase ─ v0.7.7
  Updating `~/.julia/environments/v1.1/Project.toml`
  [e2554f3b] + Clp v0.6.2
  Updating `~/.julia/environments/v1.1/Manifest.toml`
  [e2554f3b] + Clp v0.6.2
  [fdba3010] + MathProgBase v0.7.7
  Building Clp → `~/.julia/packages/Clp/IBQzB/deps/build.log`
 Resolving package versions...
 Installed Cbc ─ v0.6.1
  Updating `~/.julia/environments/v1.1/Project.toml`
  [9961bab8] + Cbc v0.6.1
  Updating `~/.julia/environments/v1.1/Manifest.toml`
  [9961bab8] + Cbc v0.6.1
  Building Cbc → `~/.julia/packages/Cbc/WIAm0/deps/build.log`
 Resolving package versions...
  Updating `~/.julia/environments/v1.1/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v1.1/Manifest.toml`
 [no changes]

In [40]:
using JuMP, GLPK

# 모델 생성
m = Model(with_optimizer(GLPK.Optimizer))

# Variable 선언
@variable(m, 0<= x1 <=10)
@variable(m, x2 >=0)
@variable(m, x3 >=0)

# 목적 함수 
@objective(m, Max, x1 + 2x2 + 5x3)

# 제약 조건 설정
@constraint(m, constraint1, -x1 +  x2 + 3x3 <= -5)
@constraint(m, constraint2,  x1 + 3x2 - 7x3 <= 10)

# Optimization Model 출력
print(m)


Max x1 + 2 x2 + 5 x3
Subject to
 x1 ≥ 0.0
 x2 ≥ 0.0
 x3 ≥ 0.0
 x1 ≤ 10.0
 -x1 + x2 + 3 x3 ≤ -5.0
 x1 + 3 x2 - 7 x3 ≤ 10.0

In [41]:
# 최적화 문제 풀기
JuMP.optimize!(m)

# Optimal Solution Print
println("Optimal Solutions:")
println("x1 = ", JuMP.value(x1))
println("x2 = ", JuMP.value(x2))
println("x3 = ", JuMP.value(x3))

# Optimal dual variables Print
println("Dual Variables:")
println("dual1 = ", JuMP.shadow_price(constraint1))
println("dual2 = ", JuMP.shadow_price(constraint2))


Optimal Solutions:
x1 = 10.0
x2 = 2.1875
x3 = 0.9375
Dual Variables:
dual1 = 1.8125
dual2 = 0.06249999999999998

LP


In [42]:
@variable(m, x[1:3] >= 0)


Out[42]:
3-element Array{VariableRef,1}:
 x[1]
 x[2]
 x[3]

In [43]:
c = [1; 2; 5]
@objective(m, Max, sum( c[i]*x[i] for i in 1:3))


Out[43]:
$$ x_{1} + 2 x_{2} + 5 x_{3} $$

In [45]:
A = [-1  1  3;
      1  3 -7]
b = [-5; 10]
@constraint(m, constraint3, sum( A[1,i]*x[i] for i in 1:3) <= b[1] )
@constraint(m, constraint4, sum( A[2,i]*x[i] for i in 1:3) <= b[2] )


Out[45]:
constraint4 : $ x_{1} + 3 x_{2} - 7 x_{3} \leq 10.0 $

In [46]:
using Pkg
Pkg.add("PyPlot")


 Resolving package versions...
 Installed Reexport ────────── v0.2.0
 Installed FixedPointNumbers ─ v0.6.1
 Installed PyPlot ──────────── v2.8.1
 Installed Tokenize ────────── v0.5.4
 Installed PyCall ──────────── v1.91.2
 Installed MacroTools ──────── v0.5.1
 Installed CSTParser ───────── v0.6.0
 Installed LaTeXStrings ────── v1.0.3
 Installed ColorTypes ──────── v0.8.0
 Installed Colors ──────────── v0.9.5
  Updating `~/.julia/environments/v1.1/Project.toml`
  [d330b81b] + PyPlot v2.8.1
  Updating `~/.julia/environments/v1.1/Manifest.toml`
  [00ebfdb7] + CSTParser v0.6.0
  [3da002f7] + ColorTypes v0.8.0
  [5ae59095] + Colors v0.9.5
  [53c48c17] + FixedPointNumbers v0.6.1
  [b964fa9f] + LaTeXStrings v1.0.3
  [1914dd2f] + MacroTools v0.5.1
  [438e738f] + PyCall v1.91.2
  [d330b81b] + PyPlot v2.8.1
  [189a3867] + Reexport v0.2.0
  [0796e94c] + Tokenize v0.5.4
  Building PyCall → `~/.julia/packages/PyCall/ttONZ/deps/build.log`

In [48]:
using PyPlot

# Preparing a figure object
fig = figure()

# Data
x = range(0, stop=2*pi, length=1000)
y = sin.(3*x)

# Plotting with linewidth and linestyle specified
plot(x, y, color="blue", linewidth=2.0, linestyle="--")

# Labeling the axes
xlabel(L"value of $x$")
ylabel(L"\sin(3x)")

# Title
title("Test plotting")

# Save the figure as PNG and PDF
savefig("plot1.png")
savefig("plot1.pdf")

# Close the figure object
close(fig)

In [ ]: