Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Valentin Platzgummer
qgroundcontrol
Commits
05f67e26
Commit
05f67e26
authored
Oct 01, 2020
by
Valentin Platzgummer
Browse files
Merge branch 'master' into dev1
parents
c10a8d6e
9318774f
Changes
1000
Hide whitespace changes
Inline
Side-by-side
Too many changes to show.
To preserve performance only
20 of 1000+
files are displayed.
Plain diff
Email patch
libs/or-tools-src-ubuntu/LICENSE-2.0.txt
0 → 100644
View file @
05f67e26
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2010 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
libs/or-tools-src-ubuntu/README.md
0 → 100644
View file @
05f67e26
# Introduction
This file describes how to install the OR-Tools C++, Java and .Net binary archive.
OR-Tools is located at https://developers.google.com/optimization
These modules have been tested under:
-
Ubuntu 14.04, 16.04, 17.10 and 18.04 (64-bit).
-
macOS 10.13 High Sierra with Xcode 9.4 (64 bit).
-
Microsoft Windows with Visual Studio 2015 and 2017 (64-bit)
Upon decompressing the archive, you will get the following structure:
```
or-tools/
LICENSE-2.0.txt <- Apache 2.0 License
README.md <- This file
Makefile <- Main Makefile for C++,Java and .Net
examples/ <- C++, Java and .Net examples
include/ <- all include files
objs/ <- directory containing C++ compiled object files (*.o)
classes/ <- directory containing Java class files.
packages/ <- directory containing .Net nuget packages.
lib/ <- directory containing libraries and jar files.
bin/ <- directory containing executable files
```
# C++
Running the examples will involve compiling them, then running them.
We have provided a makefile target to help you.
Use Makefile:
```
shell
make run
SOURCE
=
examples/cpp/golomb.cc
```
**OR**
this is equivalent to compiling and running
`examples/cpp/golomb.cc`
.
-
on Unix:
```
shell
make bin/golomb
./bin/golomb
```
-
on Windows:
```
shell
make bin
\\
golomb.exe
bin
\\
golomb.exe
```
# Java
Running the examples will involve compiling them, then running them.
We have provided a makefile target to help you. You need to have the
java and javac tools accessible from the command line.
Use Makefile:
```
shell
make run
SOURCE
=
examples/java/RabbitsPheasants.java
```
**OR**
this is equivalent to compiling and running
`examples/java/RabbitsPheasants.java`
.
-
on Unix:
```
shell
javac
-d
classes/RabbitsPheasants
-cp
lib/com.google.ortools.jar:lib/protobuf.jar examples/java/RabbitsPheasants.java
jar cvf lib/RabbitsPheasants.jar
-C
classes/RabbitsPheasants
.
java
-Djava
.library.path
=
lib
-cp
lib/RabbitsPheasants.jar:lib/com.google.ortools.jar:lib/protobuf.jar RabbitsPheasants
```
-
on Windows:
```
shell
javac
-d
class/RabbitsPheasants
-cp
lib/com.google.ortools.jar
;
lib/protobuf.jar examples/java/RabbitsPheasants.java
jar cvf lib/RabbitsPheasants.jar
-C
classes/RabbitsPheasants
.
java
-Djava
.library.path
=
lib
-cp
lib/RabbitPheasants.jar
;
lib/com.google.ortools.jar
;
lib/protobuf.jar RabbitsPheasants
```
# .Net
Running the examples will involve compiling them, then running them.
We have provided a makefile target to help you. You need to have the
dotnet/cli tools accessible from the command line.
Use Makefile:
```
shell
make run
SOURCE
=
examples/dotnet/csflow.cs
```
**OR**
this is equivalent to compiling and running
`examples/dotnet/csflow.cs`
.
-
on Unix:
```
shell
dotnet build examples/dotnet/csflow.csproj
dotnet run
--no-build
--project
examples/dotnet/csflow.csproj
```
-
on Windows:
```
shell
dotnet build examples
\d
otnet
\c
sflow.csproj
dotnet run
--no-build
--project
examples
\d
otnet
\c
sflow.csproj
```
libs/or-tools-src-ubuntu/bin/cbc
0 → 100755
View file @
05f67e26
File added
libs/or-tools-src-ubuntu/bin/gflags_completions.sh
0 → 100755
View file @
05f67e26
#!/bin/bash
# Copyright (c) 2008, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ---
# Author: Dave Nicponski
#
# This script is invoked by bash in response to a matching compspec. When
# this happens, bash calls this script using the command shown in the -C
# block of the complete entry, but also appends 3 arguments. They are:
# - The command being used for completion
# - The word being completed
# - The word preceding the completion word.
#
# Here's an example of how you might use this script:
# $ complete -o bashdefault -o default -o nospace -C \
# '/usr/local/bin/gflags_completions.sh --tab_completion_columns $COLUMNS' \
# time env binary_name another_binary [...]
# completion_word_index gets the index of the (N-1)th argument for
# this command line. completion_word gets the actual argument from
# this command line at the (N-1)th position
completion_word_index
=
"
$((
$#
-
1
))
"
completion_word
=
"
${
!completion_word_index
}
"
# TODO(user): Replace this once gflags_completions.cc has
# a bool parameter indicating unambiguously to hijack the process for
# completion purposes.
if
[
-z
"
$completion_word
"
]
;
then
# Until an empty value for the completion word stops being misunderstood
# by binaries, don't actually execute the binary or the process
# won't be hijacked!
exit
0
fi
# binary_index gets the index of the command being completed (which bash
# places in the (N-2)nd position. binary gets the actual command from
# this command line at that (N-2)nd position
binary_index
=
"
$((
$#
-
2
))
"
binary
=
"
${
!binary_index
}
"
# For completions to be universal, we may have setup the compspec to
# trigger on 'harmless pass-through' commands, like 'time' or 'env'.
# If the command being completed is one of those two, we'll need to
# identify the actual command being executed. To do this, we need
# the actual command line that the <TAB> was pressed on. Bash helpfully
# places this in the $COMP_LINE variable.
if
[
"
$binary
"
==
"time"
]
||
[
"
$binary
"
==
"env"
]
;
then
# we'll assume that the first 'argument' is actually the
# binary
# TODO(user): This is not perfect - the 'env' command, for instance,
# is allowed to have options between the 'env' and 'the command to
# be executed'. For example, consider:
# $ env FOO="bar" bin/do_something --help<TAB>
# In this case, we'll mistake the FOO="bar" portion as the binary.
# Perhaps we should continuing consuming leading words until we
# either run out of words, or find a word that is a valid file
# marked as executable. I can't think of any reason this wouldn't
# work.
# Break up the 'original command line' (not this script's command line,
# rather the one the <TAB> was pressed on) and find the second word.
parts
=(
${
COMP_LINE
}
)
binary
=
${
parts
[1]
}
fi
# Build the command line to use for completion. Basically it involves
# passing through all the arguments given to this script (except the 3
# that bash added), and appending a '--tab_completion_word "WORD"' to
# the arguments.
params
=
""
for
((
i
=
1
;
i<
=
$((
$#
-
3
))
;
++i
))
;
do
params
=
"
$params
\"
${
!i
}
\"
"
;
done
params
=
"
$params
--tab_completion_word
\"
$completion_word
\"
"
# TODO(user): Perhaps stash the output in a temporary file somewhere
# in /tmp, and only cat it to stdout if the command returned a success
# code, to prevent false positives
# If we think we have a reasonable command to execute, then execute it
# and hope for the best.
candidate
=
$(
type
-p
"
$binary
"
)
if
[
!
-z
"
$candidate
"
]
;
then
eval
"
$candidate
2>/dev/null
$params
"
elif
[
-f
"
$binary
"
]
&&
[
-x
"
$binary
"
]
;
then
eval
"
$binary
2>/dev/null
$params
"
fi
libs/or-tools-src-ubuntu/bin/protoc
0 → 100755
View file @
05f67e26
File added
libs/or-tools-src-ubuntu/bin/protoc-3.12.2.0
0 → 100755
View file @
05f67e26
File added
libs/or-tools-src-ubuntu/examples/cpp/README.md
0 → 100644
View file @
05f67e26
# C++ examples
The following examples showcase how to use the different Operations Research libraries.
## Examples list
-
Constraint Solver examples:
-
cryptarithm.cc Demonstrates the use of basic modeling objects
(integer variables, arithmetic constraints and expressions,
simple search).
-
golomb.cc Demonstrates how to handle objective functions and collect
solutions found during the search.
-
magic_square.cc Shows how to use the automatic search to solve your
problem.
-
costas_array.cc Solves the problem of Costas Array (a constrained
assignment problem used for radars) with two version. On version is
a feasibility version with hard constraints, the other version is
an optimization version with soft constraints and violation costs.
-
jobshop.cc Demonstrates scheduling of jobs on different machines.
-
jobshop_ls.cc Demonstrates scheduling of jobs on different machines with
a search using Local Search and Large Neighorhood Search.
-
nqueens.cc Solves the n-queen problem. It also demonstrates how to break
symmetries during search.
-
network_routing.cc Solves a multicommodity mono-routing
problem with capacity constraints and a max usage cost structure.
-
sports_scheduling.cc Finds a soccer championship schedule. Its uses an
original approach where all constraints attached to either one team,
or one week are regrouped into one global 'AllowedAssignment' constraints.
-
dobble_ls.cc Shows how to write Local Search operators and Local Search
filters in a context of an assignment/partitioning problem. It also
shows how to write a simple constraint.
-
Routing examples:
-
tsp.cc Travelling Salesman Problem.
-
cvrptw.cc Capacitated Vehicle Routing Problem with Time Windows.
-
pdptw.cc Pickup and Delivery Problem with Time Windows.
-
Graph examples:
-
flow_api.cc Demonstrates how to use Min-Cost Flow and Max-Flow api.
-
linear_assignment_api.cc Demonstrates how to use the Linear Sum
Assignment solver.
-
dimacs_assignment.cc Solves DIMACS challenge on assignment
problems.
-
Linear and integer programming examples:
-
linear_programming.cc Demonstrates how to use the linear solver
wrapper API to solve Linear Programming problems.
-
integer_programming.cc Demonstrates how to use the linear solver
wrapper API to solve Integer Programming problems.
-
linear_solver_protocol_buffers.cc Demonstrates how protocol
buffers can be used as input and output to the linear solver wrapper.
-
strawberry_fields_with_column_generation.cc Complex example that
demonstrates how to use dynamic column generation to solve a 2D
covering problem.
-
Utilities
-
model_util.cc A utility to manipulate model files (.cp) dumped by the
solver.
# Execution
Running the examples will involve building them, then running them.
You can run the following command from the
**top**
directory:
```
shell
make build
SOURCE
=
examples/cpp/<example>.cc
make run
SOURCE
=
examples/cpp/<example>.cc
```
libs/or-tools-src-ubuntu/examples/cpp/assignment_sat.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START program]
// [START import]
#include
"ortools/sat/cp_model.h"
// [END import]
namespace
operations_research
{
namespace
sat
{
void
IntegerProgrammingExample
()
{
// Data
// [START data_model]
const
std
::
vector
<
std
::
vector
<
double
>>
costs
{
{
90
,
80
,
75
,
70
},
{
35
,
85
,
55
,
65
},
{
125
,
95
,
90
,
95
},
{
45
,
110
,
95
,
115
},
{
50
,
100
,
90
,
100
},
};
const
int
num_workers
=
costs
.
size
();
const
int
num_tasks
=
costs
[
0
].
size
();
// [END data_model]
// Model
// [START model]
CpModelBuilder
cp_model
;
// [END model]
// Variables
// [START variables]
// x[i][j] is an array of Boolean variables. x[i][j] is true
// if worker i is assigned to task j.
std
::
vector
<
std
::
vector
<
BoolVar
>>
x
(
num_workers
,
std
::
vector
<
BoolVar
>
(
num_tasks
));
for
(
int
i
=
0
;
i
<
num_workers
;
++
i
)
{
for
(
int
j
=
0
;
j
<
num_tasks
;
++
j
)
{
x
[
i
][
j
]
=
cp_model
.
NewBoolVar
();
}
}
// [END variables]
// Constraints
// [START constraints]
// Each worker is assigned to at most one task.
for
(
int
i
=
0
;
i
<
num_workers
;
++
i
)
{
LinearExpr
worker_sum
;
for
(
int
j
=
0
;
j
<
num_tasks
;
++
j
)
{
worker_sum
.
AddTerm
(
x
[
i
][
j
],
1
);
}
cp_model
.
AddLessOrEqual
(
worker_sum
,
1
);
}
// Each task is assigned to exactly one worker.
for
(
int
j
=
0
;
j
<
num_tasks
;
++
j
)
{
LinearExpr
task_sum
;
for
(
int
i
=
0
;
i
<
num_workers
;
++
i
)
{
task_sum
.
AddTerm
(
x
[
i
][
j
],
1
);
}
cp_model
.
AddEquality
(
task_sum
,
1
);
}
// [END constraints]
// Objective
// [START objective]
LinearExpr
total_cost
;
for
(
int
i
=
0
;
i
<
num_workers
;
++
i
)
{
for
(
int
j
=
0
;
j
<
num_tasks
;
++
j
)
{
total_cost
.
AddTerm
(
x
[
i
][
j
],
costs
[
i
][
j
]);
}
}
cp_model
.
Minimize
(
total_cost
);
// [END objective]
// Solve
// [START solve]
const
CpSolverResponse
response
=
Solve
(
cp_model
.
Build
());
// [END solve]
// Print solution.
// [START print_solution]
if
(
response
.
status
()
==
CpSolverStatus
::
INFEASIBLE
)
{
LOG
(
FATAL
)
<<
"No solution found."
;
}
LOG
(
INFO
)
<<
"Total cost: "
<<
response
.
objective_value
();
LOG
(
INFO
);
for
(
int
i
=
0
;
i
<
num_workers
;
++
i
)
{
for
(
int
j
=
0
;
j
<
num_tasks
;
++
j
)
{
if
(
SolutionBooleanValue
(
response
,
x
[
i
][
j
]))
{
LOG
(
INFO
)
<<
"Task "
<<
i
<<
" assigned to worker "
<<
j
<<
". Cost: "
<<
costs
[
i
][
j
];
}
}
}
// [END print_solution]
}
}
// namespace sat
}
// namespace operations_research
int
main
(
int
argc
,
char
**
argv
)
{
operations_research
::
sat
::
IntegerProgrammingExample
();
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/bin_packing_mip.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START program]
// [START import]
#include
<iostream>
#include
<numeric>
#include
<vector>
#include
"ortools/linear_solver/linear_expr.h"
#include
"ortools/linear_solver/linear_solver.h"
// [END import]
// [START program_part1]
namespace
operations_research
{
// [START data_model]
struct
DataModel
{
const
std
::
vector
<
double
>
weights
=
{
48
,
30
,
19
,
36
,
36
,
27
,
42
,
42
,
36
,
24
,
30
};
const
int
num_items
=
weights
.
size
();
const
int
num_bins
=
weights
.
size
();
const
int
bin_capacity
=
100
;
};
// [END data_model]
void
BinPackingMip
()
{
// [START data]
DataModel
data
;
// [END data]
// [END program_part1]
// [START solver]
// Create the mip solver with the CBC backend.
MPSolver
solver
(
"bin_packing_mip"
,
MPSolver
::
CBC_MIXED_INTEGER_PROGRAMMING
);
// [END solver]
// [START program_part2]
// [START variables]
std
::
vector
<
std
::
vector
<
const
MPVariable
*>>
x
(
data
.
num_items
,
std
::
vector
<
const
MPVariable
*>
(
data
.
num_bins
));
for
(
int
i
=
0
;
i
<
data
.
num_items
;
++
i
)
{
for
(
int
j
=
0
;
j
<
data
.
num_bins
;
++
j
)
{
x
[
i
][
j
]
=
solver
.
MakeIntVar
(
0.0
,
1.0
,
""
);
}
}
// y[j] = 1 if bin j is used.
std
::
vector
<
const
MPVariable
*>
y
(
data
.
num_bins
);
for
(
int
j
=
0
;
j
<
data
.
num_bins
;
++
j
)
{
y
[
j
]
=
solver
.
MakeIntVar
(
0.0
,
1.0
,
""
);
}
// [END variables]
// [START constraints]
// Create the constraints.
// Each item is in exactly one bin.
for
(
int
i
=
0
;
i
<
data
.
num_items
;
++
i
)
{
LinearExpr
sum
;
for
(
int
j
=
0
;
j
<
data
.
num_bins
;
++
j
)
{
sum
+=
x
[
i
][
j
];
}
solver
.
MakeRowConstraint
(
sum
==
1.0
);
}
// For each bin that is used, the total packed weight can be at most
// the bin capacity.
for
(
int
j
=
0
;
j
<
data
.
num_bins
;
++
j
)
{
LinearExpr
weight
;
for
(
int
i
=
0
;
i
<
data
.
num_items
;
++
i
)
{
weight
+=
data
.
weights
[
i
]
*
LinearExpr
(
x
[
i
][
j
]);
}
solver
.
MakeRowConstraint
(
weight
<=
LinearExpr
(
y
[
j
])
*
data
.
bin_capacity
);
}
// [END constraints]
// [START objective]
// Create the objective function.
MPObjective
*
const
objective
=
solver
.
MutableObjective
();
LinearExpr
num_bins_used
;
for
(
int
j
=
0
;
j
<
data
.
num_bins
;
++
j
)
{
num_bins_used
+=
y
[
j
];
}
objective
->
MinimizeLinearExpr
(
num_bins_used
);
// [END objective]
// [START solve]
const
MPSolver
::
ResultStatus
result_status
=
solver
.
Solve
();
// [END solve]
// [START print_solution]
// Check that the problem has an optimal solution.
if
(
result_status
!=
MPSolver
::
OPTIMAL
)
{
std
::
cerr
<<
"The problem does not have an optimal solution!"
;
return
;
}
std
::
cout
<<
"Number of bins used: "
<<
objective
->
Value
()
<<
std
::
endl
<<
std
::
endl
;
double
total_weight
=
0
;
for
(
int
j
=
0
;
j
<
data
.
num_bins
;
++
j
)
{
if
(
y
[
j
]
->
solution_value
()
==
1
)
{
std
::
cout
<<
"Bin "
<<
j
<<
std
::
endl
<<
std
::
endl
;
double
bin_weight
=
0
;
for
(
int
i
=
0
;
i
<
data
.
num_items
;
++
i
)
{
if
(
x
[
i
][
j
]
->
solution_value
()
==
1
)
{
std
::
cout
<<
"Item "
<<
i
<<
" - Weight: "
<<
data
.
weights
[
i
]
<<
std
::
endl
;
bin_weight
+=
data
.
weights
[
i
];
}
}
std
::
cout
<<
"Packed bin weight: "
<<
bin_weight
<<
std
::
endl
<<
std
::
endl
;
total_weight
+=
bin_weight
;
}
}
std
::
cout
<<
"Total packed weight: "
<<
total_weight
<<
std
::
endl
;
// [END print_solution]
}
}
// namespace operations_research
int
main
(
int
argc
,
char
**
argv
)
{
operations_research
::
BinPackingMip
();
return
EXIT_SUCCESS
;
}
// [END program_part2]
// [END program]
libs/or-tools-src-ubuntu/examples/cpp/binpacking_problem_sat.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include
"ortools/sat/cp_model.h"
namespace
operations_research
{
namespace
sat
{
void
BinpackingProblemSat
()
{
// Data.
const
int
kBinCapacity
=
100
;
const
int
kSlackCapacity
=
20
;
const
int
kNumBins
=
5
;
const
std
::
vector
<
std
::
vector
<
int
>>
items
=
{
{
20
,
6
},
{
15
,
6
},
{
30
,
4
},
{
45
,
3
}};
const
int
num_items
=
items
.
size
();
// Model.
CpModelBuilder
cp_model
;
// Main variables.
std
::
vector
<
std
::
vector
<
IntVar
>>
x
(
num_items
);
for
(
int
i
=
0
;
i
<
num_items
;
++
i
)
{
const
int
num_copies
=
items
[
i
][
1
];
for
(
int
b
=
0
;
b
<
kNumBins
;
++
b
)
{
x
[
i
].
push_back
(
cp_model
.
NewIntVar
({
0
,
num_copies
}));
}
}
// Load variables.
std
::
vector
<
IntVar
>
load
(
kNumBins
);
for
(
int
b
=
0
;
b
<
kNumBins
;
++
b
)
{
load
[
b
]
=
cp_model
.
NewIntVar
({
0
,
kBinCapacity
});
}
// Slack variables.
std
::
vector
<
BoolVar
>
slacks
(
kNumBins
);
for
(
int
b
=
0
;
b
<
kNumBins
;
++
b
)
{
slacks
[
b
]
=
cp_model
.
NewBoolVar
();
}
// Links load and x.
for
(
int
b
=
0
;
b
<
kNumBins
;
++
b
)
{
LinearExpr
expr
;
for
(
int
i
=
0
;
i
<
num_items
;
++
i
)
{
expr
.
AddTerm
(
x
[
i
][
b
],
items
[
i
][
0
]);
}
cp_model
.
AddEquality
(
expr
,
load
[
b
]);
}
// Place all items.
for
(
int
i
=
0
;
i
<
num_items
;
++
i
)
{
cp_model
.
AddEquality
(
LinearExpr
::
Sum
(
x
[
i
]),
items
[
i
][
1
]);
}
// Links load and slack through an equivalence relation.
const
int
safe_capacity
=
kBinCapacity
-
kSlackCapacity
;
for
(
int
b
=
0
;
b
<
kNumBins
;
++
b
)
{
// slack[b] => load[b] <= safe_capacity.
cp_model
.
AddLessOrEqual
(
load
[
b
],
safe_capacity
).
OnlyEnforceIf
(
slacks
[
b
]);
// not(slack[b]) => load[b] > safe_capacity.
cp_model
.
AddGreaterThan
(
load
[
b
],
safe_capacity
)
.
OnlyEnforceIf
(
Not
(
slacks
[
b
]));
}
// Maximize sum of slacks.
cp_model
.
Maximize
(
LinearExpr
::
BooleanSum
(
slacks
));
// Solving part.
const
CpSolverResponse
response
=
Solve
(
cp_model
.
Build
());
LOG
(
INFO
)
<<
CpSolverResponseStats
(
response
);
}
}
// namespace sat
}
// namespace operations_research
int
main
()
{
operations_research
::
sat
::
BinpackingProblemSat
();
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/bool_or_sample_sat.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include
"ortools/sat/cp_model.h"
namespace
operations_research
{
namespace
sat
{
void
BoolOrSampleSat
()
{
CpModelBuilder
cp_model
;
const
BoolVar
x
=
cp_model
.
NewBoolVar
();
const
BoolVar
y
=
cp_model
.
NewBoolVar
();
cp_model
.
AddBoolOr
({
x
,
Not
(
y
)});
}
}
// namespace sat
}
// namespace operations_research
int
main
()
{
operations_research
::
sat
::
BoolOrSampleSat
();
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/channeling_sample_sat.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include
"ortools/sat/cp_model.h"
#include
"ortools/sat/model.h"
#include
"ortools/sat/sat_parameters.pb.h"
namespace
operations_research
{
namespace
sat
{
void
ChannelingSampleSat
()
{
// Create the CP-SAT model.
CpModelBuilder
cp_model
;
// Declare our two primary variables.
const
IntVar
x
=
cp_model
.
NewIntVar
({
0
,
10
});
const
IntVar
y
=
cp_model
.
NewIntVar
({
0
,
10
});
// Declare our intermediate boolean variable.
const
BoolVar
b
=
cp_model
.
NewBoolVar
();
// Implement b == (x >= 5).
cp_model
.
AddGreaterOrEqual
(
x
,
5
).
OnlyEnforceIf
(
b
);
cp_model
.
AddLessThan
(
x
,
5
).
OnlyEnforceIf
(
Not
(
b
));
// Create our two half-reified constraints.
// First, b implies (y == 10 - x).
cp_model
.
AddEquality
(
LinearExpr
::
Sum
({
x
,
y
}),
10
).
OnlyEnforceIf
(
b
);
// Second, not(b) implies y == 0.
cp_model
.
AddEquality
(
y
,
0
).
OnlyEnforceIf
(
Not
(
b
));
// Search for x values in increasing order.
cp_model
.
AddDecisionStrategy
({
x
},
DecisionStrategyProto
::
CHOOSE_FIRST
,
DecisionStrategyProto
::
SELECT_MIN_VALUE
);
// Create a solver and solve with a fixed search.
Model
model
;
SatParameters
parameters
;
parameters
.
set_search_branching
(
SatParameters
::
FIXED_SEARCH
);
parameters
.
set_enumerate_all_solutions
(
true
);
model
.
Add
(
NewSatParameters
(
parameters
));
model
.
Add
(
NewFeasibleSolutionObserver
([
&
](
const
CpSolverResponse
&
r
)
{
LOG
(
INFO
)
<<
"x="
<<
SolutionIntegerValue
(
r
,
x
)
<<
" y="
<<
SolutionIntegerValue
(
r
,
y
)
<<
" b="
<<
SolutionBooleanValue
(
r
,
b
);
}));
SolveCpModel
(
cp_model
.
Build
(),
&
model
);
}
}
// namespace sat
}
// namespace operations_research
int
main
()
{
operations_research
::
sat
::
ChannelingSampleSat
();
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/costas_array_sat.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Costas Array Problem
//
// Finds an NxN matrix of 0s and 1s, with only one 1 per row,
// and one 1 per column, such that all displacement vectors
// between each pair of 1s are distinct.
//
// This example contains two separate implementations. CostasHard()
// uses hard constraints, whereas CostasSoft() uses a minimizer to
// minimize the number of duplicates.
#include
<ctime>
#include
<set>
#include
<utility>
#include
"absl/strings/str_cat.h"
#include
"absl/strings/str_format.h"
#include
"ortools/base/commandlineflags.h"
#include
"ortools/base/integral_types.h"
#include
"ortools/base/logging.h"
#include
"ortools/base/random.h"
#include
"ortools/sat/cp_model.h"
#include
"ortools/sat/model.h"
DEFINE_int32
(
minsize
,
0
,
"Minimum problem size."
);
DEFINE_int32
(
maxsize
,
0
,
"Maximum problem size."
);
DEFINE_int32
(
model
,
1
,
"Model type: 1 integer variables hard, 2 boolean variables, 3 "
"boolean_variable soft"
);
DEFINE_string
(
params
,
""
,
"Sat parameters."
);
namespace
operations_research
{
namespace
sat
{
// Checks that all pairwise distances are unique and returns all violators
void
CheckConstraintViolators
(
const
std
::
vector
<
int64
>
&
vars
,
std
::
vector
<
int
>
*
const
violators
)
{
int
dim
=
vars
.
size
();
// Check that all indices are unique
for
(
int
i
=
0
;
i
<
dim
;
++
i
)
{
for
(
int
k
=
i
+
1
;
k
<
dim
;
++
k
)
{
if
(
vars
[
i
]
==
vars
[
k
])
{
violators
->
push_back
(
i
);
violators
->
push_back
(
k
);
}
}
}
// Check that all differences are unique for each level
for
(
int
level
=
1
;
level
<
dim
;
++
level
)
{
for
(
int
i
=
0
;
i
<
dim
-
level
;
++
i
)
{
const
int
difference
=
vars
[
i
+
level
]
-
vars
[
i
];
for
(
int
k
=
i
+
1
;
k
<
dim
-
level
;
++
k
)
{
if
(
difference
==
vars
[
k
+
level
]
-
vars
[
k
])
{
violators
->
push_back
(
k
+
level
);
violators
->
push_back
(
k
);
violators
->
push_back
(
i
+
level
);
violators
->
push_back
(
i
);
}
}
}
}
}
// Check that all pairwise differences are unique
bool
CheckCostas
(
const
std
::
vector
<
int64
>
&
vars
)
{
std
::
vector
<
int
>
violators
;
CheckConstraintViolators
(
vars
,
&
violators
);
return
violators
.
empty
();
}
// Computes a Costas Array.
void
CostasHard
(
const
int
dim
)
{
CpModelBuilder
cp_model
;
// create the variables
std
::
vector
<
IntVar
>
vars
;
Domain
domain
(
1
,
dim
);
for
(
int
i
=
0
;
i
<
dim
;
++
i
)
{
vars
.
push_back
(
cp_model
.
NewIntVar
(
domain
).
WithName
(
absl
::
StrCat
(
"var_"
,
i
)));
}
cp_model
.
AddAllDifferent
(
vars
);
// Check that the pairwise difference is unique
for
(
int
i
=
1
;
i
<
dim
;
++
i
)
{
std
::
vector
<
IntVar
>
subset
;
Domain
diff
(
-
dim
,
dim
);
for
(
int
j
=
0
;
j
<
dim
-
i
;
++
j
)
{
subset
.
push_back
(
cp_model
.
NewIntVar
(
diff
));
cp_model
.
AddEquality
(
LinearExpr
::
Sum
({
subset
[
j
],
vars
[
j
]}),
vars
[
j
+
i
]);
}
cp_model
.
AddAllDifferent
(
subset
);
}
Model
model
;
if
(
!
FLAGS_params
.
empty
())
{
model
.
Add
(
NewSatParameters
(
FLAGS_params
));
}
const
CpSolverResponse
response
=
SolveCpModel
(
cp_model
.
Build
(),
&
model
);
if
(
response
.
status
()
==
CpSolverStatus
::
FEASIBLE
)
{
std
::
vector
<
int64
>
costas_matrix
;
std
::
string
output
;
for
(
int
n
=
0
;
n
<
dim
;
++
n
)
{
const
int64
v
=
SolutionIntegerValue
(
response
,
vars
[
n
]);
costas_matrix
.
push_back
(
v
);
absl
::
StrAppendFormat
(
&
output
,
"%3lld"
,
v
);
}
LOG
(
INFO
)
<<
output
<<
" ("
<<
response
.
wall_time
()
<<
" s)"
;
CHECK
(
CheckCostas
(
costas_matrix
))
<<
": Solution is not a valid Costas Matrix."
;
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
}
// Computes a Costas Array.
void
CostasBool
(
const
int
dim
)
{
CpModelBuilder
cp_model
;
// create the variables
std
::
vector
<
std
::
vector
<
BoolVar
>>
vars
(
dim
);
std
::
vector
<
std
::
vector
<
BoolVar
>>
transposed_vars
(
dim
);
for
(
int
i
=
0
;
i
<
dim
;
++
i
)
{
for
(
int
j
=
0
;
j
<
dim
;
++
j
)
{
const
BoolVar
var
=
cp_model
.
NewBoolVar
();
vars
[
i
].
push_back
(
var
);
transposed_vars
[
j
].
push_back
(
var
);
}
}
for
(
int
i
=
0
;
i
<
dim
;
++
i
)
{
cp_model
.
AddEquality
(
LinearExpr
::
BooleanSum
(
vars
[
i
]),
1
);
cp_model
.
AddEquality
(
LinearExpr
::
BooleanSum
(
transposed_vars
[
i
]),
1
);
}
// Check that the pairwise difference is unique
for
(
int
step
=
1
;
step
<
dim
;
++
step
)
{
for
(
int
diff
=
1
;
diff
<
dim
-
1
;
++
diff
)
{
std
::
vector
<
BoolVar
>
positive_diffs
;
std
::
vector
<
BoolVar
>
negative_diffs
;
for
(
int
var
=
0
;
var
<
dim
-
step
;
++
var
)
{
for
(
int
value
=
0
;
value
<
dim
-
diff
;
++
value
)
{
const
BoolVar
pos
=
cp_model
.
NewBoolVar
();
const
BoolVar
neg
=
cp_model
.
NewBoolVar
();
positive_diffs
.
push_back
(
pos
);
negative_diffs
.
push_back
(
neg
);
cp_model
.
AddBoolOr
({
Not
(
vars
[
var
][
value
]),
Not
(
vars
[
var
+
step
][
value
+
diff
]),
pos
});
cp_model
.
AddBoolOr
({
Not
(
vars
[
var
][
value
+
diff
]),
Not
(
vars
[
var
+
step
][
value
]),
neg
});
}
}
cp_model
.
AddLessOrEqual
(
LinearExpr
::
BooleanSum
(
positive_diffs
),
1
);
cp_model
.
AddLessOrEqual
(
LinearExpr
::
BooleanSum
(
negative_diffs
),
1
);
}
}
Model
model
;
if
(
!
FLAGS_params
.
empty
())
{
model
.
Add
(
NewSatParameters
(
FLAGS_params
));
}
const
CpSolverResponse
response
=
SolveCpModel
(
cp_model
.
Build
(),
&
model
);
if
(
response
.
status
()
==
CpSolverStatus
::
FEASIBLE
)
{
std
::
vector
<
int64
>
costas_matrix
;
std
::
string
output
;
for
(
int
n
=
0
;
n
<
dim
;
++
n
)
{
for
(
int
v
=
0
;
v
<
dim
;
++
v
)
{
if
(
SolutionBooleanValue
(
response
,
vars
[
n
][
v
]))
{
costas_matrix
.
push_back
(
v
+
1
);
absl
::
StrAppendFormat
(
&
output
,
"%3lld"
,
v
+
1
);
break
;
}
}
}
LOG
(
INFO
)
<<
output
<<
" ("
<<
response
.
wall_time
()
<<
" s)"
;
CHECK
(
CheckCostas
(
costas_matrix
))
<<
": Solution is not a valid Costas Matrix."
;
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
}
// Computes a Costas Array with a minimization objective.
void
CostasBoolSoft
(
const
int
dim
)
{
CpModelBuilder
cp_model
;
// create the variables
std
::
vector
<
std
::
vector
<
BoolVar
>>
vars
(
dim
);
std
::
vector
<
std
::
vector
<
BoolVar
>>
transposed_vars
(
dim
);
for
(
int
i
=
0
;
i
<
dim
;
++
i
)
{
for
(
int
j
=
0
;
j
<
dim
;
++
j
)
{
const
BoolVar
var
=
cp_model
.
NewBoolVar
();
vars
[
i
].
push_back
(
var
);
transposed_vars
[
j
].
push_back
(
var
);
}
}
for
(
int
i
=
0
;
i
<
dim
;
++
i
)
{
cp_model
.
AddEquality
(
LinearExpr
::
BooleanSum
(
vars
[
i
]),
1
);
cp_model
.
AddEquality
(
LinearExpr
::
BooleanSum
(
transposed_vars
[
i
]),
1
);
}
std
::
vector
<
IntVar
>
all_violations
;
// Check that the pairwise difference is unique
for
(
int
step
=
1
;
step
<
dim
;
++
step
)
{
for
(
int
diff
=
1
;
diff
<
dim
-
1
;
++
diff
)
{
std
::
vector
<
BoolVar
>
positive_diffs
;
std
::
vector
<
BoolVar
>
negative_diffs
;
for
(
int
var
=
0
;
var
<
dim
-
step
;
++
var
)
{
for
(
int
value
=
0
;
value
<
dim
-
diff
;
++
value
)
{
const
BoolVar
pos
=
cp_model
.
NewBoolVar
();
const
BoolVar
neg
=
cp_model
.
NewBoolVar
();
positive_diffs
.
push_back
(
pos
);
negative_diffs
.
push_back
(
neg
);
cp_model
.
AddBoolOr
({
Not
(
vars
[
var
][
value
]),
Not
(
vars
[
var
+
step
][
value
+
diff
]),
pos
});
cp_model
.
AddBoolOr
({
Not
(
vars
[
var
][
value
+
diff
]),
Not
(
vars
[
var
+
step
][
value
]),
neg
});
}
}
const
IntVar
pos_var
=
cp_model
.
NewIntVar
(
Domain
(
0
,
positive_diffs
.
size
()));
const
IntVar
neg_var
=
cp_model
.
NewIntVar
(
Domain
(
0
,
negative_diffs
.
size
()));
cp_model
.
AddGreaterOrEqual
(
pos_var
,
LinearExpr
::
BooleanSum
(
positive_diffs
).
AddConstant
(
-
1
));
cp_model
.
AddGreaterOrEqual
(
neg_var
,
LinearExpr
::
BooleanSum
(
negative_diffs
).
AddConstant
(
-
1
));
all_violations
.
push_back
(
pos_var
);
all_violations
.
push_back
(
neg_var
);
}
}
cp_model
.
Minimize
(
LinearExpr
::
Sum
(
all_violations
));
Model
model
;
if
(
!
FLAGS_params
.
empty
())
{
model
.
Add
(
NewSatParameters
(
FLAGS_params
));
}
const
CpSolverResponse
response
=
SolveCpModel
(
cp_model
.
Build
(),
&
model
);
if
(
response
.
status
()
==
CpSolverStatus
::
OPTIMAL
)
{
std
::
vector
<
int64
>
costas_matrix
;
std
::
string
output
;
for
(
int
n
=
0
;
n
<
dim
;
++
n
)
{
for
(
int
v
=
0
;
v
<
dim
;
++
v
)
{
if
(
SolutionBooleanValue
(
response
,
vars
[
n
][
v
]))
{
costas_matrix
.
push_back
(
v
+
1
);
absl
::
StrAppendFormat
(
&
output
,
"%3lld"
,
v
+
1
);
break
;
}
}
}
LOG
(
INFO
)
<<
output
<<
" ("
<<
response
.
wall_time
()
<<
" s)"
;
CHECK
(
CheckCostas
(
costas_matrix
))
<<
": Solution is not a valid Costas Matrix."
;
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
}
}
// namespace sat
}
// namespace operations_research
int
main
(
int
argc
,
char
**
argv
)
{
gflags
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
int
min
=
1
;
int
max
=
10
;
if
(
FLAGS_minsize
!=
0
)
{
min
=
FLAGS_minsize
;
if
(
FLAGS_maxsize
!=
0
)
{
max
=
FLAGS_maxsize
;
}
else
{
max
=
min
;
}
}
for
(
int
size
=
min
;
size
<=
max
;
++
size
)
{
LOG
(
INFO
)
<<
"Computing Costas Array for dim = "
<<
size
;
if
(
FLAGS_model
==
1
)
{
operations_research
::
sat
::
CostasHard
(
size
);
}
else
if
(
FLAGS_model
==
2
)
{
operations_research
::
sat
::
CostasBool
(
size
);
}
else
if
(
FLAGS_model
==
3
)
{
operations_research
::
sat
::
CostasBoolSoft
(
size
);
}
}
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/cp_is_fun_sat.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START program]
// Cryptarithmetic puzzle
//
// First attempt to solve equation CP + IS + FUN = TRUE
// where each letter represents a unique digit.
//
// This problem has 72 different solutions in base 10.
#include
<vector>
#include
"ortools/sat/cp_model.h"
#include
"ortools/sat/model.h"
#include
"ortools/sat/sat_parameters.pb.h"
namespace
operations_research
{
namespace
sat
{
void
CPIsFunSat
()
{
// Instantiate the solver.
CpModelBuilder
cp_model
;
// [START variables]
const
int64
kBase
=
10
;
// Define decision variables.
Domain
digit
(
0
,
kBase
-
1
);
Domain
non_zero_digit
(
1
,
kBase
-
1
);
IntVar
c
=
cp_model
.
NewIntVar
(
non_zero_digit
).
WithName
(
"C"
);
IntVar
p
=
cp_model
.
NewIntVar
(
digit
).
WithName
(
"P"
);
IntVar
i
=
cp_model
.
NewIntVar
(
non_zero_digit
).
WithName
(
"I"
);
IntVar
s
=
cp_model
.
NewIntVar
(
digit
).
WithName
(
"S"
);
IntVar
f
=
cp_model
.
NewIntVar
(
non_zero_digit
).
WithName
(
"F"
);
IntVar
u
=
cp_model
.
NewIntVar
(
digit
).
WithName
(
"U"
);
IntVar
n
=
cp_model
.
NewIntVar
(
digit
).
WithName
(
"N"
);
IntVar
t
=
cp_model
.
NewIntVar
(
non_zero_digit
).
WithName
(
"T"
);
IntVar
r
=
cp_model
.
NewIntVar
(
digit
).
WithName
(
"R"
);
IntVar
e
=
cp_model
.
NewIntVar
(
digit
).
WithName
(
"E"
);
// [END variables]
// [START constraints]
// Define constraints.
cp_model
.
AddAllDifferent
({
c
,
p
,
i
,
s
,
f
,
u
,
n
,
t
,
r
,
e
});
// CP + IS + FUN = TRUE
cp_model
.
AddEquality
(
LinearExpr
::
ScalProd
({
c
,
p
,
i
,
s
,
f
,
u
,
n
},
{
kBase
,
1
,
kBase
,
1
,
kBase
*
kBase
,
kBase
,
1
}),
LinearExpr
::
ScalProd
({
t
,
r
,
u
,
e
},
{
kBase
*
kBase
*
kBase
,
kBase
*
kBase
,
kBase
,
1
}));
// [END constraints]
// [START solution_printing]
Model
model
;
int
num_solutions
=
0
;
model
.
Add
(
NewFeasibleSolutionObserver
([
&
](
const
CpSolverResponse
&
response
)
{
LOG
(
INFO
)
<<
"Solution "
<<
num_solutions
;
LOG
(
INFO
)
<<
"C="
<<
SolutionIntegerValue
(
response
,
c
)
<<
" "
<<
"P="
<<
SolutionIntegerValue
(
response
,
p
)
<<
" "
<<
"I="
<<
SolutionIntegerValue
(
response
,
i
)
<<
" "
<<
"S="
<<
SolutionIntegerValue
(
response
,
s
)
<<
" "
<<
"F="
<<
SolutionIntegerValue
(
response
,
f
)
<<
" "
<<
"U="
<<
SolutionIntegerValue
(
response
,
u
)
<<
" "
<<
"N="
<<
SolutionIntegerValue
(
response
,
n
)
<<
" "
<<
"T="
<<
SolutionIntegerValue
(
response
,
t
)
<<
" "
<<
"R="
<<
SolutionIntegerValue
(
response
,
r
)
<<
" "
<<
"E="
<<
SolutionIntegerValue
(
response
,
e
);
num_solutions
++
;
}));
// [END solution_printing]
// [START solve]
// Tell the solver to enumerate all solutions.
SatParameters
parameters
;
parameters
.
set_enumerate_all_solutions
(
true
);
model
.
Add
(
NewSatParameters
(
parameters
));
const
CpSolverResponse
response
=
SolveCpModel
(
cp_model
.
Build
(),
&
model
);
LOG
(
INFO
)
<<
"Number of solutions found: "
<<
num_solutions
;
// [END solve]
}
}
// namespace sat
}
// namespace operations_research
// ----- MAIN -----
int
main
(
int
argc
,
char
**
argv
)
{
operations_research
::
sat
::
CPIsFunSat
();
return
EXIT_SUCCESS
;
}
// [END program]
libs/or-tools-src-ubuntu/examples/cpp/cvrp_disjoint_tw.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Capacitated Vehicle Routing Problem with Disjoint Time Windows (and optional
// orders).
// A description of the problem can be found here:
// http://en.wikipedia.org/wiki/Vehicle_routing_problem.
// The variant which is tackled by this model includes a capacity dimension,
// disjoint time windows and optional orders, with a penalty cost if orders are
// not performed. For the sake of simplicty, orders are randomly located and
// distances are computed using the Manhattan distance. Distances are assumed
// to be in meters and times in seconds.
#include
<vector>
#include
"examples/cpp/cvrptw_lib.h"
#include
"google/protobuf/text_format.h"
#include
"ortools/base/commandlineflags.h"
#include
"ortools/base/integral_types.h"
#include
"ortools/base/logging.h"
#include
"ortools/constraint_solver/routing.h"
#include
"ortools/constraint_solver/routing_index_manager.h"
#include
"ortools/constraint_solver/routing_parameters.h"
#include
"ortools/constraint_solver/routing_parameters.pb.h"
using
operations_research
::
ACMRandom
;
using
operations_research
::
Assignment
;
using
operations_research
::
DefaultRoutingSearchParameters
;
using
operations_research
::
GetSeed
;
using
operations_research
::
LocationContainer
;
using
operations_research
::
RandomDemand
;
using
operations_research
::
RoutingDimension
;
using
operations_research
::
RoutingIndexManager
;
using
operations_research
::
RoutingModel
;
using
operations_research
::
RoutingNodeIndex
;
using
operations_research
::
RoutingSearchParameters
;
using
operations_research
::
ServiceTimePlusTransition
;
using
operations_research
::
Solver
;
DEFINE_int32
(
vrp_orders
,
100
,
"Nodes in the problem."
);
DEFINE_int32
(
vrp_vehicles
,
20
,
"Size of Traveling Salesman Problem instance."
);
DEFINE_int32
(
vrp_windows
,
5
,
"Number of disjoint windows per node."
);
DEFINE_bool
(
vrp_use_deterministic_random_seed
,
false
,
"Use deterministic random seeds."
);
DEFINE_bool
(
vrp_use_same_vehicle_costs
,
false
,
"Use same vehicle costs in the routing model"
);
DEFINE_string
(
routing_search_parameters
,
""
,
"Text proto RoutingSearchParameters (possibly partial) that will "
"override the DefaultRoutingSearchParameters()"
);
const
char
*
kTime
=
"Time"
;
const
char
*
kCapacity
=
"Capacity"
;
const
int64
kMaxNodesPerGroup
=
10
;
const
int64
kSameVehicleCost
=
1000
;
int
main
(
int
argc
,
char
**
argv
)
{
gflags
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
CHECK_LT
(
0
,
FLAGS_vrp_orders
)
<<
"Specify an instance size greater than 0."
;
CHECK_LT
(
0
,
FLAGS_vrp_vehicles
)
<<
"Specify a non-null vehicle fleet size."
;
// VRP of size FLAGS_vrp_size.
// Nodes are indexed from 0 to FLAGS_vrp_orders, the starts and ends of
// the routes are at node 0.
const
RoutingIndexManager
::
NodeIndex
kDepot
(
0
);
RoutingIndexManager
manager
(
FLAGS_vrp_orders
+
1
,
FLAGS_vrp_vehicles
,
kDepot
);
RoutingModel
routing
(
manager
);
// Setting up locations.
const
int64
kXMax
=
100000
;
const
int64
kYMax
=
100000
;
const
int64
kSpeed
=
10
;
LocationContainer
locations
(
kSpeed
,
FLAGS_vrp_use_deterministic_random_seed
);
for
(
int
location
=
0
;
location
<=
FLAGS_vrp_orders
;
++
location
)
{
locations
.
AddRandomLocation
(
kXMax
,
kYMax
);
}
// Setting the cost function.
const
int
vehicle_cost
=
routing
.
RegisterTransitCallback
([
&
locations
,
&
manager
](
int64
i
,
int64
j
)
{
return
locations
.
ManhattanDistance
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
});
routing
.
SetArcCostEvaluatorOfAllVehicles
(
vehicle_cost
);
// Adding capacity dimension constraints.
const
int64
kVehicleCapacity
=
40
;
const
int64
kNullCapacitySlack
=
0
;
RandomDemand
demand
(
manager
.
num_nodes
(),
kDepot
,
FLAGS_vrp_use_deterministic_random_seed
);
demand
.
Initialize
();
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
demand
,
&
manager
](
int64
i
,
int64
j
)
{
return
demand
.
Demand
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kNullCapacitySlack
,
kVehicleCapacity
,
/*fix_start_cumul_to_zero=*/
true
,
kCapacity
);
// Adding time dimension constraints.
const
int64
kTimePerDemandUnit
=
300
;
const
int64
kHorizon
=
24
*
3600
;
ServiceTimePlusTransition
time
(
kTimePerDemandUnit
,
[
&
demand
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
demand
.
Demand
(
i
,
j
);
},
[
&
locations
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
locations
.
ManhattanTime
(
i
,
j
);
});
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
time
,
&
manager
](
int64
i
,
int64
j
)
{
return
time
.
Compute
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kHorizon
,
kHorizon
,
/*fix_start_cumul_to_zero=*/
false
,
kTime
);
const
RoutingDimension
&
time_dimension
=
routing
.
GetDimensionOrDie
(
kTime
);
// Adding disjoint time windows.
Solver
*
solver
=
routing
.
solver
();
ACMRandom
randomizer
(
GetSeed
(
FLAGS_vrp_use_deterministic_random_seed
));
for
(
int
order
=
1
;
order
<
manager
.
num_nodes
();
++
order
)
{
std
::
vector
<
int64
>
forbid_points
(
2
*
FLAGS_vrp_windows
,
0
);
for
(
int
i
=
0
;
i
<
forbid_points
.
size
();
++
i
)
{
forbid_points
[
i
]
=
randomizer
.
Uniform
(
kHorizon
);
}
std
::
sort
(
forbid_points
.
begin
(),
forbid_points
.
end
());
std
::
vector
<
int64
>
forbid_starts
(
1
,
0
);
std
::
vector
<
int64
>
forbid_ends
;
for
(
int
i
=
0
;
i
<
forbid_points
.
size
();
i
+=
2
)
{
forbid_ends
.
push_back
(
forbid_points
[
i
]);
forbid_starts
.
push_back
(
forbid_points
[
i
+
1
]);
}
forbid_ends
.
push_back
(
kHorizon
);
solver
->
AddConstraint
(
solver
->
MakeNotMemberCt
(
time_dimension
.
CumulVar
(
order
),
forbid_starts
,
forbid_ends
));
}
// Adding penalty costs to allow skipping orders.
const
int64
kPenalty
=
10000000
;
const
RoutingIndexManager
::
NodeIndex
kFirstNodeAfterDepot
(
1
);
for
(
RoutingIndexManager
::
NodeIndex
order
=
kFirstNodeAfterDepot
;
order
<
manager
.
num_nodes
();
++
order
)
{
std
::
vector
<
int64
>
orders
(
1
,
manager
.
NodeToIndex
(
order
));
routing
.
AddDisjunction
(
orders
,
kPenalty
);
}
// Adding same vehicle constraint costs for consecutive nodes.
if
(
FLAGS_vrp_use_same_vehicle_costs
)
{
std
::
vector
<
int64
>
group
;
for
(
RoutingIndexManager
::
NodeIndex
order
=
kFirstNodeAfterDepot
;
order
<
manager
.
num_nodes
();
++
order
)
{
group
.
push_back
(
manager
.
NodeToIndex
(
order
));
if
(
group
.
size
()
==
kMaxNodesPerGroup
)
{
routing
.
AddSoftSameVehicleConstraint
(
group
,
kSameVehicleCost
);
group
.
clear
();
}
}
if
(
!
group
.
empty
())
{
routing
.
AddSoftSameVehicleConstraint
(
group
,
kSameVehicleCost
);
}
}
// Solve, returns a solution if any (owned by RoutingModel).
RoutingSearchParameters
parameters
=
DefaultRoutingSearchParameters
();
CHECK
(
google
::
protobuf
::
TextFormat
::
MergeFromString
(
FLAGS_routing_search_parameters
,
&
parameters
));
const
Assignment
*
solution
=
routing
.
SolveWithParameters
(
parameters
);
if
(
solution
!=
nullptr
)
{
DisplayPlan
(
manager
,
routing
,
*
solution
,
FLAGS_vrp_use_same_vehicle_costs
,
kMaxNodesPerGroup
,
kSameVehicleCost
,
routing
.
GetDimensionOrDie
(
kCapacity
),
routing
.
GetDimensionOrDie
(
kTime
));
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/cvrptw.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Capacitated Vehicle Routing Problem with Time Windows (and optional orders).
// A description of the problem can be found here:
// http://en.wikipedia.org/wiki/Vehicle_routing_problem.
// The variant which is tackled by this model includes a capacity dimension,
// time windows and optional orders, with a penalty cost if orders are not
// performed. For the sake of simplicty, orders are randomly located and
// distances are computed using the Manhattan distance. Distances are assumed
// to be in meters and times in seconds.
#include
<vector>
#include
"examples/cpp/cvrptw_lib.h"
#include
"google/protobuf/text_format.h"
#include
"ortools/base/commandlineflags.h"
#include
"ortools/base/integral_types.h"
#include
"ortools/base/logging.h"
#include
"ortools/base/random.h"
#include
"ortools/constraint_solver/routing.h"
#include
"ortools/constraint_solver/routing_index_manager.h"
#include
"ortools/constraint_solver/routing_parameters.h"
#include
"ortools/constraint_solver/routing_parameters.pb.h"
using
operations_research
::
ACMRandom
;
using
operations_research
::
Assignment
;
using
operations_research
::
DefaultRoutingSearchParameters
;
using
operations_research
::
GetSeed
;
using
operations_research
::
LocationContainer
;
using
operations_research
::
RandomDemand
;
using
operations_research
::
RoutingDimension
;
using
operations_research
::
RoutingIndexManager
;
using
operations_research
::
RoutingModel
;
using
operations_research
::
RoutingNodeIndex
;
using
operations_research
::
RoutingSearchParameters
;
using
operations_research
::
ServiceTimePlusTransition
;
DEFINE_int32
(
vrp_orders
,
100
,
"Nodes in the problem."
);
DEFINE_int32
(
vrp_vehicles
,
20
,
"Size of Traveling Salesman Problem instance."
);
DEFINE_bool
(
vrp_use_deterministic_random_seed
,
false
,
"Use deterministic random seeds."
);
DEFINE_bool
(
vrp_use_same_vehicle_costs
,
false
,
"Use same vehicle costs in the routing model"
);
DEFINE_string
(
routing_search_parameters
,
""
,
"Text proto RoutingSearchParameters (possibly partial) that will "
"override the DefaultRoutingSearchParameters()"
);
const
char
*
kTime
=
"Time"
;
const
char
*
kCapacity
=
"Capacity"
;
const
int64
kMaxNodesPerGroup
=
10
;
const
int64
kSameVehicleCost
=
1000
;
int
main
(
int
argc
,
char
**
argv
)
{
gflags
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
CHECK_LT
(
0
,
FLAGS_vrp_orders
)
<<
"Specify an instance size greater than 0."
;
CHECK_LT
(
0
,
FLAGS_vrp_vehicles
)
<<
"Specify a non-null vehicle fleet size."
;
// VRP of size FLAGS_vrp_size.
// Nodes are indexed from 0 to FLAGS_vrp_orders, the starts and ends of
// the routes are at node 0.
const
RoutingIndexManager
::
NodeIndex
kDepot
(
0
);
RoutingIndexManager
manager
(
FLAGS_vrp_orders
+
1
,
FLAGS_vrp_vehicles
,
kDepot
);
RoutingModel
routing
(
manager
);
// Setting up locations.
const
int64
kXMax
=
100000
;
const
int64
kYMax
=
100000
;
const
int64
kSpeed
=
10
;
LocationContainer
locations
(
kSpeed
,
FLAGS_vrp_use_deterministic_random_seed
);
for
(
int
location
=
0
;
location
<=
FLAGS_vrp_orders
;
++
location
)
{
locations
.
AddRandomLocation
(
kXMax
,
kYMax
);
}
// Setting the cost function.
const
int
vehicle_cost
=
routing
.
RegisterTransitCallback
([
&
locations
,
&
manager
](
int64
i
,
int64
j
)
{
return
locations
.
ManhattanDistance
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
});
routing
.
SetArcCostEvaluatorOfAllVehicles
(
vehicle_cost
);
// Adding capacity dimension constraints.
const
int64
kVehicleCapacity
=
40
;
const
int64
kNullCapacitySlack
=
0
;
RandomDemand
demand
(
manager
.
num_nodes
(),
kDepot
,
FLAGS_vrp_use_deterministic_random_seed
);
demand
.
Initialize
();
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
demand
,
&
manager
](
int64
i
,
int64
j
)
{
return
demand
.
Demand
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kNullCapacitySlack
,
kVehicleCapacity
,
/*fix_start_cumul_to_zero=*/
true
,
kCapacity
);
// Adding time dimension constraints.
const
int64
kTimePerDemandUnit
=
300
;
const
int64
kHorizon
=
24
*
3600
;
ServiceTimePlusTransition
time
(
kTimePerDemandUnit
,
[
&
demand
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
demand
.
Demand
(
i
,
j
);
},
[
&
locations
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
locations
.
ManhattanTime
(
i
,
j
);
});
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
time
,
&
manager
](
int64
i
,
int64
j
)
{
return
time
.
Compute
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kHorizon
,
kHorizon
,
/*fix_start_cumul_to_zero=*/
true
,
kTime
);
const
RoutingDimension
&
time_dimension
=
routing
.
GetDimensionOrDie
(
kTime
);
// Adding time windows.
ACMRandom
randomizer
(
GetSeed
(
FLAGS_vrp_use_deterministic_random_seed
));
const
int64
kTWDuration
=
5
*
3600
;
for
(
int
order
=
1
;
order
<
manager
.
num_nodes
();
++
order
)
{
const
int64
start
=
randomizer
.
Uniform
(
kHorizon
-
kTWDuration
);
time_dimension
.
CumulVar
(
order
)
->
SetRange
(
start
,
start
+
kTWDuration
);
}
// Adding penalty costs to allow skipping orders.
const
int64
kPenalty
=
10000000
;
const
RoutingIndexManager
::
NodeIndex
kFirstNodeAfterDepot
(
1
);
for
(
RoutingIndexManager
::
NodeIndex
order
=
kFirstNodeAfterDepot
;
order
<
manager
.
num_nodes
();
++
order
)
{
std
::
vector
<
int64
>
orders
(
1
,
manager
.
NodeToIndex
(
order
));
routing
.
AddDisjunction
(
orders
,
kPenalty
);
}
// Adding same vehicle constraint costs for consecutive nodes.
if
(
FLAGS_vrp_use_same_vehicle_costs
)
{
std
::
vector
<
int64
>
group
;
for
(
RoutingIndexManager
::
NodeIndex
order
=
kFirstNodeAfterDepot
;
order
<
manager
.
num_nodes
();
++
order
)
{
group
.
push_back
(
manager
.
NodeToIndex
(
order
));
if
(
group
.
size
()
==
kMaxNodesPerGroup
)
{
routing
.
AddSoftSameVehicleConstraint
(
group
,
kSameVehicleCost
);
group
.
clear
();
}
}
if
(
!
group
.
empty
())
{
routing
.
AddSoftSameVehicleConstraint
(
group
,
kSameVehicleCost
);
}
}
// Solve, returns a solution if any (owned by RoutingModel).
RoutingSearchParameters
parameters
=
DefaultRoutingSearchParameters
();
CHECK
(
google
::
protobuf
::
TextFormat
::
MergeFromString
(
FLAGS_routing_search_parameters
,
&
parameters
));
const
Assignment
*
solution
=
routing
.
SolveWithParameters
(
parameters
);
if
(
solution
!=
nullptr
)
{
DisplayPlan
(
manager
,
routing
,
*
solution
,
FLAGS_vrp_use_same_vehicle_costs
,
kMaxNodesPerGroup
,
kSameVehicleCost
,
routing
.
GetDimensionOrDie
(
kCapacity
),
routing
.
GetDimensionOrDie
(
kTime
));
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/cvrptw_lib.h
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This header provides functions to help creating random instaces of the
// vehicle routing problem; random capacities and random time windows.
#ifndef OR_TOOLS_EXAMPLES_CVRPTW_LIB_H_
#define OR_TOOLS_EXAMPLES_CVRPTW_LIB_H_
#include
<memory>
#include
<set>
#include
"absl/strings/str_format.h"
#include
"ortools/base/logging.h"
#include
"ortools/base/random.h"
#include
"ortools/constraint_solver/routing.h"
namespace
operations_research
{
typedef
std
::
function
<
int64
(
RoutingNodeIndex
,
RoutingNodeIndex
)
>
RoutingNodeEvaluator2
;
// Random seed generator.
int32
GetSeed
(
bool
deterministic
);
// Location container, contains positions of orders and can be used to obtain
// Manhattan distances/times between locations.
class
LocationContainer
{
public:
LocationContainer
(
int64
speed
,
bool
use_deterministic_seed
);
void
AddLocation
(
int64
x
,
int64
y
)
{
locations_
.
push_back
(
Location
(
x
,
y
));
}
void
AddRandomLocation
(
int64
x_max
,
int64
y_max
);
void
AddRandomLocation
(
int64
x_max
,
int64
y_max
,
int
duplicates
);
int64
ManhattanDistance
(
RoutingIndexManager
::
NodeIndex
from
,
RoutingIndexManager
::
NodeIndex
to
)
const
;
int64
NegManhattanDistance
(
RoutingIndexManager
::
NodeIndex
from
,
RoutingIndexManager
::
NodeIndex
to
)
const
;
int64
ManhattanTime
(
RoutingIndexManager
::
NodeIndex
from
,
RoutingIndexManager
::
NodeIndex
to
)
const
;
bool
SameLocation
(
RoutingIndexManager
::
NodeIndex
node1
,
RoutingIndexManager
::
NodeIndex
node2
)
const
;
int64
SameLocationFromIndex
(
int64
node1
,
int64
node2
)
const
;
private:
class
Location
{
public:
Location
();
Location
(
int64
x
,
int64
y
);
int64
DistanceTo
(
const
Location
&
location
)
const
;
bool
IsAtSameLocation
(
const
Location
&
location
)
const
;
private:
static
int64
Abs
(
int64
value
);
int64
x_
;
int64
y_
;
};
MTRandom
randomizer_
;
const
int64
speed_
;
gtl
::
ITIVector
<
RoutingIndexManager
::
NodeIndex
,
Location
>
locations_
;
};
// Random demand.
class
RandomDemand
{
public:
RandomDemand
(
int
size
,
RoutingIndexManager
::
NodeIndex
depot
,
bool
use_deterministic_seed
);
void
Initialize
();
int64
Demand
(
RoutingIndexManager
::
NodeIndex
from
,
RoutingIndexManager
::
NodeIndex
to
)
const
;
private:
std
::
unique_ptr
<
int64
[]
>
demand_
;
const
int
size_
;
const
RoutingIndexManager
::
NodeIndex
depot_
;
const
bool
use_deterministic_seed_
;
};
// Service time (proportional to demand) + transition time callback.
class
ServiceTimePlusTransition
{
public:
ServiceTimePlusTransition
(
int64
time_per_demand_unit
,
RoutingNodeEvaluator2
demand
,
RoutingNodeEvaluator2
transition_time
);
int64
Compute
(
RoutingIndexManager
::
NodeIndex
from
,
RoutingIndexManager
::
NodeIndex
to
)
const
;
private:
const
int64
time_per_demand_unit_
;
RoutingNodeEvaluator2
demand_
;
RoutingNodeEvaluator2
transition_time_
;
};
// Stop service time + transition time callback.
class
StopServiceTimePlusTransition
{
public:
StopServiceTimePlusTransition
(
int64
stop_time
,
const
LocationContainer
&
location_container
,
RoutingNodeEvaluator2
transition_time
);
int64
Compute
(
RoutingIndexManager
::
NodeIndex
from
,
RoutingIndexManager
::
NodeIndex
to
)
const
;
private:
const
int64
stop_time_
;
const
LocationContainer
&
location_container_
;
RoutingNodeEvaluator2
demand_
;
RoutingNodeEvaluator2
transition_time_
;
};
// Route plan displayer.
// TODO(user): Move the display code to the routing library.
void
DisplayPlan
(
const
operations_research
::
RoutingIndexManager
&
manager
,
const
operations_research
::
RoutingModel
&
routing
,
const
operations_research
::
Assignment
&
plan
,
bool
use_same_vehicle_costs
,
int64
max_nodes_per_group
,
int64
same_vehicle_cost
,
const
operations_research
::
RoutingDimension
&
capacity_dimension
,
const
operations_research
::
RoutingDimension
&
time_dimension
);
using
NodeIndex
=
RoutingIndexManager
::
NodeIndex
;
int32
GetSeed
(
bool
deterministic
)
{
if
(
deterministic
)
{
return
ACMRandom
::
DeterministicSeed
();
}
else
{
return
ACMRandom
::
HostnamePidTimeSeed
();
}
}
LocationContainer
::
LocationContainer
(
int64
speed
,
bool
use_deterministic_seed
)
:
randomizer_
(
GetSeed
(
use_deterministic_seed
)),
speed_
(
speed
)
{
CHECK_LT
(
0
,
speed_
);
}
void
LocationContainer
::
AddRandomLocation
(
int64
x_max
,
int64
y_max
)
{
AddRandomLocation
(
x_max
,
y_max
,
1
);
}
void
LocationContainer
::
AddRandomLocation
(
int64
x_max
,
int64
y_max
,
int
duplicates
)
{
const
int64
x
=
randomizer_
.
Uniform
(
x_max
+
1
);
const
int64
y
=
randomizer_
.
Uniform
(
y_max
+
1
);
for
(
int
i
=
0
;
i
<
duplicates
;
++
i
)
{
AddLocation
(
x
,
y
);
}
}
int64
LocationContainer
::
ManhattanDistance
(
NodeIndex
from
,
NodeIndex
to
)
const
{
return
locations_
[
from
].
DistanceTo
(
locations_
[
to
]);
}
int64
LocationContainer
::
NegManhattanDistance
(
NodeIndex
from
,
NodeIndex
to
)
const
{
return
-
ManhattanDistance
(
from
,
to
);
}
int64
LocationContainer
::
ManhattanTime
(
NodeIndex
from
,
NodeIndex
to
)
const
{
return
ManhattanDistance
(
from
,
to
)
/
speed_
;
}
bool
LocationContainer
::
SameLocation
(
NodeIndex
node1
,
NodeIndex
node2
)
const
{
if
(
node1
<
locations_
.
size
()
&&
node2
<
locations_
.
size
())
{
return
locations_
[
node1
].
IsAtSameLocation
(
locations_
[
node2
]);
}
return
false
;
}
int64
LocationContainer
::
SameLocationFromIndex
(
int64
node1
,
int64
node2
)
const
{
// The direct conversion from constraint model indices to routing model
// nodes is correct because the depot is node 0.
// TODO(user): Fetch proper indices from routing model.
return
SameLocation
(
NodeIndex
(
node1
),
NodeIndex
(
node2
));
}
LocationContainer
::
Location
::
Location
()
:
x_
(
0
),
y_
(
0
)
{}
LocationContainer
::
Location
::
Location
(
int64
x
,
int64
y
)
:
x_
(
x
),
y_
(
y
)
{}
int64
LocationContainer
::
Location
::
DistanceTo
(
const
Location
&
location
)
const
{
return
Abs
(
x_
-
location
.
x_
)
+
Abs
(
y_
-
location
.
y_
);
}
bool
LocationContainer
::
Location
::
IsAtSameLocation
(
const
Location
&
location
)
const
{
return
x_
==
location
.
x_
&&
y_
==
location
.
y_
;
}
int64
LocationContainer
::
Location
::
Abs
(
int64
value
)
{
return
std
::
max
(
value
,
-
value
);
}
RandomDemand
::
RandomDemand
(
int
size
,
NodeIndex
depot
,
bool
use_deterministic_seed
)
:
size_
(
size
),
depot_
(
depot
),
use_deterministic_seed_
(
use_deterministic_seed
)
{
CHECK_LT
(
0
,
size_
);
}
void
RandomDemand
::
Initialize
()
{
const
int64
kDemandMax
=
5
;
const
int64
kDemandMin
=
1
;
demand_
=
absl
::
make_unique
<
int64
[]
>
(
size_
);
MTRandom
randomizer
(
GetSeed
(
use_deterministic_seed_
));
for
(
int
order
=
0
;
order
<
size_
;
++
order
)
{
if
(
order
==
depot_
)
{
demand_
[
order
]
=
0
;
}
else
{
demand_
[
order
]
=
kDemandMin
+
randomizer
.
Uniform
(
kDemandMax
-
kDemandMin
+
1
);
}
}
}
int64
RandomDemand
::
Demand
(
NodeIndex
from
,
NodeIndex
/*to*/
)
const
{
return
demand_
[
from
.
value
()];
}
ServiceTimePlusTransition
::
ServiceTimePlusTransition
(
int64
time_per_demand_unit
,
RoutingNodeEvaluator2
demand
,
RoutingNodeEvaluator2
transition_time
)
:
time_per_demand_unit_
(
time_per_demand_unit
),
demand_
(
std
::
move
(
demand
)),
transition_time_
(
std
::
move
(
transition_time
))
{}
int64
ServiceTimePlusTransition
::
Compute
(
NodeIndex
from
,
NodeIndex
to
)
const
{
return
time_per_demand_unit_
*
demand_
(
from
,
to
)
+
transition_time_
(
from
,
to
);
}
StopServiceTimePlusTransition
::
StopServiceTimePlusTransition
(
int64
stop_time
,
const
LocationContainer
&
location_container
,
RoutingNodeEvaluator2
transition_time
)
:
stop_time_
(
stop_time
),
location_container_
(
location_container
),
transition_time_
(
std
::
move
(
transition_time
))
{}
int64
StopServiceTimePlusTransition
::
Compute
(
NodeIndex
from
,
NodeIndex
to
)
const
{
return
location_container_
.
SameLocation
(
from
,
to
)
?
0
:
stop_time_
+
transition_time_
(
from
,
to
);
}
void
DisplayPlan
(
const
RoutingIndexManager
&
manager
,
const
RoutingModel
&
routing
,
const
operations_research
::
Assignment
&
plan
,
bool
use_same_vehicle_costs
,
int64
max_nodes_per_group
,
int64
same_vehicle_cost
,
const
operations_research
::
RoutingDimension
&
capacity_dimension
,
const
operations_research
::
RoutingDimension
&
time_dimension
)
{
// Display plan cost.
std
::
string
plan_output
=
absl
::
StrFormat
(
"Cost %d
\n
"
,
plan
.
ObjectiveValue
());
// Display dropped orders.
std
::
string
dropped
;
for
(
int64
order
=
0
;
order
<
routing
.
Size
();
++
order
)
{
if
(
routing
.
IsStart
(
order
)
||
routing
.
IsEnd
(
order
))
continue
;
if
(
plan
.
Value
(
routing
.
NextVar
(
order
))
==
order
)
{
if
(
dropped
.
empty
())
{
absl
::
StrAppendFormat
(
&
dropped
,
" %d"
,
manager
.
IndexToNode
(
order
).
value
());
}
else
{
absl
::
StrAppendFormat
(
&
dropped
,
", %d"
,
manager
.
IndexToNode
(
order
).
value
());
}
}
}
if
(
!
dropped
.
empty
())
{
plan_output
+=
"Dropped orders:"
+
dropped
+
"
\n
"
;
}
if
(
use_same_vehicle_costs
)
{
int
group_size
=
0
;
int64
group_same_vehicle_cost
=
0
;
std
::
set
<
int
>
visited
;
for
(
int64
order
=
0
;
order
<
routing
.
Size
();
++
order
)
{
if
(
routing
.
IsStart
(
order
)
||
routing
.
IsEnd
(
order
))
continue
;
++
group_size
;
visited
.
insert
(
plan
.
Value
(
routing
.
VehicleVar
(
order
)));
if
(
group_size
==
max_nodes_per_group
)
{
if
(
visited
.
size
()
>
1
)
{
group_same_vehicle_cost
+=
(
visited
.
size
()
-
1
)
*
same_vehicle_cost
;
}
group_size
=
0
;
visited
.
clear
();
}
}
if
(
visited
.
size
()
>
1
)
{
group_same_vehicle_cost
+=
(
visited
.
size
()
-
1
)
*
same_vehicle_cost
;
}
LOG
(
INFO
)
<<
"Same vehicle costs: "
<<
group_same_vehicle_cost
;
}
// Display actual output for each vehicle.
for
(
int
route_number
=
0
;
route_number
<
routing
.
vehicles
();
++
route_number
)
{
int64
order
=
routing
.
Start
(
route_number
);
absl
::
StrAppendFormat
(
&
plan_output
,
"Route %d: "
,
route_number
);
if
(
routing
.
IsEnd
(
plan
.
Value
(
routing
.
NextVar
(
order
))))
{
plan_output
+=
"Empty
\n
"
;
}
else
{
while
(
true
)
{
operations_research
::
IntVar
*
const
load_var
=
capacity_dimension
.
CumulVar
(
order
);
operations_research
::
IntVar
*
const
time_var
=
time_dimension
.
CumulVar
(
order
);
operations_research
::
IntVar
*
const
slack_var
=
routing
.
IsEnd
(
order
)
?
nullptr
:
time_dimension
.
SlackVar
(
order
);
if
(
slack_var
!=
nullptr
&&
plan
.
Contains
(
slack_var
))
{
absl
::
StrAppendFormat
(
&
plan_output
,
"%d Load(%d) Time(%d, %d) Slack(%d, %d)"
,
manager
.
IndexToNode
(
order
).
value
(),
plan
.
Value
(
load_var
),
plan
.
Min
(
time_var
),
plan
.
Max
(
time_var
),
plan
.
Min
(
slack_var
),
plan
.
Max
(
slack_var
));
}
else
{
absl
::
StrAppendFormat
(
&
plan_output
,
"%d Load(%d) Time(%d, %d)"
,
manager
.
IndexToNode
(
order
).
value
(),
plan
.
Value
(
load_var
),
plan
.
Min
(
time_var
),
plan
.
Max
(
time_var
));
}
if
(
routing
.
IsEnd
(
order
))
break
;
plan_output
+=
" -> "
;
order
=
plan
.
Value
(
routing
.
NextVar
(
order
));
}
plan_output
+=
"
\n
"
;
}
}
LOG
(
INFO
)
<<
plan_output
;
}
}
// namespace operations_research
#endif // OR_TOOLS_EXAMPLES_CVRPTW_LIB_H_
libs/or-tools-src-ubuntu/examples/cpp/cvrptw_with_breaks.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Capacitated Vehicle Routing Problem with Time Windows and Breaks.
// A description of the Capacitated Vehicle Routing Problem with Time Windows
// can be found here:
// http://en.wikipedia.org/wiki/Vehicle_routing_problem.
// The variant which is tackled by this model includes a capacity dimension,
// time windows and optional orders, with a penalty cost if orders are not
// performed. For the sake of simplicty, orders are randomly located and
// distances are computed using the Manhattan distance. Distances are assumed
// to be in meters and times in seconds.
// This variant also includes vehicle breaks which must happen during the day
// with two alternate breaks schemes: either a long break in the middle of the
// day or two smaller ones which can be taken during a longer period of the day.
#include
<vector>
#include
"absl/strings/str_cat.h"
#include
"examples/cpp/cvrptw_lib.h"
#include
"google/protobuf/text_format.h"
#include
"ortools/base/commandlineflags.h"
#include
"ortools/base/integral_types.h"
#include
"ortools/base/logging.h"
#include
"ortools/base/random.h"
#include
"ortools/constraint_solver/routing.h"
#include
"ortools/constraint_solver/routing_enums.pb.h"
#include
"ortools/constraint_solver/routing_index_manager.h"
#include
"ortools/constraint_solver/routing_parameters.h"
#include
"ortools/constraint_solver/routing_parameters.pb.h"
using
operations_research
::
ACMRandom
;
using
operations_research
::
Assignment
;
using
operations_research
::
DefaultRoutingSearchParameters
;
using
operations_research
::
FirstSolutionStrategy
;
using
operations_research
::
GetSeed
;
using
operations_research
::
IntervalVar
;
using
operations_research
::
LocationContainer
;
using
operations_research
::
RandomDemand
;
using
operations_research
::
RoutingDimension
;
using
operations_research
::
RoutingIndexManager
;
using
operations_research
::
RoutingModel
;
using
operations_research
::
RoutingNodeIndex
;
using
operations_research
::
RoutingSearchParameters
;
using
operations_research
::
ServiceTimePlusTransition
;
using
operations_research
::
Solver
;
DEFINE_int32
(
vrp_orders
,
100
,
"Nodes in the problem."
);
DEFINE_int32
(
vrp_vehicles
,
20
,
"Size of Traveling Salesman Problem instance."
);
DEFINE_bool
(
vrp_use_deterministic_random_seed
,
false
,
"Use deterministic random seeds."
);
DEFINE_string
(
routing_search_parameters
,
""
,
"Text proto RoutingSearchParameters (possibly partial) that will "
"override the DefaultRoutingSearchParameters()"
);
const
char
*
kTime
=
"Time"
;
const
char
*
kCapacity
=
"Capacity"
;
int
main
(
int
argc
,
char
**
argv
)
{
gflags
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
CHECK_LT
(
0
,
FLAGS_vrp_orders
)
<<
"Specify an instance size greater than 0."
;
CHECK_LT
(
0
,
FLAGS_vrp_vehicles
)
<<
"Specify a non-null vehicle fleet size."
;
// VRP of size FLAGS_vrp_size.
// Nodes are indexed from 0 to FLAGS_vrp_orders, the starts and ends of
// the routes are at node 0.
const
RoutingIndexManager
::
NodeIndex
kDepot
(
0
);
RoutingIndexManager
manager
(
FLAGS_vrp_orders
+
1
,
FLAGS_vrp_vehicles
,
kDepot
);
RoutingModel
routing
(
manager
);
RoutingSearchParameters
parameters
=
DefaultRoutingSearchParameters
();
CHECK
(
google
::
protobuf
::
TextFormat
::
MergeFromString
(
FLAGS_routing_search_parameters
,
&
parameters
));
parameters
.
set_first_solution_strategy
(
FirstSolutionStrategy
::
PARALLEL_CHEAPEST_INSERTION
);
// Setting up locations.
const
int64
kXMax
=
100000
;
const
int64
kYMax
=
100000
;
const
int64
kSpeed
=
10
;
LocationContainer
locations
(
kSpeed
,
FLAGS_vrp_use_deterministic_random_seed
);
for
(
int
location
=
0
;
location
<=
FLAGS_vrp_orders
;
++
location
)
{
locations
.
AddRandomLocation
(
kXMax
,
kYMax
);
}
// Setting the cost function.
const
int
vehicle_cost
=
routing
.
RegisterTransitCallback
([
&
locations
,
&
manager
](
int64
i
,
int64
j
)
{
return
locations
.
ManhattanDistance
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
});
routing
.
SetArcCostEvaluatorOfAllVehicles
(
vehicle_cost
);
// Adding capacity dimension constraints.
const
int64
kVehicleCapacity
=
40
;
const
int64
kNullCapacitySlack
=
0
;
RandomDemand
demand
(
manager
.
num_nodes
(),
kDepot
,
FLAGS_vrp_use_deterministic_random_seed
);
demand
.
Initialize
();
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
demand
,
&
manager
](
int64
i
,
int64
j
)
{
return
demand
.
Demand
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kNullCapacitySlack
,
kVehicleCapacity
,
/*fix_start_cumul_to_zero=*/
true
,
kCapacity
);
// Adding time dimension constraints.
const
int64
kTimePerDemandUnit
=
300
;
const
int64
kHorizon
=
24
*
3600
;
ServiceTimePlusTransition
time
(
kTimePerDemandUnit
,
[
&
demand
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
demand
.
Demand
(
i
,
j
);
},
[
&
locations
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
locations
.
ManhattanTime
(
i
,
j
);
});
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
time
,
&
manager
](
int64
i
,
int64
j
)
{
return
time
.
Compute
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kHorizon
,
kHorizon
,
/*fix_start_cumul_to_zero=*/
false
,
kTime
);
RoutingDimension
*
const
time_dimension
=
routing
.
GetMutableDimension
(
kTime
);
// Adding time windows.
ACMRandom
randomizer
(
GetSeed
(
FLAGS_vrp_use_deterministic_random_seed
));
const
int64
kTWDuration
=
5
*
3600
;
for
(
int
order
=
1
;
order
<
manager
.
num_nodes
();
++
order
)
{
const
int64
start
=
randomizer
.
Uniform
(
kHorizon
-
kTWDuration
);
time_dimension
->
CumulVar
(
order
)
->
SetRange
(
start
,
start
+
kTWDuration
);
routing
.
AddToAssignment
(
time_dimension
->
SlackVar
(
order
));
}
// Minimize time variables.
for
(
int
i
=
0
;
i
<
routing
.
Size
();
++
i
)
{
routing
.
AddVariableMinimizedByFinalizer
(
time_dimension
->
CumulVar
(
i
));
}
for
(
int
j
=
0
;
j
<
FLAGS_vrp_vehicles
;
++
j
)
{
routing
.
AddVariableMinimizedByFinalizer
(
time_dimension
->
CumulVar
(
routing
.
Start
(
j
)));
routing
.
AddVariableMinimizedByFinalizer
(
time_dimension
->
CumulVar
(
routing
.
End
(
j
)));
}
// Adding vehicle breaks:
// - 40min breaks between 11:00am and 1:00pm
// or
// - 2 x 30min breaks between 10:00am and 3:00pm, at least 1h apart
// First, fill service time vector.
std
::
vector
<
int64
>
service_times
(
routing
.
Size
());
for
(
int
node
=
0
;
node
<
routing
.
Size
();
node
++
)
{
if
(
node
>=
routing
.
nodes
())
{
service_times
[
node
]
=
0
;
}
else
{
const
RoutingIndexManager
::
NodeIndex
index
(
node
);
service_times
[
node
]
=
kTimePerDemandUnit
*
demand
.
Demand
(
index
,
index
);
}
}
const
std
::
vector
<
std
::
vector
<
int
>>
break_data
=
{
{
/*start_min*/
11
,
/*start_max*/
13
,
/*duration*/
2400
},
{
/*start_min*/
10
,
/*start_max*/
15
,
/*duration*/
1800
},
{
/*start_min*/
10
,
/*start_max*/
15
,
/*duration*/
1800
}};
Solver
*
const
solver
=
routing
.
solver
();
for
(
int
vehicle
=
0
;
vehicle
<
FLAGS_vrp_vehicles
;
++
vehicle
)
{
std
::
vector
<
IntervalVar
*>
breaks
;
for
(
int
i
=
0
;
i
<
break_data
.
size
();
++
i
)
{
IntervalVar
*
const
break_interval
=
solver
->
MakeFixedDurationIntervalVar
(
break_data
[
i
][
0
]
*
3600
,
break_data
[
i
][
1
]
*
3600
,
break_data
[
i
][
2
],
true
,
absl
::
StrCat
(
"Break "
,
i
,
" on vehicle "
,
vehicle
));
breaks
.
push_back
(
break_interval
);
}
// break1 performed iff break2 performed
solver
->
AddConstraint
(
solver
->
MakeEquality
(
breaks
[
1
]
->
PerformedExpr
(),
breaks
[
2
]
->
PerformedExpr
()));
// break2 start 1h after break1.
solver
->
AddConstraint
(
solver
->
MakeIntervalVarRelationWithDelay
(
breaks
[
2
],
Solver
::
STARTS_AFTER_END
,
breaks
[
1
],
3600
));
// break0 performed iff break2 unperformed
solver
->
AddConstraint
(
solver
->
MakeNonEquality
(
breaks
[
0
]
->
PerformedExpr
(),
breaks
[
2
]
->
PerformedExpr
()));
time_dimension
->
SetBreakIntervalsOfVehicle
(
std
::
move
(
breaks
),
vehicle
,
service_times
);
}
// Adding penalty costs to allow skipping orders.
const
int64
kPenalty
=
10000000
;
const
RoutingIndexManager
::
NodeIndex
kFirstNodeAfterDepot
(
1
);
for
(
RoutingIndexManager
::
NodeIndex
order
=
kFirstNodeAfterDepot
;
order
<
routing
.
nodes
();
++
order
)
{
std
::
vector
<
int64
>
orders
(
1
,
manager
.
NodeToIndex
(
order
));
routing
.
AddDisjunction
(
orders
,
kPenalty
);
}
// Solve, returns a solution if any (owned by RoutingModel).
const
Assignment
*
solution
=
routing
.
SolveWithParameters
(
parameters
);
if
(
solution
!=
nullptr
)
{
LOG
(
INFO
)
<<
"Breaks: "
;
for
(
const
auto
&
break_interval
:
solution
->
IntervalVarContainer
().
elements
())
{
if
(
break_interval
.
PerformedValue
()
==
1
)
{
LOG
(
INFO
)
<<
break_interval
.
Var
()
->
name
()
<<
" "
<<
break_interval
.
DebugString
();
}
else
{
LOG
(
INFO
)
<<
break_interval
.
Var
()
->
name
()
<<
" unperformed"
;
}
}
DisplayPlan
(
manager
,
routing
,
*
solution
,
false
,
0
,
0
,
routing
.
GetDimensionOrDie
(
kCapacity
),
routing
.
GetDimensionOrDie
(
kTime
));
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/cvrptw_with_refueling.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Capacitated Vehicle Routing Problem with Time Windows and refueling
// constraints.
// This is an extension to the model in cvrptw.cc so refer to that file for
// more information on the common part of the model. The model implemented here
// takes into account refueling constraints using a specific dimension: vehicles
// must visit certain nodes (refueling nodes) before the quantity of fuel
// reaches zero. Fuel consumption is proportional to the distance traveled.
#include
<vector>
#include
"examples/cpp/cvrptw_lib.h"
#include
"google/protobuf/text_format.h"
#include
"ortools/base/commandlineflags.h"
#include
"ortools/base/integral_types.h"
#include
"ortools/base/logging.h"
#include
"ortools/base/random.h"
#include
"ortools/constraint_solver/routing.h"
#include
"ortools/constraint_solver/routing_index_manager.h"
#include
"ortools/constraint_solver/routing_parameters.h"
#include
"ortools/constraint_solver/routing_parameters.pb.h"
using
operations_research
::
ACMRandom
;
using
operations_research
::
Assignment
;
using
operations_research
::
DefaultRoutingSearchParameters
;
using
operations_research
::
GetSeed
;
using
operations_research
::
LocationContainer
;
using
operations_research
::
RandomDemand
;
using
operations_research
::
RoutingDimension
;
using
operations_research
::
RoutingIndexManager
;
using
operations_research
::
RoutingModel
;
using
operations_research
::
RoutingNodeIndex
;
using
operations_research
::
RoutingSearchParameters
;
using
operations_research
::
ServiceTimePlusTransition
;
DEFINE_int32
(
vrp_orders
,
100
,
"Nodes in the problem."
);
DEFINE_int32
(
vrp_vehicles
,
20
,
"Size of Traveling Salesman Problem instance."
);
DEFINE_bool
(
vrp_use_deterministic_random_seed
,
false
,
"Use deterministic random seeds."
);
DEFINE_string
(
routing_search_parameters
,
""
,
"Text proto RoutingSearchParameters (possibly partial) that will "
"override the DefaultRoutingSearchParameters()"
);
const
char
*
kTime
=
"Time"
;
const
char
*
kCapacity
=
"Capacity"
;
const
char
*
kFuel
=
"Fuel"
;
// Returns true if node is a refueling node (based on node / refuel node ratio).
bool
IsRefuelNode
(
int64
node
)
{
const
int64
kRefuelNodeRatio
=
10
;
return
(
node
%
kRefuelNodeRatio
==
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
gflags
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
CHECK_LT
(
0
,
FLAGS_vrp_orders
)
<<
"Specify an instance size greater than 0."
;
CHECK_LT
(
0
,
FLAGS_vrp_vehicles
)
<<
"Specify a non-null vehicle fleet size."
;
// VRP of size FLAGS_vrp_size.
// Nodes are indexed from 0 to FLAGS_vrp_orders, the starts and ends of
// the routes are at node 0.
const
RoutingIndexManager
::
NodeIndex
kDepot
(
0
);
RoutingIndexManager
manager
(
FLAGS_vrp_orders
+
1
,
FLAGS_vrp_vehicles
,
kDepot
);
RoutingModel
routing
(
manager
);
// Setting up locations.
const
int64
kXMax
=
100000
;
const
int64
kYMax
=
100000
;
const
int64
kSpeed
=
10
;
LocationContainer
locations
(
kSpeed
,
FLAGS_vrp_use_deterministic_random_seed
);
for
(
int
location
=
0
;
location
<=
FLAGS_vrp_orders
;
++
location
)
{
locations
.
AddRandomLocation
(
kXMax
,
kYMax
);
}
// Setting the cost function.
const
int
vehicle_cost
=
routing
.
RegisterTransitCallback
([
&
locations
,
&
manager
](
int64
i
,
int64
j
)
{
return
locations
.
ManhattanDistance
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
});
routing
.
SetArcCostEvaluatorOfAllVehicles
(
vehicle_cost
);
// Adding capacity dimension constraints.
const
int64
kVehicleCapacity
=
40
;
const
int64
kNullCapacitySlack
=
0
;
RandomDemand
demand
(
manager
.
num_nodes
(),
kDepot
,
FLAGS_vrp_use_deterministic_random_seed
);
demand
.
Initialize
();
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
demand
,
&
manager
](
int64
i
,
int64
j
)
{
return
demand
.
Demand
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kNullCapacitySlack
,
kVehicleCapacity
,
/*fix_start_cumul_to_zero=*/
true
,
kCapacity
);
// Adding time dimension constraints.
const
int64
kTimePerDemandUnit
=
300
;
const
int64
kHorizon
=
24
*
3600
;
ServiceTimePlusTransition
time
(
kTimePerDemandUnit
,
[
&
demand
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
demand
.
Demand
(
i
,
j
);
},
[
&
locations
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
locations
.
ManhattanTime
(
i
,
j
);
});
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
time
,
&
manager
](
int64
i
,
int64
j
)
{
return
time
.
Compute
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kHorizon
,
kHorizon
,
/*fix_start_cumul_to_zero=*/
true
,
kTime
);
const
RoutingDimension
&
time_dimension
=
routing
.
GetDimensionOrDie
(
kTime
);
// Adding time windows.
ACMRandom
randomizer
(
GetSeed
(
FLAGS_vrp_use_deterministic_random_seed
));
const
int64
kTWDuration
=
5
*
3600
;
for
(
int
order
=
1
;
order
<
manager
.
num_nodes
();
++
order
)
{
if
(
!
IsRefuelNode
(
order
))
{
const
int64
start
=
randomizer
.
Uniform
(
kHorizon
-
kTWDuration
);
time_dimension
.
CumulVar
(
order
)
->
SetRange
(
start
,
start
+
kTWDuration
);
}
}
// Adding fuel dimension. This dimension consumes a quantity equal to the
// distance traveled. Only refuel nodes can make the quantity of dimension
// increase by letting slack variable replenish the fuel.
const
int64
kFuelCapacity
=
kXMax
+
kYMax
;
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
locations
,
&
manager
](
int64
i
,
int64
j
)
{
return
locations
.
NegManhattanDistance
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kFuelCapacity
,
kFuelCapacity
,
/*fix_start_cumul_to_zero=*/
false
,
kFuel
);
const
RoutingDimension
&
fuel_dimension
=
routing
.
GetDimensionOrDie
(
kFuel
);
for
(
int
order
=
0
;
order
<
routing
.
Size
();
++
order
)
{
// Only let slack free for refueling nodes.
if
(
!
IsRefuelNode
(
order
)
||
routing
.
IsStart
(
order
))
{
fuel_dimension
.
SlackVar
(
order
)
->
SetValue
(
0
);
}
// Needed to instantiate fuel quantity at each node.
routing
.
AddVariableMinimizedByFinalizer
(
fuel_dimension
.
CumulVar
(
order
));
}
// Adding penalty costs to allow skipping orders.
const
int64
kPenalty
=
100000
;
const
RoutingIndexManager
::
NodeIndex
kFirstNodeAfterDepot
(
1
);
for
(
RoutingIndexManager
::
NodeIndex
order
=
kFirstNodeAfterDepot
;
order
<
routing
.
nodes
();
++
order
)
{
std
::
vector
<
int64
>
orders
(
1
,
manager
.
NodeToIndex
(
order
));
routing
.
AddDisjunction
(
orders
,
kPenalty
);
}
// Solve, returns a solution if any (owned by RoutingModel).
RoutingSearchParameters
parameters
=
DefaultRoutingSearchParameters
();
CHECK
(
google
::
protobuf
::
TextFormat
::
MergeFromString
(
FLAGS_routing_search_parameters
,
&
parameters
));
const
Assignment
*
solution
=
routing
.
SolveWithParameters
(
parameters
);
if
(
solution
!=
nullptr
)
{
DisplayPlan
(
manager
,
routing
,
*
solution
,
/*use_same_vehicle_costs=*/
false
,
/*max_nodes_per_group=*/
0
,
/*same_vehicle_cost=*/
0
,
routing
.
GetDimensionOrDie
(
kCapacity
),
routing
.
GetDimensionOrDie
(
kTime
));
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
return
EXIT_SUCCESS
;
}
libs/or-tools-src-ubuntu/examples/cpp/cvrptw_with_resources.cc
0 → 100644
View file @
05f67e26
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Capacitated Vehicle Routing Problem with Time Windows and capacitated
// resources.
// This is an extension to the model in cvrptw.cc so refer to that file for
// more information on the common part of the model. The model implemented here
// limits the number of vehicles which can simultaneously leave or enter the
// depot due to limited resources (or capacity) available.
// TODO(user): The current model consumes resources even for vehicles with
// empty routes; fix this when we have an API on the cumulative constraints
// with variable demands.
#include
<vector>
#include
"examples/cpp/cvrptw_lib.h"
#include
"google/protobuf/text_format.h"
#include
"ortools/base/commandlineflags.h"
#include
"ortools/base/integral_types.h"
#include
"ortools/base/logging.h"
#include
"ortools/base/random.h"
#include
"ortools/constraint_solver/routing.h"
#include
"ortools/constraint_solver/routing_index_manager.h"
#include
"ortools/constraint_solver/routing_parameters.h"
#include
"ortools/constraint_solver/routing_parameters.pb.h"
using
operations_research
::
ACMRandom
;
using
operations_research
::
Assignment
;
using
operations_research
::
DefaultRoutingSearchParameters
;
using
operations_research
::
GetSeed
;
using
operations_research
::
IntervalVar
;
using
operations_research
::
IntVar
;
using
operations_research
::
LocationContainer
;
using
operations_research
::
RandomDemand
;
using
operations_research
::
RoutingDimension
;
using
operations_research
::
RoutingIndexManager
;
using
operations_research
::
RoutingModel
;
using
operations_research
::
RoutingNodeIndex
;
using
operations_research
::
RoutingSearchParameters
;
using
operations_research
::
ServiceTimePlusTransition
;
using
operations_research
::
Solver
;
DEFINE_int32
(
vrp_orders
,
100
,
"Nodes in the problem."
);
DEFINE_int32
(
vrp_vehicles
,
20
,
"Size of Traveling Salesman Problem instance."
);
DEFINE_bool
(
vrp_use_deterministic_random_seed
,
false
,
"Use deterministic random seeds."
);
DEFINE_string
(
routing_search_parameters
,
""
,
"Text proto RoutingSearchParameters (possibly partial) that will "
"override the DefaultRoutingSearchParameters()"
);
const
char
*
kTime
=
"Time"
;
const
char
*
kCapacity
=
"Capacity"
;
int
main
(
int
argc
,
char
**
argv
)
{
gflags
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
CHECK_LT
(
0
,
FLAGS_vrp_orders
)
<<
"Specify an instance size greater than 0."
;
CHECK_LT
(
0
,
FLAGS_vrp_vehicles
)
<<
"Specify a non-null vehicle fleet size."
;
// VRP of size FLAGS_vrp_size.
// Nodes are indexed from 0 to FLAGS_vrp_orders, the starts and ends of
// the routes are at node 0.
const
RoutingIndexManager
::
NodeIndex
kDepot
(
0
);
RoutingIndexManager
manager
(
FLAGS_vrp_orders
+
1
,
FLAGS_vrp_vehicles
,
kDepot
);
RoutingModel
routing
(
manager
);
// Setting up locations.
const
int64
kXMax
=
100000
;
const
int64
kYMax
=
100000
;
const
int64
kSpeed
=
10
;
LocationContainer
locations
(
kSpeed
,
FLAGS_vrp_use_deterministic_random_seed
);
for
(
int
location
=
0
;
location
<=
FLAGS_vrp_orders
;
++
location
)
{
locations
.
AddRandomLocation
(
kXMax
,
kYMax
);
}
// Setting the cost function.
const
int
vehicle_cost
=
routing
.
RegisterTransitCallback
([
&
locations
,
&
manager
](
int64
i
,
int64
j
)
{
return
locations
.
ManhattanDistance
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
});
routing
.
SetArcCostEvaluatorOfAllVehicles
(
vehicle_cost
);
// Adding capacity dimension constraints.
const
int64
kVehicleCapacity
=
40
;
const
int64
kNullCapacitySlack
=
0
;
RandomDemand
demand
(
manager
.
num_nodes
(),
kDepot
,
FLAGS_vrp_use_deterministic_random_seed
);
demand
.
Initialize
();
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
demand
,
&
manager
](
int64
i
,
int64
j
)
{
return
demand
.
Demand
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kNullCapacitySlack
,
kVehicleCapacity
,
/*fix_start_cumul_to_zero=*/
true
,
kCapacity
);
// Adding time dimension constraints.
const
int64
kTimePerDemandUnit
=
300
;
const
int64
kHorizon
=
24
*
3600
;
ServiceTimePlusTransition
time
(
kTimePerDemandUnit
,
[
&
demand
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
demand
.
Demand
(
i
,
j
);
},
[
&
locations
](
RoutingNodeIndex
i
,
RoutingNodeIndex
j
)
{
return
locations
.
ManhattanTime
(
i
,
j
);
});
routing
.
AddDimension
(
routing
.
RegisterTransitCallback
([
&
time
,
&
manager
](
int64
i
,
int64
j
)
{
return
time
.
Compute
(
manager
.
IndexToNode
(
i
),
manager
.
IndexToNode
(
j
));
}),
kHorizon
,
kHorizon
,
/*fix_start_cumul_to_zero=*/
false
,
kTime
);
const
RoutingDimension
&
time_dimension
=
routing
.
GetDimensionOrDie
(
kTime
);
// Adding time windows.
ACMRandom
randomizer
(
GetSeed
(
FLAGS_vrp_use_deterministic_random_seed
));
const
int64
kTWDuration
=
5
*
3600
;
for
(
int
order
=
1
;
order
<
manager
.
num_nodes
();
++
order
)
{
const
int64
start
=
randomizer
.
Uniform
(
kHorizon
-
kTWDuration
);
time_dimension
.
CumulVar
(
order
)
->
SetRange
(
start
,
start
+
kTWDuration
);
}
// Adding resource constraints at the depot (start and end location of
// routes).
std
::
vector
<
IntVar
*>
start_end_times
;
for
(
int
i
=
0
;
i
<
FLAGS_vrp_vehicles
;
++
i
)
{
start_end_times
.
push_back
(
time_dimension
.
CumulVar
(
routing
.
End
(
i
)));
start_end_times
.
push_back
(
time_dimension
.
CumulVar
(
routing
.
Start
(
i
)));
}
// Build corresponding time intervals.
const
int64
kVehicleSetup
=
180
;
Solver
*
const
solver
=
routing
.
solver
();
std
::
vector
<
IntervalVar
*>
intervals
;
solver
->
MakeFixedDurationIntervalVarArray
(
start_end_times
,
kVehicleSetup
,
"depot_interval"
,
&
intervals
);
// Constrain the number of maximum simultaneous intervals at depot.
const
int64
kDepotCapacity
=
5
;
std
::
vector
<
int64
>
depot_usage
(
start_end_times
.
size
(),
1
);
solver
->
AddConstraint
(
solver
->
MakeCumulative
(
intervals
,
depot_usage
,
kDepotCapacity
,
"depot"
));
// Instantiate route start and end times to produce feasible times.
for
(
int
i
=
0
;
i
<
start_end_times
.
size
();
++
i
)
{
routing
.
AddVariableMinimizedByFinalizer
(
start_end_times
[
i
]);
}
// Adding penalty costs to allow skipping orders.
const
int64
kPenalty
=
100000
;
const
RoutingIndexManager
::
NodeIndex
kFirstNodeAfterDepot
(
1
);
for
(
RoutingIndexManager
::
NodeIndex
order
=
kFirstNodeAfterDepot
;
order
<
manager
.
num_nodes
();
++
order
)
{
std
::
vector
<
int64
>
orders
(
1
,
manager
.
NodeToIndex
(
order
));
routing
.
AddDisjunction
(
orders
,
kPenalty
);
}
// Solve, returns a solution if any (owned by RoutingModel).
RoutingSearchParameters
parameters
=
DefaultRoutingSearchParameters
();
CHECK
(
google
::
protobuf
::
TextFormat
::
MergeFromString
(
FLAGS_routing_search_parameters
,
&
parameters
));
const
Assignment
*
solution
=
routing
.
SolveWithParameters
(
parameters
);
if
(
solution
!=
nullptr
)
{
DisplayPlan
(
manager
,
routing
,
*
solution
,
/*use_same_vehicle_costs=*/
false
,
/*max_nodes_per_group=*/
0
,
/*same_vehicle_cost=*/
0
,
routing
.
GetDimensionOrDie
(
kCapacity
),
routing
.
GetDimensionOrDie
(
kTime
));
}
else
{
LOG
(
INFO
)
<<
"No solution found."
;
}
return
EXIT_SUCCESS
;
}
Prev
1
2
3
4
5
…
50
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment