HLPL / VerTeX Modelling Language Documentation

← examples

# Facility Location Problem
# Selects which facilities to open and assigns customers to facilities
# to minimize total cost while respecting capacity constraints

model facility_location
set L = {1, 2, 3, 4, 5}                                          # Locations (customers)
set F = {1, 2, 3}                                                # Facilities (potential sites)

const c = {{4, 2, 9},
           {1, 3, 3},
           {7, 4, 5},
           {3, 2, 1},
           {3, 1, 7}}, forall i in L, forall j in F             # Cost to serve customer i from facility j

const f = {100, 200, 300}, forall j in F                        # Fixed cost to open facility j
const d = {5, 11, 9, 7, 18}, forall i in L                      # Demand of customer i
const k = {20, 15, 25}, forall j in F                           # Capacity of facility j

bin x, forall i in L, forall j in F                             # 1 if customer i served by facility j
bin y, forall j in F                                            # 1 if facility j is opened

# Objective: Minimize total cost (assignment + fixed costs)
min sum_{i in L}{sum_{j in F}{c_{i,j} * x_{i,j}}} + sum_{j in F}{f_{j} * y_{j}}

# Each customer must be assigned to exactly one facility
constr sum_{j in F}{x_{i,j}} = 1, forall i in L

# Can only assign customer to an open facility
constr x_{i,j} <= y_{j}, forall i in L, forall j in F

# Facility capacity constraint
constr sum_{i in L}{d_{i} * x_{i,j}} <= k_{j} * y_{j}, forall j in F
end

import elytica
import json

def main():
    elytica.init_model("facility_location")
    elytica.run_model("facility_location")

    # Get sets from model
    L = elytica.get_model_set("facility_location", "L")
    F = elytica.get_model_set("facility_location", "F")

    # Collect solution
    print("\n" + "="*60)
    print("FACILITY LOCATION SOLUTION")
    print("="*60)

    opened_facilities = []
    assignments = {}

    # Check which facilities are opened
    for j in F:
        y_val = elytica.get_variable_value("facility_location", f"y{j}")
        if y_val > 0.5:
            opened_facilities.append(j)
            assignments[j] = []

    print(f"\nOpened Facilities: {opened_facilities}")
    print("\n" + "-"*60)
    print("Customer Assignments:")
    print("-"*60)

    # Get customer assignments
    for j in opened_facilities:
        print(f"\nFacility {j}:")
        for i in L:
            x_val = elytica.get_variable_value("facility_location", f"x{i},{j}")
            if x_val > 0.5:
                assignments[j].append(i)
                print(f"  Customer {i}")

    # Get objective value
    obj_value = elytica.get_best_primal_bound("facility_location")
    print("\n" + "-"*60)
    print(f"Total Cost: {obj_value:.2f}")
    print("="*60)

    # Prepare results for output
    results = {
        "objective_value": obj_value,
        "opened_facilities": opened_facilities,
        "assignments": assignments
    }

    elytica.write_results(json.dumps(results, indent=2))

    return 0

Read this through MCP: read with path: "examples/facility-location", or resource hlpl://examples/facility-location.