{ "cells": [ { "cell_type": "markdown", "id": "f99a4975", "metadata": {}, "source": [ "# Specifying Objectives" ] }, { "cell_type": "markdown", "id": "c6f47e3b", "metadata": {}, "source": [ "Below, we show how to specify an objective when dealing with a COP (Constraint Optimization Problem) instance. " ] }, { "cell_type": "markdown", "id": "9ab301e7", "metadata": {}, "source": [ "**Remark.** In PyCSP$^3$, which is currently targeted to XCSP$^3$-core, we currently only consider mono-objective optimization. Multi-objective optimization will be addressed in the future." ] }, { "cell_type": "markdown", "id": "40a6abde", "metadata": {}, "source": [ "To see how it works, we need first to import the library PyCSP$^3$:" ] }, { "cell_type": "code", "execution_count": 1, "id": "c1b9eff6", "metadata": {}, "outputs": [], "source": [ "from pycsp3 import *" ] }, { "cell_type": "markdown", "id": "c361b8ea", "metadata": {}, "source": [ "For specifying an objective to optimize, you must call one of the two functions:\n", "- *minimize()*\n", "- *maximize()*" ] }, { "cell_type": "markdown", "id": "f6a8b808", "metadata": {}, "source": [ "We can pass a parameter to either functions, which can be:\n", "- a variable, as in: \n", "```\n", "minimize(v)\n", "```\n", "- an expression, as in:\n", "```\n", "minimize(v + w * w)\n", "```\n", "- a sum, as in:\n", "```\n", "minimize(Sum(x))\n", "```\n", "- a dot product, as in:\n", "```\n", "minimize([u,v,w] * [3, 2, 5])\n", "```\n", "- a generator, as in:\n", "```\n", "minimize(Sum((x[i] > 1) * c[i] for i in range(n))) \n", "```\n", "- a minimum, as in:\n", "```\n", "minimize(Minimum(x))\n", "```\n", "- a maximum, as in:\n", "```\n", "minimize(Maximum(x))\n", "```\n", "- a number of distinct values, as in:\n", "```\n", "minimize(NValues(x))\n", "```\n", "- ..." ] }, { "cell_type": "raw", "id": "0c0c1d6c", "metadata": {}, "source": [ "For our illustration, let us declare an array x of 6 variables." ] }, { "cell_type": "code", "execution_count": 2, "id": "bcf8a597", "metadata": {}, "outputs": [], "source": [ "x = VarArray(size=6, dom=range(10))" ] }, { "cell_type": "markdown", "id": "1614c37e", "metadata": {}, "source": [ "We can decide to minimize the sum of values assigned to the variables of x. We just write:" ] }, { "cell_type": "code", "execution_count": 3, "id": "91ed5c76", "metadata": {}, "outputs": [], "source": [ "minimize(\n", " Sum(x)\n", ");" ] }, { "cell_type": "markdown", "id": "c8adb151", "metadata": {}, "source": [ "To control things, we can display the internal representation of the objective as follows:" ] }, { "cell_type": "code", "execution_count": 4, "id": "6f0d4cff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "minimize(sum(list:[x[0], x[1], x[2], x[3], x[4], x[5]]))\n" ] } ], "source": [ "print(objective())" ] }, { "cell_type": "markdown", "id": "3a68bf09", "metadata": {}, "source": [ "If ever we decide to maximize the number of distinct values assigned to the variables of x, because any new call to *minimize()* or *maximize()* overwrites the previous objective (if one was posted), we can write:" ] }, { "cell_type": "code", "execution_count": 5, "id": "c742abab", "metadata": {}, "outputs": [], "source": [ "maximize(\n", " NValues(x)\n", ");" ] }, { "cell_type": "markdown", "id": "19c17f6a", "metadata": {}, "source": [ "We can control that we have a new objective." ] }, { "cell_type": "code", "execution_count": 6, "id": "5dc43b57", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "maximize(nValues(list:[x[0], x[1], x[2], x[3], x[4], x[5]]))\n" ] } ], "source": [ "print(objective())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.12" } }, "nbformat": 4, "nbformat_minor": 5 }