Buckets:
| # Code automatically generated from OptiMUS | |
| # Problem type: LP | |
| # Problem description | |
| ''' | |
| Mrs. Watson plans to invest at most MaximumBudget in two types of properties: | |
| condos and detached houses. The profit per dollar for condos is | |
| ProfitRateCondos, and the profit per dollar for detached houses is | |
| ProfitRateHouses. Furthermore, at least MinimumFractionCondos of the total | |
| amount invested must be allocated to condos, and an investment of at least | |
| MinimumInvestmentHouses must be made in detached houses. The objective is to | |
| maximize the total profit from these investments. | |
| ''' | |
| # Import necessary libraries | |
| import json | |
| from gurobipy import * | |
| # Create a new model | |
| model = Model() | |
| # Load data | |
| with open("parameters.json", "r") as f: | |
| data = json.load(f) | |
| # @Def: definition of a target | |
| # @Shape: shape of a target | |
| # Parameters | |
| # @Parameter MaximumBudget @Def: The total budget that can be invested @Shape: [] | |
| MaximumBudget = data['MaximumBudget'] | |
| # @Parameter ProfitRateCondos @Def: Profit per dollar invested in condos @Shape: [] | |
| ProfitRateCondos = data['ProfitRateCondos'] | |
| # @Parameter ProfitRateHouses @Def: Profit per dollar invested in detached houses @Shape: [] | |
| ProfitRateHouses = data['ProfitRateHouses'] | |
| # @Parameter MinimumFractionCondos @Def: Minimum fraction of total budget that must be invested in condos @Shape: [] | |
| MinimumFractionCondos = data['MinimumFractionCondos'] | |
| # @Parameter MinimumInvestmentHouses @Def: Minimum amount to be invested in detached houses @Shape: [] | |
| MinimumInvestmentHouses = data['MinimumInvestmentHouses'] | |
| # Variables | |
| # @Variable AmountCondos @Def: The amount invested in condos @Shape: [] | |
| AmountCondos = model.addVar(vtype=GRB.CONTINUOUS, lb=0.0, name="AmountCondos") | |
| # @Variable AmountHouses @Def: The amount invested in detached houses @Shape: [] | |
| AmountHouses = model.addVar( | |
| lb=MinimumInvestmentHouses, | |
| ub=MaximumBudget, | |
| vtype=GRB.CONTINUOUS, | |
| name="AmountHouses" | |
| ) | |
| # @Variable TotalInvestment @Def: The total amount invested in condos and detached houses @Shape: [] | |
| TotalInvestment = model.addVar(lb=0.0, ub=MaximumBudget, vtype=GRB.CONTINUOUS, name='TotalInvestment') | |
| # Constraints | |
| # @Constraint Constr_1 @Def: The total amount invested in condos and detached houses cannot exceed MaximumBudget | |
| model.addConstr(AmountCondos + AmountHouses <= MaximumBudget, name='max_investment_constraint') | |
| # @Constraint Constr_2 @Def: At least MinimumFractionCondos fraction of the total investment must be allocated to condos | |
| model.addConstr( | |
| AmountCondos + AmountHouses == TotalInvestment, | |
| name='cons_total_investment' | |
| ) | |
| model.addConstr( | |
| AmountCondos >= MinimumFractionCondos * TotalInvestment, | |
| name='cons_min_fraction_condos' | |
| ) | |
| # @Constraint Constr_3 @Def: At least MinimumInvestmentHouses must be invested in detached houses | |
| model.addConstr( | |
| AmountHouses >= MinimumInvestmentHouses, | |
| name="MinInvestmentHouses" | |
| ) | |
| # Objective | |
| # @Objective Objective @Def: Maximize the total profit, which is calculated as (ProfitRateCondos × amount invested in condos) + (ProfitRateHouses × amount invested in detached houses) | |
| model.setObjective( | |
| ProfitRateCondos * AmountCondos + ProfitRateHouses * AmountHouses, | |
| GRB.MAXIMIZE | |
| ) | |
| # Solve | |
| model.optimize() | |
| # Extract solution | |
| solution = {} | |
| variables = {} | |
| objective = [] | |
| variables['AmountCondos'] = AmountCondos.x | |
| variables['AmountHouses'] = AmountHouses.x | |
| variables['TotalInvestment'] = TotalInvestment.x | |
| solution['variables'] = variables | |
| solution['objective'] = model.objVal | |
| with open('solution.json', 'w') as f: | |
| json.dump(solution, f, indent=4) | |
Xet Storage Details
- Size:
- 3.61 kB
- Xet hash:
- aa0505347b6a8e4b76ff92908184228bdfbb04f111ce6260e12209027604f96c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.