Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions src/objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ function test_and_reshape_bounds(bounds, nx,ns, variable)
end
end


function iswithinbounds(x, bounds::Array)
test = true
for (i, xi) in enumerate(x)
test = test&(xi <= bounds[i][2])&(xi >= bounds[i][1])
end
test
end

function max_bounds(bounds::Array)
warn("Varying bounds badly not in sdp, define in constraint function")
m_bounds = ones(size(bounds)[1],2)
for j in 1:size(bounds)[2]
for i in 1:size(bounds)[1]
m_bounds[i,1] = min(m_bounds[i,1], bounds[i,j][1])
m_bounds[i,2] = max(m_bounds[i,2], bounds[i,j][2])
end
end
[(m_bounds[i,1], m_bounds[i,2]) for i in 1:size(bounds)[1]]
end

type StochDynProgModel <: SPModel
# problem dimension
stageNumber::Int64
Expand All @@ -126,11 +147,11 @@ type StochDynProgModel <: SPModel
dimNoises::Int64

# Bounds of states and controls:
xlim::Array{Tuple{Float64,Float64},2}
ulim::Array{Tuple{Float64,Float64},2}
xlim::Array
ulim::Array


initialState::Array{Float64, 1}
initialState::Array

costFunctions::Function
finalCostFunction::Function
Expand Down Expand Up @@ -158,17 +179,33 @@ type StochDynProgModel <: SPModel
model.noises)
end

function StochDynProgModel(TF, x_bounds, u_bounds, x0, costFunctions,
function StochDynProgModel(TF::Int, x_bounds, u_bounds, x0, costFunctions,
finalCostFunction, dynamic, constraints, aleas, search_space_builder = Nullable{Function}())
dimState = length(x0)
dimControls = size(u_bounds)[1]
u_bounds = test_and_reshape_bounds(u_bounds, dimControls,TF, "Controls")
x_bounds = test_and_reshape_bounds(x_bounds, dimState,TF, "State")
u_bounds1 = ndims(u_bounds) == 1? u_bounds : max_bounds(u_bounds)
x_bounds1 = ndims(x_bounds) == 1? x_bounds : max_bounds(x_bounds)

# cons_fun_x(t,x,u,w) = true
# cons_fun_u(t,x,u,w) = true

# if ndims(x_bounds) > 1
# cons_fun_x(t,x,u,w) = iswithinbounds(x, x_bounds[:,t])
# end

# println(cons_fun_x(2,[1. 1.],[1.,1.],[1.,1.]))
# if ndims(u_bounds) > 1
# cons_fun_u(t,x,u,w) = iswithinbounds(u, u_bounds[:,t])
# end

# cons_fun(t,x,u,w) = return constraints(t,x,u,w)&&cons_fun_x(t,x,u,w)&&cons_fun_u(t,x,u,w)

return new(TF, dimControls, dimState, length(aleas[1].support[:, 1]),
x_bounds, u_bounds, x0, costFunctions, finalCostFunction, dynamic,
x_bounds1, u_bounds1, x0, costFunctions, finalCostFunction, dynamic,
constraints, aleas, search_space_builder)
end


end


Expand Down
7 changes: 3 additions & 4 deletions src/sdp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ function build_sdpmodel_from_spmodel(model::SPModel)
end

if isa(model,LinearSPModel)
function cons_fun(t,x,u,w)
return true
end

cons_fun(t,x,u,w) = true

if in(:finalCostFunction,fieldnames(model))
SDPmodel = StochDynProgModel(model, model.finalCostFunction, cons_fun)
else
Expand Down Expand Up @@ -279,7 +279,6 @@ function get_bellman_value(model::SPModel, param::SDPparameters,
V::Union{SharedArray, Array})
ind_x0 = SdpLoops.real_index_from_variable(model.initialState, model.xlim, param.stateSteps)
Vi = value_function_interpolation(model.dimStates, V, 1)
println(ind_x0)
return Vi[ind_x0...,1]
end

Expand Down