|
| 1 | +// Copyright 2010-2025 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 | +package com.google.ortools.mathopt; |
| 15 | + |
| 16 | +import com.google.common.base.Preconditions; |
| 17 | + |
| 18 | +/** |
| 19 | + * Additional objectives for optimization models with multiple hierarchical objectives. |
| 20 | + * |
| 21 | + * <p>Unlike the primary objective, auxiliary objectives have an id and can be deleted. Users who do |
| 22 | + * not need these features (most users) can program against the parent class {@link Objective} |
| 23 | + * instead, allowing more uniform treatment of the primary and auxiliary objectives. |
| 24 | + */ |
| 25 | +public final class AuxiliaryObjective extends Objective implements ModelElement { |
| 26 | + private final long id; |
| 27 | + private boolean deleted = false; |
| 28 | + |
| 29 | + AuxiliaryObjective( |
| 30 | + long priority, String name, long id, OnChangeListener listener, ModelId modelId) { |
| 31 | + super(priority, name, listener, modelId); |
| 32 | + Preconditions.checkArgument(id >= 0, "id should be nonnegative, was %s", id); |
| 33 | + this.id = id; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public long getId() { |
| 38 | + return id; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public boolean isDeleted() { |
| 43 | + return deleted; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String toString() { |
| 48 | + String label = "Objective " + id; |
| 49 | + if (getName().isEmpty()) { |
| 50 | + return label; |
| 51 | + } else { |
| 52 | + return getName() + " (" + label + ")"; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * To be called when this objective is deleted from the model, ensures future modifications result |
| 58 | + * in an error. It is an error to call this on the primary objective. |
| 59 | + */ |
| 60 | + void markDeleted() { |
| 61 | + deleted = true; |
| 62 | + } |
| 63 | +} |
0 commit comments