quicopt 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -29
- data/lib/quicopt/client.rb +130 -48
- data/lib/quicopt/indicators.rb +9 -8
- data/lib/quicopt/model.rb +82 -63
- data/lib/quicopt/structured.rb +40 -39
- data/lib/quicopt/version.rb +1 -1
- data/lib/quicopt/wire.rb +261 -7
- data/lib/quicopt.rb +1 -1
- metadata +1 -1
data/lib/quicopt/model.rb
CHANGED
|
@@ -47,7 +47,7 @@ module Quicopt
|
|
|
47
47
|
value == value.to_i ? value.to_i.to_s : value.to_s
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
# One term, dropping a unit coefficient (
|
|
50
|
+
# One term, dropping a unit coefficient (<tt>x</tt>, <tt>-x</tt>, <tt>3*x</tt>).
|
|
51
51
|
def term(coefficient, factors)
|
|
52
52
|
return factors if coefficient == 1.0
|
|
53
53
|
return "-#{factors}" if coefficient == -1.0
|
|
@@ -63,7 +63,7 @@ module Quicopt
|
|
|
63
63
|
# what lets +3 * x+ parse: Ruby hands the multiplication back to us rather than
|
|
64
64
|
# failing in +Integer#*+.
|
|
65
65
|
module Operators
|
|
66
|
-
# @return [Expression]
|
|
66
|
+
# @return [Expression] <tt>self + other</tt>
|
|
67
67
|
def +(other)
|
|
68
68
|
to_expr.plus(Expression.cast(other))
|
|
69
69
|
end
|
|
@@ -148,9 +148,10 @@ module Quicopt
|
|
|
148
148
|
# Builds an *equality row*, not a boolean — +x == 5+ is a constraint.
|
|
149
149
|
#
|
|
150
150
|
# A non-numeric, non-expression operand falls back to identity, so an
|
|
151
|
-
# incidental comparison (
|
|
152
|
-
# of raising. Hash and Set membership are unaffected
|
|
153
|
-
#
|
|
151
|
+
# incidental comparison (<tt>x == nil</tt>, <tt>x == "x"</tt>) still
|
|
152
|
+
# answers +false+ instead of raising. Hash and Set membership are unaffected
|
|
153
|
+
# either way: they key off <tt>hash</tt>/<tt>eql?</tt>, which +Variable+
|
|
154
|
+
# defines separately.
|
|
154
155
|
#
|
|
155
156
|
# @return [Constraint, Boolean]
|
|
156
157
|
def ==(other)
|
|
@@ -171,7 +172,8 @@ module Quicopt
|
|
|
171
172
|
end
|
|
172
173
|
|
|
173
174
|
# Lets Ruby fold a numeric on the left into an expression, so +3 * x+ and
|
|
174
|
-
# +10 - x+ dispatch here instead of failing inside
|
|
175
|
+
# +10 - x+ dispatch here instead of failing inside
|
|
176
|
+
# <tt>Integer</tt>/<tt>Float</tt>.
|
|
175
177
|
#
|
|
176
178
|
# @param other [Numeric]
|
|
177
179
|
# @return [Array(Expression, Expression)]
|
|
@@ -272,11 +274,11 @@ module Quicopt
|
|
|
272
274
|
end
|
|
273
275
|
|
|
274
276
|
# A polynomial of degree ≤ 2 over a model's variables, always kept in collected
|
|
275
|
-
# form:
|
|
277
|
+
# form: <tt>constant + Σ cᵢ·xᵢ + Σ qᵢⱼ·xᵢxⱼ</tt>.
|
|
276
278
|
#
|
|
277
|
-
# A value read from a permutation (
|
|
278
|
-
# (
|
|
279
|
-
# linear, +x * dist[i, j]+ quadratic.
|
|
279
|
+
# A value read from a permutation (<tt>route.item_at(0)</tt>) or a lookup
|
|
280
|
+
# table (<tt>dist[i, j]</tt>) counts as one factor, like a variable:
|
|
281
|
+
# +2 * dist[i, j]+ is linear, +x * dist[i, j]+ quadratic.
|
|
280
282
|
#
|
|
281
283
|
# Instances are immutable — every operator returns a new expression — so an
|
|
282
284
|
# expression can be shared, reused and stored without aliasing surprises.
|
|
@@ -335,7 +337,7 @@ module Quicopt
|
|
|
335
337
|
degree.zero?
|
|
336
338
|
end
|
|
337
339
|
|
|
338
|
-
# @return [Expression]
|
|
340
|
+
# @return [Expression] <tt>self + other</tt>
|
|
339
341
|
def plus(other)
|
|
340
342
|
Expression.new(constant: @constant + other.constant,
|
|
341
343
|
linear: Expression.merge(@linear, other.linear),
|
|
@@ -401,7 +403,7 @@ module Quicopt
|
|
|
401
403
|
seen
|
|
402
404
|
end
|
|
403
405
|
|
|
404
|
-
# Renders the polynomial, e.g. +3
|
|
406
|
+
# Renders the polynomial, e.g. <tt>-4 + 3\*x + 2\*y</tt>.
|
|
405
407
|
def to_s
|
|
406
408
|
terms = []
|
|
407
409
|
terms << Format.number(@constant) unless @constant.zero?
|
|
@@ -547,6 +549,9 @@ module Quicopt
|
|
|
547
549
|
@partitions = []
|
|
548
550
|
@tables = []
|
|
549
551
|
@blocks = []
|
|
552
|
+
# The name of the block each blocked variable is in, by identity, so that
|
|
553
|
+
# declaring a block costs time in its own size, not in the model's.
|
|
554
|
+
@block_of = {}.compare_by_identity
|
|
550
555
|
@structured = {}
|
|
551
556
|
@interned = {}
|
|
552
557
|
@reads = 0
|
|
@@ -614,10 +619,11 @@ module Quicopt
|
|
|
614
619
|
@by_name[name.to_s]
|
|
615
620
|
end
|
|
616
621
|
|
|
617
|
-
# Declare an
|
|
618
|
-
# slot, both numbered from 0. For a route the items are
|
|
619
|
-
# slots are the visits, so slot 0 holds the first stop
|
|
620
|
-
# the items are the jobs and the slots the positions
|
|
622
|
+
# Declare an ordering as a decision: +n_items+ items placed in as many
|
|
623
|
+
# slots, one item per slot, both numbered from 0. For a route the items are
|
|
624
|
+
# the stops and the slots are the visits, so slot 0 holds the first stop
|
|
625
|
+
# visited; for a machine the items are the jobs and the slots the positions
|
|
626
|
+
# in its queue.
|
|
621
627
|
#
|
|
622
628
|
# Five stops along a road; find the shortest route through all of them that
|
|
623
629
|
# visits stop 3 before stop 0:
|
|
@@ -629,7 +635,7 @@ module Quicopt
|
|
|
629
635
|
# route.precede(3, 0) # stop 3 is visited before stop 0
|
|
630
636
|
# m.minimize((0...4).sum { |k| dist[route.item_at(k), route.item_at(k + 1)] })
|
|
631
637
|
#
|
|
632
|
-
# result = Quicopt::Client.new.solve(m)
|
|
638
|
+
# result = Quicopt::Client.new(api_key: "…").solve(m)
|
|
633
639
|
# result.objective # 4.0
|
|
634
640
|
# result.structures["route"].item_at # [3, 1, 4, 2, 0]: the stops in the order visited
|
|
635
641
|
#
|
|
@@ -639,31 +645,33 @@ module Quicopt
|
|
|
639
645
|
# @param name [String, Symbol] identifies the permutation in the result, as
|
|
640
646
|
# +result.structures[name]+; shares one namespace with the model's variables
|
|
641
647
|
# and lookup tables
|
|
642
|
-
# @param
|
|
648
|
+
# @param n_items [Integer] the number of items, at least 2
|
|
643
649
|
# @param start [Array<Integer>, nil] the arrangement the service's search
|
|
644
650
|
# starts from: +start[i]+ is the slot of item +i+, so it holds each of
|
|
645
|
-
# +0...
|
|
651
|
+
# +0...n_items+ once. Left out, item +i+ starts in slot +i+.
|
|
646
652
|
# @return [Permutation]
|
|
647
|
-
# @raise [TypeError] if +
|
|
648
|
-
# @raise [ArgumentError] if +
|
|
649
|
-
# not an arrangement of +0...
|
|
650
|
-
def permutation(name,
|
|
651
|
-
|
|
652
|
-
|
|
653
|
+
# @raise [TypeError] if +n_items+ is not an Integer
|
|
654
|
+
# @raise [ArgumentError] if +n_items+ is below 2, the name is taken, or
|
|
655
|
+
# +start+ is not an arrangement of +0...n_items+
|
|
656
|
+
def permutation(name, n_items, start: nil)
|
|
657
|
+
unless n_items.is_a?(Integer)
|
|
658
|
+
raise TypeError, "a permutation's number of items is a whole number, got #{n_items.inspect}"
|
|
659
|
+
end
|
|
660
|
+
raise ArgumentError, "a permutation has at least 2 items, got #{n_items}" if n_items < 2
|
|
653
661
|
|
|
654
662
|
nm = claim(name)
|
|
655
|
-
unless start.nil? || (start.is_a?(Array) && start.all?(Integer) && start.sort == (0...
|
|
656
|
-
raise ArgumentError, "permutation #{nm.inspect}: start must hold each of 0 to #{
|
|
663
|
+
unless start.nil? || (start.is_a?(Array) && start.all?(Integer) && start.sort == (0...n_items).to_a)
|
|
664
|
+
raise ArgumentError, "permutation #{nm.inspect}: start must hold each of 0 to #{n_items - 1} once " \
|
|
657
665
|
"(start[i] is the slot item i starts in)"
|
|
658
666
|
end
|
|
659
|
-
permutation = Permutation.new(self, nm,
|
|
667
|
+
permutation = Permutation.new(self, nm, n_items, start&.dup)
|
|
660
668
|
@permutations << permutation
|
|
661
669
|
@structured[nm] = permutation
|
|
662
670
|
end
|
|
663
671
|
|
|
664
|
-
# Declare an assignment of +
|
|
665
|
-
# team each person joins, which truck each parcel rides in. Items and
|
|
666
|
-
# are both numbered from 0, and a part may stay empty.
|
|
672
|
+
# Declare an assignment of +n_items+ items to +n_parts+ parts as a decision:
|
|
673
|
+
# which team each person joins, which truck each parcel rides in. Items and
|
|
674
|
+
# parts are both numbered from 0, and a part may stay empty.
|
|
667
675
|
#
|
|
668
676
|
# Six players of strengths 7, 5, 4, 3, 2 and 1 form a team of four and a
|
|
669
677
|
# team of two, as evenly matched as possible:
|
|
@@ -675,34 +683,39 @@ module Quicopt
|
|
|
675
683
|
# m.add(teams.size_of(0) == 4) # team 0 has four players
|
|
676
684
|
# m.minimize(Quicopt.max(total[0], total[1]))
|
|
677
685
|
#
|
|
678
|
-
# result = Quicopt::Client.new.solve(m)
|
|
686
|
+
# result = Quicopt::Client.new(api_key: "…").solve(m)
|
|
679
687
|
# result.objective # 11.0
|
|
680
688
|
# result.structures["teams"].part_of # [1, 0, 1, 0, 0, 0]: players 0 and 2 form team 1
|
|
681
689
|
#
|
|
682
690
|
# @param name [String, Symbol] identifies the partition in the result, as
|
|
683
691
|
# +result.structures[name]+; shares one namespace with the model's
|
|
684
692
|
# variables, permutations and lookup tables
|
|
685
|
-
# @param
|
|
686
|
-
# @param
|
|
693
|
+
# @param n_items [Integer] the number of items, at least 1
|
|
694
|
+
# @param n_parts [Integer] the number of parts, at least 2
|
|
687
695
|
# @param start [Array<Integer>, nil] the assignment the service's search
|
|
688
|
-
# starts from: +start[i]+ is the part of item +i+, one of +0...
|
|
689
|
-
# Left out, item +i+ starts in part +i %
|
|
696
|
+
# starts from: +start[i]+ is the part of item +i+, one of +0...n_parts+.
|
|
697
|
+
# Left out, item +i+ starts in part +i % n_parts+.
|
|
690
698
|
# @return [Partition]
|
|
691
|
-
# @raise [TypeError] if +
|
|
692
|
-
# @raise [ArgumentError] if +
|
|
693
|
-
# taken, or +start+ does not give each item a part
|
|
694
|
-
def partition(name,
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
+
# @raise [TypeError] if +n_items+ or +n_parts+ is not an Integer
|
|
700
|
+
# @raise [ArgumentError] if +n_items+ is below 1, +n_parts+ below 2, the
|
|
701
|
+
# name is taken, or +start+ does not give each item a part
|
|
702
|
+
def partition(name, n_items, n_parts, start: nil)
|
|
703
|
+
unless n_items.is_a?(Integer)
|
|
704
|
+
raise TypeError, "a partition's number of items is a whole number, got #{n_items.inspect}"
|
|
705
|
+
end
|
|
706
|
+
unless n_parts.is_a?(Integer)
|
|
707
|
+
raise TypeError, "a partition's number of parts is a whole number, got #{n_parts.inspect}"
|
|
708
|
+
end
|
|
709
|
+
raise ArgumentError, "a partition has at least 1 item, got #{n_items}" if n_items < 1
|
|
710
|
+
raise ArgumentError, "a partition has at least 2 parts, got #{n_parts}" if n_parts < 2
|
|
699
711
|
|
|
700
712
|
nm = claim(name)
|
|
701
|
-
unless start.nil? || (start.is_a?(Array) && start.size ==
|
|
702
|
-
|
|
703
|
-
|
|
713
|
+
unless start.nil? || (start.is_a?(Array) && start.size == n_items &&
|
|
714
|
+
start.all? { |b| b.is_a?(Integer) && b >= 0 && b < n_parts })
|
|
715
|
+
raise ArgumentError, "partition #{nm.inspect}: start gives each of the #{n_items} items a part " \
|
|
716
|
+
"from 0 to #{n_parts - 1}"
|
|
704
717
|
end
|
|
705
|
-
partition = Partition.new(self, nm,
|
|
718
|
+
partition = Partition.new(self, nm, n_items, n_parts, start&.dup)
|
|
706
719
|
@partitions << partition
|
|
707
720
|
@structured[nm] = partition
|
|
708
721
|
end
|
|
@@ -734,28 +747,33 @@ module Quicopt
|
|
|
734
747
|
members = Array(vars)
|
|
735
748
|
raise ArgumentError, "block #{nm.inspect} has no variables" if members.empty?
|
|
736
749
|
|
|
737
|
-
members
|
|
750
|
+
# The members checked so far, by identity. Nothing is recorded on the model
|
|
751
|
+
# until every member has passed, so a refused block leaves no trace.
|
|
752
|
+
seen = {}.compare_by_identity
|
|
753
|
+
members.each do |v|
|
|
738
754
|
raise TypeError, "block #{nm.inspect}: a member is a variable, got #{v.class}" unless v.is_a?(Variable)
|
|
739
755
|
raise ArgumentError, "variable #{v.name.inspect} belongs to a different Quicopt::Model" unless v.model.equal?(self)
|
|
740
|
-
raise ArgumentError, "variable #{v.name.inspect} is listed twice in block #{nm.inspect}" if
|
|
741
|
-
if (owner = @
|
|
742
|
-
raise ArgumentError, "variable #{v.name.inspect} is already in block #{owner.
|
|
756
|
+
raise ArgumentError, "variable #{v.name.inspect} is listed twice in block #{nm.inspect}" if seen.key?(v)
|
|
757
|
+
if (owner = @block_of[v])
|
|
758
|
+
raise ArgumentError, "variable #{v.name.inspect} is already in block #{owner.inspect}"
|
|
743
759
|
end
|
|
744
760
|
unless v.lower.finite? && v.upper.finite?
|
|
745
761
|
raise ArgumentError, "variable #{v.name.inspect} has bounds [#{v.lower}, #{v.upper}]; " \
|
|
746
762
|
"a block's variables need finite bounds"
|
|
747
763
|
end
|
|
764
|
+
seen[v] = true
|
|
748
765
|
end
|
|
749
766
|
block = Block.new(nm, members.dup.freeze)
|
|
767
|
+
members.each { |v| @block_of[v] = nm }
|
|
750
768
|
@blocks << block
|
|
751
769
|
block
|
|
752
770
|
end
|
|
753
771
|
|
|
754
772
|
# Declare a table of data that can be indexed by decisions. A model sometimes
|
|
755
773
|
# needs a number from a table at a position that is not known yet: the distance
|
|
756
|
-
# between the first and the second stop of a route whose
|
|
757
|
-
# still choosing, or the price of an option an integer variable picks. A
|
|
758
|
-
# array cannot be indexed this way; the table returned here can.
|
|
774
|
+
# between the first and the second stop of a route whose ordering the service
|
|
775
|
+
# is still choosing, or the price of an option an integer variable picks. A
|
|
776
|
+
# Ruby array cannot be indexed this way; the table returned here can.
|
|
759
777
|
#
|
|
760
778
|
# price = m.lookup_table("price", [3.0, 1.0, 4.0, 1.5])
|
|
761
779
|
# choice = m.int_var(0, 3, "choice")
|
|
@@ -771,8 +789,8 @@ module Quicopt
|
|
|
771
789
|
# @param name [String, Symbol] the table's name; shares one namespace with the
|
|
772
790
|
# model's variables and permutations
|
|
773
791
|
# @param values [Array<Numeric>, Array<Array<Numeric>>] a vector (indexed
|
|
774
|
-
# +table[i]+) or a matrix (
|
|
775
|
-
# numbers, none of them NaN
|
|
792
|
+
# +table[i]+) or a matrix (<tt>table[i, j]</tt>, every row the same
|
|
793
|
+
# length) of numbers, none of them NaN
|
|
776
794
|
# @return [LookupTable]
|
|
777
795
|
# @raise [TypeError] if +values+ is neither a vector nor a matrix of numbers
|
|
778
796
|
# @raise [ArgumentError] if it is empty, ragged or holds NaN, or the name is
|
|
@@ -864,10 +882,11 @@ module Quicopt
|
|
|
864
882
|
lines << " #{v.name} in #{v.domain} #{range}"
|
|
865
883
|
end
|
|
866
884
|
@permutations.each do |p|
|
|
867
|
-
|
|
868
|
-
|
|
885
|
+
rules = p.precedences.map { |a, b| "#{a} before #{b}" }
|
|
886
|
+
rules = rules.empty? ? "" : ", with #{rules.join(', ')}"
|
|
887
|
+
lines << " #{p.name} is an ordering of items 0 to #{p.n_items - 1}#{rules}"
|
|
869
888
|
end
|
|
870
|
-
@partitions.each { |p| lines << " #{p.name} puts items 0 to #{p.
|
|
889
|
+
@partitions.each { |p| lines << " #{p.name} puts items 0 to #{p.n_items - 1} in parts 0 to #{p.n_parts - 1}" }
|
|
871
890
|
@tables.each { |t| lines << " #{t.name} is a #{t.shape.join(' × ')} lookup table" }
|
|
872
891
|
@blocks.each { |b| lines << " #{b.name} is a block of #{b.variables.join(', ')}" }
|
|
873
892
|
lines.join("\n")
|
|
@@ -918,9 +937,9 @@ module Quicopt
|
|
|
918
937
|
end
|
|
919
938
|
|
|
920
939
|
def auto_name
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
"x#{
|
|
940
|
+
number = @variables.size + 1
|
|
941
|
+
number += 1 while @by_name.key?("x#{number}") || @structured.key?("x#{number}")
|
|
942
|
+
"x#{number}"
|
|
924
943
|
end
|
|
925
944
|
|
|
926
945
|
# +name+ as a String, checked to be free among the model's variables,
|
data/lib/quicopt/structured.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# SPDX-FileCopyrightText: (c) 2026 Tim Bode, PGI-12, Forschungszentrum Jülich
|
|
3
3
|
# frozen_string_literal: true
|
|
4
4
|
|
|
5
|
-
# quicopt/structured —
|
|
5
|
+
# quicopt/structured — orderings as decisions (+Permutation+), assignments to
|
|
6
6
|
# groups as decisions (+Partition+) and data read at decided positions
|
|
7
7
|
# (+LookupTable+).
|
|
8
8
|
#
|
|
@@ -44,9 +44,9 @@ module Quicopt
|
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
# A permutation declared by +Model#permutation+: +
|
|
48
|
-
# slots, one item per slot, both numbered from 0. Read it with +item_at+
|
|
49
|
-
# +slot_of+; constrain it with +precede+.
|
|
47
|
+
# A permutation declared by +Model#permutation+: +n_items+ items placed in as
|
|
48
|
+
# many slots, one item per slot, both numbered from 0. Read it with +item_at+
|
|
49
|
+
# and +slot_of+; constrain it with +precede+.
|
|
50
50
|
#
|
|
51
51
|
# route = m.permutation("route", 5)
|
|
52
52
|
# route.item_at(0) # the first stop visited
|
|
@@ -58,7 +58,7 @@ module Quicopt
|
|
|
58
58
|
# @return [String] the permutation's name; its arrangement comes back keyed by it
|
|
59
59
|
attr_reader :name
|
|
60
60
|
# @return [Integer] the number of items, which is also the number of slots
|
|
61
|
-
attr_reader :
|
|
61
|
+
attr_reader :n_items
|
|
62
62
|
# @return [Array<Integer>, nil] the slot each item starts in, or +nil+ for the
|
|
63
63
|
# default (item +i+ in slot +i+)
|
|
64
64
|
attr_reader :start
|
|
@@ -66,10 +66,10 @@ module Quicopt
|
|
|
66
66
|
# Declared with +Model#permutation+, not built directly.
|
|
67
67
|
#
|
|
68
68
|
# @api private
|
|
69
|
-
def initialize(model, name,
|
|
69
|
+
def initialize(model, name, n_items, start)
|
|
70
70
|
@model = model
|
|
71
71
|
@name = name
|
|
72
|
-
@
|
|
72
|
+
@n_items = n_items
|
|
73
73
|
@start = start&.freeze
|
|
74
74
|
@precedences = []
|
|
75
75
|
@reads = {}
|
|
@@ -79,22 +79,22 @@ module Quicopt
|
|
|
79
79
|
# a route whose items are stops, +route.item_at(0)+ is the first stop visited.
|
|
80
80
|
# Index a +LookupTable+ with it to read data about that item.
|
|
81
81
|
#
|
|
82
|
-
# @param slot [Integer] from 0 to +
|
|
82
|
+
# @param slot [Integer] from 0 to +n_items - 1+
|
|
83
83
|
# @return [Expression]
|
|
84
84
|
# @raise [TypeError, ArgumentError] if +slot+ is not one of the slots
|
|
85
85
|
def item_at(slot)
|
|
86
|
-
read(:item_at, Positions.check(slot, @
|
|
86
|
+
read(:item_at, Positions.check(slot, @n_items, "slot", owner))
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
# The slot item +item+ is placed in, an expression whose value the service
|
|
90
90
|
# chooses. For a route whose items are stops, +route.slot_of(3)+ is when stop 3
|
|
91
91
|
# is visited.
|
|
92
92
|
#
|
|
93
|
-
# @param item [Integer] from 0 to +
|
|
93
|
+
# @param item [Integer] from 0 to +n_items - 1+
|
|
94
94
|
# @return [Expression]
|
|
95
95
|
# @raise [TypeError, ArgumentError] if +item+ is not one of the items
|
|
96
96
|
def slot_of(item)
|
|
97
|
-
read(:slot_of, Positions.check(item, @
|
|
97
|
+
read(:slot_of, Positions.check(item, @n_items, "item", owner))
|
|
98
98
|
end
|
|
99
99
|
|
|
100
100
|
# Require item +before+ to be in an earlier slot than item +after+: a pickup
|
|
@@ -109,13 +109,13 @@ module Quicopt
|
|
|
109
109
|
# contradicts those already given (1 before 2 and 2 before 1), or if the
|
|
110
110
|
# +start+ places +before+ after +after+
|
|
111
111
|
def precede(before, after)
|
|
112
|
-
a = Positions.check(before, @
|
|
113
|
-
b = Positions.check(after, @
|
|
112
|
+
a = Positions.check(before, @n_items, "item", owner)
|
|
113
|
+
b = Positions.check(after, @n_items, "item", owner)
|
|
114
114
|
raise ArgumentError, "an item cannot precede itself" if a == b
|
|
115
115
|
if @start && @start[a] > @start[b]
|
|
116
116
|
raise ArgumentError, "#{owner}: the start places item #{a} after item #{b}, which it must precede"
|
|
117
117
|
end
|
|
118
|
-
if Permutation.cycle?(@precedences + [[a, b]], @
|
|
118
|
+
if Permutation.cycle?(@precedences + [[a, b]], @n_items)
|
|
119
119
|
raise ArgumentError, "requiring #{a} before #{b} contradicts the requirements already given for #{owner}"
|
|
120
120
|
end
|
|
121
121
|
|
|
@@ -129,20 +129,20 @@ module Quicopt
|
|
|
129
129
|
@precedences.dup.freeze
|
|
130
130
|
end
|
|
131
131
|
|
|
132
|
-
# @return [String] name and
|
|
132
|
+
# @return [String] name and number of items
|
|
133
133
|
def inspect
|
|
134
|
-
"#<Quicopt::Permutation #{@name} (#{@
|
|
134
|
+
"#<Quicopt::Permutation #{@name} (#{@n_items} items in #{@n_items} slots)>"
|
|
135
135
|
end
|
|
136
136
|
|
|
137
|
-
# Whether the +[before, after]+ requirements over items +0...
|
|
138
|
-
# each other: Kahn's algorithm leaves an item unplaced exactly
|
|
139
|
-
# contain a cycle.
|
|
137
|
+
# Whether the +[before, after]+ requirements over items +0...n_items+
|
|
138
|
+
# contradict each other: Kahn's algorithm leaves an item unplaced exactly
|
|
139
|
+
# when they contain a cycle.
|
|
140
140
|
#
|
|
141
141
|
# @api private
|
|
142
|
-
def self.cycle?(edges,
|
|
143
|
-
indegree = Array.new(
|
|
142
|
+
def self.cycle?(edges, n_items)
|
|
143
|
+
indegree = Array.new(n_items, 0)
|
|
144
144
|
edges.each { |_, b| indegree[b] += 1 }
|
|
145
|
-
ready = (0...
|
|
145
|
+
ready = (0...n_items).select { |i| indegree[i].zero? }
|
|
146
146
|
placed = 0
|
|
147
147
|
until ready.empty?
|
|
148
148
|
x = ready.pop
|
|
@@ -154,7 +154,7 @@ module Quicopt
|
|
|
154
154
|
ready << b if indegree[b].zero?
|
|
155
155
|
end
|
|
156
156
|
end
|
|
157
|
-
placed <
|
|
157
|
+
placed < n_items
|
|
158
158
|
end
|
|
159
159
|
|
|
160
160
|
private
|
|
@@ -169,8 +169,8 @@ module Quicopt
|
|
|
169
169
|
end
|
|
170
170
|
end
|
|
171
171
|
|
|
172
|
-
# A partition declared by +Model#partition+: +
|
|
173
|
-
# both numbered from 0. Read it with +part_of+ and +size_of+.
|
|
172
|
+
# A partition declared by +Model#partition+: +n_items+ items in +n_parts+
|
|
173
|
+
# parts, both numbered from 0. Read it with +part_of+ and +size_of+.
|
|
174
174
|
#
|
|
175
175
|
# teams = m.partition("teams", 6, 2)
|
|
176
176
|
# teams.part_of(4) # the team person 4 joins
|
|
@@ -181,21 +181,21 @@ module Quicopt
|
|
|
181
181
|
# @return [String] the partition's name; its assignment comes back keyed by it
|
|
182
182
|
attr_reader :name
|
|
183
183
|
# @return [Integer] the number of items
|
|
184
|
-
attr_reader :
|
|
184
|
+
attr_reader :n_items
|
|
185
185
|
# @return [Integer] the number of parts
|
|
186
|
-
attr_reader :
|
|
186
|
+
attr_reader :n_parts
|
|
187
187
|
# @return [Array<Integer>, nil] the part each item starts in, or +nil+ for the
|
|
188
|
-
# default (item +i+ in part +i %
|
|
188
|
+
# default (item +i+ in part +i % n_parts+)
|
|
189
189
|
attr_reader :start
|
|
190
190
|
|
|
191
191
|
# Declared with +Model#partition+, not built directly.
|
|
192
192
|
#
|
|
193
193
|
# @api private
|
|
194
|
-
def initialize(model, name,
|
|
194
|
+
def initialize(model, name, n_items, n_parts, start)
|
|
195
195
|
@model = model
|
|
196
196
|
@name = name
|
|
197
|
-
@
|
|
198
|
-
@
|
|
197
|
+
@n_items = n_items
|
|
198
|
+
@n_parts = n_parts
|
|
199
199
|
@start = start&.freeze
|
|
200
200
|
@reads = {}
|
|
201
201
|
end
|
|
@@ -203,26 +203,26 @@ module Quicopt
|
|
|
203
203
|
# The part item +item+ is in, an expression whose value the service
|
|
204
204
|
# chooses. Index a +LookupTable+ with it to read data about that part.
|
|
205
205
|
#
|
|
206
|
-
# @param item [Integer] from 0 to +
|
|
206
|
+
# @param item [Integer] from 0 to +n_items - 1+
|
|
207
207
|
# @return [Expression]
|
|
208
208
|
# @raise [TypeError, ArgumentError] if +item+ is not one of the items
|
|
209
209
|
def part_of(item)
|
|
210
|
-
read(:part_of, Positions.check(item, @
|
|
210
|
+
read(:part_of, Positions.check(item, @n_items, "item", owner))
|
|
211
211
|
end
|
|
212
212
|
|
|
213
213
|
# The number of items in part +part+, an expression whose value the service
|
|
214
|
-
# chooses: a count from 0 to +
|
|
214
|
+
# chooses: a count from 0 to +n_items+.
|
|
215
215
|
#
|
|
216
|
-
# @param part [Integer] from 0 to +
|
|
216
|
+
# @param part [Integer] from 0 to +n_parts - 1+
|
|
217
217
|
# @return [Expression]
|
|
218
218
|
# @raise [TypeError, ArgumentError] if +part+ is not one of the parts
|
|
219
219
|
def size_of(part)
|
|
220
|
-
read(:size_of, Positions.check(part, @
|
|
220
|
+
read(:size_of, Positions.check(part, @n_parts, "part", owner))
|
|
221
221
|
end
|
|
222
222
|
|
|
223
|
-
# @return [String] name and
|
|
223
|
+
# @return [String] name, number of items and number of parts
|
|
224
224
|
def inspect
|
|
225
|
-
"#<Quicopt::Partition #{@name} (#{@
|
|
225
|
+
"#<Quicopt::Partition #{@name} (#{@n_items} items in #{@n_parts} parts)>"
|
|
226
226
|
end
|
|
227
227
|
|
|
228
228
|
private
|
|
@@ -252,7 +252,8 @@ module Quicopt
|
|
|
252
252
|
attr_reader :name
|
|
253
253
|
# @return [Array<Float>, Array<Array<Float>>] the entries, as given
|
|
254
254
|
attr_reader :values
|
|
255
|
-
# @return [Array<Integer>] the extent of each dimension: +[
|
|
255
|
+
# @return [Array<Integer>] the extent of each dimension: +[length]+ or
|
|
256
|
+
# +[rows, columns]+
|
|
256
257
|
attr_reader :shape
|
|
257
258
|
|
|
258
259
|
# Declared with +Model#lookup_table+, not built directly.
|
data/lib/quicopt/version.rb
CHANGED