<|start_filename|>Examples/basic_JuMP_ex2.jl<|end_filename|>
using JuMP

### Place optimization problem definition and solve in a function
function solveOptProb1(a,b)
	m = Model()
	@variable(m, 0 <= x <= 2 )
	@variable(m, 0 <= y <= 30 )

	@objective(m, Max, a*x + 3*y )
	@constraint(m, 1x + 5y <= b )
	
	status = solve(m)
	
	return getobjectivevalue(m), [getvalue(x); getvalue(y)], status
	
end

### Test function

(obj, z, status) = solveOptProb1(1,1)


### Iterate over possible values of a and b
aOpt = 0:5
bOpt = 1:3

aN = length(aOpt)
bN = length(bOpt)

objValues = zeros(aN,bN)
zValues = zeros(2,aN,bN)

for i in 1:length(aOpt)
	for j in 1:length(bOpt)
		(objValues[i,j], zValues[:,i,j], status) = solveOptProb1(aOpt[i],bOpt[j])
	end
end

println("objValues = ")
println(objValues)
println(" ")

println("zValues = ")
println(zValues)
println(" ")

<|start_filename|>Examples/basic_JuMP_ex3.jl<|end_filename|>
# Created by <NAME> (alexdowling.net) while at the University of Wisconsin-Madison

##### This example solves an optimization in parallel using pmap()
# More info: http://julia.readthedocs.org/en/latest/manual/parallel-computing/
# Use "julia -p n" to start Julia with n 

### Import optimization problem definition/solve in a function

push!(LOAD_PATH,pwd())
using DummyModule

### Test function

my = solveOptProb2((1,1))


### Iterate over possible values of a and b
aOpt = 0:5
bOpt = 1:3

aN = length(aOpt)
bN = length(bOpt)

# Assemble array of input parameters for the optimization problem
P = Array(Tuple,aN*bN)

for i in 1:length(aOpt)
	for j in 1:length(bOpt)
		P[(i-1)*bN + j] = (aOpt[i],bOpt[j])
	end
end

# Display P
print(P)

# Solve the optimization problem in parallel
# Note: results is an array of tuples, with the same dimensions as P
results = pmap(solveOptProb2, P)

for i = 1:length(results)
	DummyModule.print(results[i])
end

<|start_filename|>Examples/demo_dh.jl<|end_filename|>
using DegeneracyHunter

# Create an optimization model 'm'
include("zero_flowrates.jl")

# Load MILP solver
using Gurobi

# Search for irreducible degenerate sets
sets, dd, ds = DegeneracyHunter.degeneracyHunter(m, GurobiSolver());

println("Done.")


<|start_filename|>Examples/DummyModule.jl<|end_filename|>
module DummyModule

using JuMP

export solveOptProb2, MyResults

function solveOptProb2(p)
	a = p[1]
	b = p[2]
	m = Model()
	@variable(m, 0 <= x <= 2 )
	@variable(m, 0 <= y <= 30 )

	@objective(m, Max, a*x + 3*y )
	@constraint(m, 1x + 5y <= b )
	
	status = solve(m)
	
	return MyResults(getobjectivevalue(m), getvalue(x), getvalue(y), status)
	
end

type MyResults
	obj::Float64
	x::Float64
	y::Float64
	status::Symbol
end

function print(r::MyResults)

	println(" ")
	println("objective = ",r.obj)
	println("x = ",r.x,"\t y = ",r.y)
	println(r.status)

	return nothing

end

end

<|start_filename|>Examples/zero_flowrates.jl<|end_filename|>
using JuMP
using Ipopt

# Define sets
# Streams
S = 2:7

# Components (chemical species)
C = ["A", "B"]

# Units
U = ["U1", "U2"]

# Define stream/unit connectivity using dictionaries
inlets = Dict{String,Integer}("U1"=>2, "U2"=>5)
outletV = Dict{String,Integer}("U1"=>3, "U2"=>6)
outletL = Dict{String,Integer}("U1"=>4, "U2"=>7)

# steams
feeds = [2, 5]

# Equilibrium coefficients

K = Dict{Tuple{String, String}, Float64}(
	("U1","A")=>1.008,		("U1","B")=>0.9,
	("U2","A")=>1.099,		("U2","B")=>0.9 )	 

		
# Feed flowrate specification
# (for components, same for all feed streams)
feedflow = Dict{String, Float64}("A"=>0.55, "B"=>0.45)

# Define model
m = Model(solver=IpoptSolver())

##### General flowsheet unit model #####

# Total molar flowrate
@variable(m, f[S] >= 0)

# Component molar flowrate
@variable(m, fc[S,C] >= 0)

# Component mole fraction
@variable(m, 0 <= x[S,C] <= 1)

# Unit model
for u in U
	
	i = inlets[u]
	v = outletV[u]
	l = outletL[u]
	
	# Overall mass balance
	@constraint(m, f[i] == f[v] + f[l])

	for c in C
		# Component mass balance
		@constraint(m, fc[i,c] == fc[v,c] + fc[l,c])
	
		# vapor-liquid equilibrium
		@constraint(m, x[v, c] == K[(u,c)]*x[l,c])
	
	end
	
	# Unit summation (Rachford-Rice equation)
	@constraint(m, sum(x[v,c] - x[l,c] for c in C) == 0)
	
end

# Stream model: mole fraction specification
for s in S
	for c in C
		@constraint(m, fc[s,c] == f[s]*x[s,c])
	end
end

##### Economics and recovery/purity specifications

@variable(m, 0.6 <= purityA <= 1.0)
@variable(m, 0.551 <= recoveryA <= 1.0)

# Equipment cost
ecost = Dict{String,Float64}("U1"=>1.5, "U2"=>1.0)

# Set total feed to one
@constraint(m, sum(f[s] for s in feeds) == 1)

# Calculate purity of A
@constraint(m, purityA*sum(sum(f[s] for s in outletV[u]) for u in U) == sum(sum(fc[s,"A"] for s in outletV[u]) for u in U))

# Calculate recovery of A
@constraint(m, recoveryA == sum(sum(fc[s,"A"] for s in outletV[u]) for u in U) / feedflow["A"])

# Objective
@objective(m, Min, sum(sum(f[s] for s in inlets[u])*ecost[u] for u in U) - 100*purityA)

for s in feeds
	for c in C
		setupperbound(x[s,c], feedflow[c])
		setlowerbound(x[s,c], feedflow[c])
	end
end

##### Initialization

# Guess all molar flowrates are one
# And molar fractions match feed
for s in S
	
	setvalue(f[s], 1.0)
	
	for c in C
		setvalue(fc[s,c], feedflow[c])
		setvalue(x[s,c], feedflow[c])
	end
end

print(m)

solve(m)

<|start_filename|>Examples/basic_JuMP_ex1.jl<|end_filename|>
using JuMP

##### Simple LP
# Example taken directly from https://jump.readthedocs.org/en/latest/quickstart.html

println("***** Problem 1 *****")

m = Model()
@variable(m, 0 <= x <= 2 )
@variable(m, 0 <= y <= 30 )

@objective(m, Max, 5x + 3*y )
@constraint(m, 1x + 5y <= 3.0 )

print(m)

status = solve(m)

println("Objective value: ", getobjectivevalue(m))
println("x = ", getvalue(x))
println("y = ", getvalue(y))

##### Another simple LP with logical conditions in constraint definition
# Example from https://groups.google.com/forum/#!topic/julia-opt/pMHz-9YHN2o
# Note: This example only works with Julia-v0.4 and later

println(" ")
println("***** Problem 2 *****")

m = Model()

I = 1:5

@variable(m, 0 <= x[I] <= 10)
@variable(m, 0 <= y[I] <= 10)

# This constraint is only considered when i + j are less than or equal to 3
# Note: Only supported by Julia-v0.4 and later
@constraint(m, constr[i=I,j=I; i+j <= 3], x[i] - y[j] == 1)

@objective(m, Min, sum{x[i] + y[i], i=I})

print(m)

status = solve(m)

println(" ")
println("Objective value: ", getobjectivevalue(m))
println("x = ", getvalue(x))
println("y = ", getvalue(y))

<|start_filename|>Examples/nlp1_manual.jl<|end_filename|>
a_ = zeros(5)
y_ = 0
z_ = -9.4826529886227799e+00
r = zeros(5)

a_[1] = -9.0909090552980257e-02
a_[2] = 9.0909091401531950e-01
a_[3] = -1.0000000051491075e+01
a_[4] = 1.0000000051491075e+01
a_[5] = -1.0000000097329545e+01

r[1] = max(10 - a_[1] - a_[2] + a_[3], 0)
r[2] = max(-a_[2] + a_[2]*y_ - a_[1]*a_[4], 0)
r[3] = a_[3]*a_[2] + a_[1]*a_[4] - a_[5]
r[4] = z_ - sum(a_) + 0.001*sum(a_.^2)
r[5] = a_[3] + a_[4] - 10*y_

for i = 1:5
	println("r[",i,"] = ",r[i])
end