push!(Base.LOAD_PATH,"D:\\MOI") # Load JuMP using JuMP using MathOptInterface # Load solver package using GLPK # shortcuts const MOI = MathOptInterface const MOIU = MathOptInterface.Utilities nrm = Model(optimizer = GLPK.GLPKOptimizerLP()) @variables(nrm, begin 0 <= BOStoMDW_R <= 80 0 <= BOStoMDW_D <= 120 0 <= BOStoSFO_R <= 75 0 <= BOStoSFO_D <= 100 0 <= MDWtoSFO_R <= 60 0 <= MDWtoSFO_D <= 110 end) nrm @objective(nrm, Max, 428*BOStoMDW_R + 190*BOStoMDW_D + 642*BOStoSFO_R + 224*BOStoSFO_D + 512*MDWtoSFO_R + 190*MDWtoSFO_D) nrm @constraint(nrm, BOStoMDW_R + BOStoMDW_D + BOStoSFO_R + BOStoSFO_D <= 166) @constraint(nrm, MDWtoSFO_R + MDWtoSFO_D + BOStoSFO_R + BOStoSFO_D <= 166) nrm # solve problem JuMP.optimize(nrm) @show JuMP.hasresultvalues(nrm) @show JuMP.terminationstatus(nrm) == MOI.Success @show JuMP.primalstatus(nrm) == MOI.FeasiblePoint JuMP.resultvalue(BOStoMDW_R) JuMP.resultvalue(BOStoMDW_D) JuMP.getobjectivevalue(nrm) # Set the random seed to ensure we always # get the same stream of 'random' numbers srand(1988) # Lets create a vector of symbols, one for each airport airports = [:BOS, :MDW, :SFO, :YYZ] num_airport = length(airports) # We'll also create a vector of fare classes classes = [:REG, :DIS] # All the demand and price data for each triple of # (origin, destination, class) will be stored in # 'dictionaries', also known as 'maps'. demand = Dict() prices = Dict() # Generate a demand and price for each pair of airports # To keep the code simple we will generate info for # nonsense flights like BOS-BOS and SFO-SFO, but they # won't appear in our final model. for origin in airports, dest in airports # Generate demand: # - Regular demand is Uniform(50,90) # - Discount demand is Uniform(100,130) demand[(origin,dest,:REG)] = rand(50:90) demand[(origin,dest,:DIS)] = rand(100:130) # Generate prices: # - Regular price is Uniform(400,700) # - Discount price is Uniform(150,300) prices[(origin,dest,:REG)] = rand(400:700) prices[(origin,dest,:DIS)] = rand(150:300) end # Finally set all places to have the same capacity plane_cap = rand(150:200) # Lets look at a sample demand at random @show demand[(:BOS,:YYZ,:REG)] nrm2 = Model(optimizer = GLPK.GLPKOptimizerLP()) # Create a variable indexed by 3 things: # * Origin # * Destination # * Class # And set the upper bound for each variable differently. # The origins and destinations should be indexed across # the vector of airports, and the class should be indexed # across the vector of classes. # We'll draw the upper bounds from the demand dictionary. @variable(nrm2, 0 <= x[o=airports, d=airports, c=classes] <= demand[(o,d,c)]) # Note that we don't have to split it up across multiple lines, # its personal preference! # The objective is just like before, except now we can use # the sum() functionality provided by JuMP to sum over all # combinations of origin/destination/class, and provide a # filter to exclude all cases where # the origin is equal to the destination @objective(nrm2, Max, sum(prices[(o,d,c)]*x[o,d,c] for o in airports, d in airports, c in classes if o != d)) # Next we'll add constraints on flights from the hub # to any of the non-hub airports. for d in airports if d == :MDW continue end println("Adding constraint for hub (MDW) to $d") @constraint(nrm2, sum(x[o,d,c] for o in airports, c in classes if o!=d) <= plane_cap) end nrm2 # Now flights into the hub for o in airports if o == :MDW continue end println("Adding constraint for $o to hub (MDW)") @constraint(nrm2, sum(x[o,d,c] for d in airports, c in classes if o!=d) <= plane_cap) end nrm2 # solve problem JuMP.optimize(nrm2) JuMP.resultvalue.(x)