{ "cells": [ { "cell_type": "markdown", "id": "google", "metadata": {}, "source": [ "##### Copyright 2025 Google LLC." ] }, { "cell_type": "markdown", "id": "apache", "metadata": {}, "source": [ "Licensed under the Apache License, Version 2.0 (the \"License\");\n", "you may not use this file except in compliance with the License.\n", "You may obtain a copy of the License at\n", "\n", " http://www.apache.org/licenses/LICENSE-2.0\n", "\n", "Unless required by applicable law or agreed to in writing, software\n", "distributed under the License is distributed on an \"AS IS\" BASIS,\n", "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "See the License for the specific language governing permissions and\n", "limitations under the License.\n" ] }, { "cell_type": "markdown", "id": "basename", "metadata": {}, "source": [ "# binpacking_problem_sat" ] }, { "cell_type": "markdown", "id": "link", "metadata": {}, "source": [ "\n", "\n", "\n", "
\n", "Run in Google Colab\n", "\n", "View source on GitHub\n", "
" ] }, { "cell_type": "markdown", "id": "doc", "metadata": {}, "source": [ "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab." ] }, { "cell_type": "code", "execution_count": null, "id": "install", "metadata": {}, "outputs": [], "source": [ "%pip install ortools" ] }, { "cell_type": "markdown", "id": "description", "metadata": {}, "source": [ "\n", "Solves a binpacking problem using the CP-SAT solver.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "code", "metadata": {}, "outputs": [], "source": [ "from ortools.sat.python import cp_model\n", "\n", "\n", "def binpacking_problem_sat():\n", " \"\"\"Solves a bin-packing problem using the CP-SAT solver.\"\"\"\n", " # Data.\n", " bin_capacity = 100\n", " slack_capacity = 20\n", " num_bins = 5\n", " all_bins = range(num_bins)\n", "\n", " items = [(20, 6), (15, 6), (30, 4), (45, 3)]\n", " num_items = len(items)\n", " all_items = range(num_items)\n", "\n", " # Model.\n", " model = cp_model.CpModel()\n", "\n", " # Main variables.\n", " x = {}\n", " for i in all_items:\n", " num_copies = items[i][1]\n", " for b in all_bins:\n", " x[(i, b)] = model.new_int_var(0, num_copies, f\"x[{i},{b}]\")\n", "\n", " # Load variables.\n", " load = [model.new_int_var(0, bin_capacity, f\"load[{b}]\") for b in all_bins]\n", "\n", " # Slack variables.\n", " slacks = [model.new_bool_var(f\"slack[{b}]\") for b in all_bins]\n", "\n", " # Links load and x.\n", " for b in all_bins:\n", " model.add(load[b] == sum(x[(i, b)] * items[i][0] for i in all_items))\n", "\n", " # Place all items.\n", " for i in all_items:\n", " model.add(sum(x[(i, b)] for b in all_bins) == items[i][1])\n", "\n", " # Links load and slack through an equivalence relation.\n", " safe_capacity = bin_capacity - slack_capacity\n", " for b in all_bins:\n", " # slack[b] => load[b] <= safe_capacity.\n", " model.add(load[b] <= safe_capacity).only_enforce_if(slacks[b])\n", " # not(slack[b]) => load[b] > safe_capacity.\n", " model.add(load[b] > safe_capacity).only_enforce_if(~slacks[b])\n", "\n", " # Maximize sum of slacks.\n", " model.maximize(sum(slacks))\n", "\n", " # Solves and prints out the solution.\n", " solver = cp_model.CpSolver()\n", " status = solver.solve(model)\n", " print(f\"solve status: {solver.status_name(status)}\")\n", " if status == cp_model.OPTIMAL:\n", " print(f\"Optimal objective value: {solver.objective_value}\")\n", " print(\"Statistics\")\n", " print(f\" - conflicts : {solver.num_conflicts}\")\n", " print(f\" - branches : {solver.num_branches}\")\n", " print(f\" - wall time : {solver.wall_time}s\")\n", "\n", "\n", "binpacking_problem_sat()\n", "\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }