ortools.math_opt.python.solver_resources

Configures solver resources.

 1# Copyright 2010-2024 Google LLC
 2# Licensed under the Apache License, Version 2.0 (the "License");
 3# you may not use this file except in compliance with the License.
 4# You may obtain a copy of the License at
 5#
 6#     http://www.apache.org/licenses/LICENSE-2.0
 7#
 8# Unless required by applicable law or agreed to in writing, software
 9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14"""Configures solver resources."""
15
16import dataclasses
17from typing import Optional
18
19from ortools.math_opt import rpc_pb2
20
21
22@dataclasses.dataclass
23class SolverResources:
24    """The hints on the resources a remote solve is expected to use.
25
26    These parameters are hints and may be ignored by the remote server (in
27    particular in case of solve in a local subprocess, for example).
28
29    When using remote_solve() and remote_compute_infeasible_subsystem(), these
30    hints are mostly optional as some defaults will be computed based on the other
31    parameters.
32
33    When using remote_streaming_solve() these hints are used to dimension the
34    resources available during the execution of every action; thus it is
35    recommended to set them.
36
37    MOE:begin_intracomment_strip
38
39    The go/uoss server will use these parameters to do a bin-packing of all
40    requests. Parameter cpu is a soft-limit, the solve may still be able to use
41    more CPUs.  The ram parameter is an hard-limit, an out-of-memory error will
42    occur if the solve attempts to use more memory.
43
44    MOE:end_intracomment_strip
45
46    Attributes:
47      cpu: The number of solver threads that are expected to actually execute in
48        parallel. Must be finite and >0.0.  For example a value of 3.0 means that
49        if the solver has 5 threads that can execute we expect at least 3 of these
50        threads to be scheduled in parallel for any given time slice of the
51        operating system scheduler.  A fractional value indicates that we don't
52        expect the operating system to constantly schedule the solver's work. For
53        example with 0.5 we would expect the solver's threads to be scheduled half
54        the time.  This parameter is usually used in conjunction with
55        SolveParameters.threads. For some solvers like Gurobi it makes sense to
56        use SolverResources.cpu = SolveParameters.threads. For other solvers like
57        CP-SAT, it may makes sense to use a value lower than the number of threads
58        as not all threads may be ready to be scheduled at the same time. It is
59        better to consult each solver documentation to set this parameter.  Note
60        that if the SolveParameters.threads is not set then this parameter should
61        also be left unset.
62      ram: The limit of RAM for the solve in bytes. Must be finite and >=1.0 (even
63        though it should in practice be much larger).
64    """
65
66    cpu: Optional[float] = None
67    ram: Optional[float] = None
68
69    def to_proto(self) -> rpc_pb2.SolverResourcesProto:
70        return rpc_pb2.SolverResourcesProto(cpu=self.cpu, ram=self.ram)
@dataclasses.dataclass
class SolverResources:
23@dataclasses.dataclass
24class SolverResources:
25    """The hints on the resources a remote solve is expected to use.
26
27    These parameters are hints and may be ignored by the remote server (in
28    particular in case of solve in a local subprocess, for example).
29
30    When using remote_solve() and remote_compute_infeasible_subsystem(), these
31    hints are mostly optional as some defaults will be computed based on the other
32    parameters.
33
34    When using remote_streaming_solve() these hints are used to dimension the
35    resources available during the execution of every action; thus it is
36    recommended to set them.
37
38    MOE:begin_intracomment_strip
39
40    The go/uoss server will use these parameters to do a bin-packing of all
41    requests. Parameter cpu is a soft-limit, the solve may still be able to use
42    more CPUs.  The ram parameter is an hard-limit, an out-of-memory error will
43    occur if the solve attempts to use more memory.
44
45    MOE:end_intracomment_strip
46
47    Attributes:
48      cpu: The number of solver threads that are expected to actually execute in
49        parallel. Must be finite and >0.0.  For example a value of 3.0 means that
50        if the solver has 5 threads that can execute we expect at least 3 of these
51        threads to be scheduled in parallel for any given time slice of the
52        operating system scheduler.  A fractional value indicates that we don't
53        expect the operating system to constantly schedule the solver's work. For
54        example with 0.5 we would expect the solver's threads to be scheduled half
55        the time.  This parameter is usually used in conjunction with
56        SolveParameters.threads. For some solvers like Gurobi it makes sense to
57        use SolverResources.cpu = SolveParameters.threads. For other solvers like
58        CP-SAT, it may makes sense to use a value lower than the number of threads
59        as not all threads may be ready to be scheduled at the same time. It is
60        better to consult each solver documentation to set this parameter.  Note
61        that if the SolveParameters.threads is not set then this parameter should
62        also be left unset.
63      ram: The limit of RAM for the solve in bytes. Must be finite and >=1.0 (even
64        though it should in practice be much larger).
65    """
66
67    cpu: Optional[float] = None
68    ram: Optional[float] = None
69
70    def to_proto(self) -> rpc_pb2.SolverResourcesProto:
71        return rpc_pb2.SolverResourcesProto(cpu=self.cpu, ram=self.ram)

The hints on the resources a remote solve is expected to use.

These parameters are hints and may be ignored by the remote server (in particular in case of solve in a local subprocess, for example).

When using remote_solve() and remote_compute_infeasible_subsystem(), these hints are mostly optional as some defaults will be computed based on the other parameters.

When using remote_streaming_solve() these hints are used to dimension the resources available during the execution of every action; thus it is recommended to set them.

MOE:begin_intracomment_strip

The go/uoss server will use these parameters to do a bin-packing of all requests. Parameter cpu is a soft-limit, the solve may still be able to use more CPUs. The ram parameter is an hard-limit, an out-of-memory error will occur if the solve attempts to use more memory.

MOE:end_intracomment_strip

Attributes:
  • cpu: The number of solver threads that are expected to actually execute in parallel. Must be finite and >0.0. For example a value of 3.0 means that if the solver has 5 threads that can execute we expect at least 3 of these threads to be scheduled in parallel for any given time slice of the operating system scheduler. A fractional value indicates that we don't expect the operating system to constantly schedule the solver's work. For example with 0.5 we would expect the solver's threads to be scheduled half the time. This parameter is usually used in conjunction with SolveParameters.threads. For some solvers like Gurobi it makes sense to use SolverResources.cpu = SolveParameters.threads. For other solvers like CP-SAT, it may makes sense to use a value lower than the number of threads as not all threads may be ready to be scheduled at the same time. It is better to consult each solver documentation to set this parameter. Note that if the SolveParameters.threads is not set then this parameter should also be left unset.
  • ram: The limit of RAM for the solve in bytes. Must be finite and >=1.0 (even though it should in practice be much larger).
SolverResources(cpu: Optional[float] = None, ram: Optional[float] = None)
cpu: Optional[float] = None
ram: Optional[float] = None
def to_proto(self) -> ortools.math_opt.rpc_pb2.SolverResourcesProto:
70    def to_proto(self) -> rpc_pb2.SolverResourcesProto:
71        return rpc_pb2.SolverResourcesProto(cpu=self.cpu, ram=self.ram)