Skip to content

Tensormaker

Base classes with various utilities for creating tensors.

TensorMakerMixin

Source code in evotorch/tools/tensormaker.py
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
class TensorMakerMixin:
    def __get_dtype_and_device_kwargs(
        self,
        *,
        dtype: Optional[DType],
        device: Optional[Device],
        use_eval_dtype: bool,
        out: Optional[Iterable],
    ) -> dict:
        result = {}
        if out is None:
            if dtype is None:
                if use_eval_dtype:
                    if hasattr(self, "eval_dtype"):
                        result["dtype"] = self.eval_dtype
                    else:
                        raise AttributeError(
                            f"Received `use_eval_dtype` as {repr(use_eval_dtype)}, which represents boolean truth."
                            f" However, evaluation dtype cannot be determined, because this object does not have"
                            f" an attribute named `eval_dtype`."
                        )
                else:
                    result["dtype"] = self.dtype
            else:
                if use_eval_dtype:
                    raise ValueError(
                        f"Received both a `dtype` argument ({repr(dtype)}) and `use_eval_dtype` as True."
                        f" These arguments are conflicting."
                        f" Please either provide a `dtype`, or leave `dtype` as None and pass `use_eval_dtype=True`."
                    )
                else:
                    result["dtype"] = dtype

            if device is None:
                result["device"] = self.device
            else:
                result["device"] = device

        return result

    def __get_size_args(self, *size: Size, num_solutions: Optional[int], out: Optional[Iterable]) -> tuple:
        if out is None:
            nsize = len(size)
            if (nsize == 0) and (num_solutions is None):
                return tuple()
            elif (nsize >= 1) and (num_solutions is None):
                return size
            elif (nsize == 0) and (num_solutions is not None):
                if hasattr(self, "solution_length"):
                    num_solutions = int(num_solutions)
                    if self.solution_length is None:
                        return (num_solutions,)
                    else:
                        return (num_solutions, self.solution_length)
                else:
                    raise AttributeError(
                        f"Received `num_solutions` as {repr(num_solutions)}."
                        f" However, to determine the target tensor's size via `num_solutions`, this object"
                        f" needs to have an attribute named `solution_length`, which seems to be missing."
                    )
            else:
                raise ValueError(
                    f"Encountered both `size` arguments ({repr(size)})"
                    f" and `num_solutions` keyword argument (num_solutions={repr(num_solutions)})."
                    f" Specifying both `size` and `num_solutions` is not valid."
                )
        else:
            return tuple()

    def __get_generator_kwargs(self, *, generator: Any) -> dict:
        result = {}
        if generator is None:
            if hasattr(self, "generator"):
                result["generator"] = self.generator
        else:
            result["generator"] = generator
        return result

    def __get_all_args_for_maker(
        self,
        *size: Size,
        num_solutions: Optional[int],
        out: Optional[Iterable],
        dtype: Optional[DType],
        device: Optional[Device],
        use_eval_dtype: bool,
    ) -> tuple:
        args = self.__get_size_args(*size, num_solutions=num_solutions, out=out)
        kwargs = self.__get_dtype_and_device_kwargs(dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=out)
        if out is not None:
            kwargs["out"] = out
        return args, kwargs

    def __get_all_args_for_random_maker(
        self,
        *size: Size,
        num_solutions: Optional[int],
        out: Optional[Iterable],
        dtype: Optional[DType],
        device: Optional[Device],
        use_eval_dtype: bool,
        generator: Any,
    ):
        args = self.__get_size_args(*size, num_solutions=num_solutions, out=out)

        kwargs = {}
        kwargs.update(
            self.__get_dtype_and_device_kwargs(dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=out)
        )
        kwargs.update(self.__get_generator_kwargs(generator=generator))
        if out is not None:
            kwargs["out"] = out

        return args, kwargs

    def make_tensor(
        self,
        data: Any,
        *,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
        read_only: bool = False,
    ) -> Iterable:
        """
        Make a new tensor.

        When not explicitly specified via arguments, the dtype and the device
        of the resulting tensor is determined by this method's parent object.

        Args:
            data: The data to be converted to a tensor.
                If one wishes to create a PyTorch tensor, this can be anything
                that can be stored by a PyTorch tensor.
                If one wishes to create an `ObjectArray` and therefore passes
                `dtype=object`, then the provided `data` is expected as an
                `Iterable`.
            dtype: Optionally a string (e.g. "float32"), or a PyTorch dtype
                (e.g. torch.float32), or `object` or "object" (as a string)
                or `Any` if one wishes to create an `ObjectArray`.
                If `dtype` is not specified it will be assumed that the user
                wishes to create a tensor using the dtype of this method's
                parent object.
            device: The device in which the tensor will be stored.
                If `device` is not specified, it will be assumed that the user
                wishes to create a tensor on the device of this method's
                parent object.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
            read_only: Whether or not the created tensor will be read-only.
                By default, this is False.
        Returns:
            A PyTorch tensor or an ObjectArray.
        """
        kwargs = self.__get_dtype_and_device_kwargs(dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=None)
        return misc.make_tensor(data, read_only=read_only, **kwargs)

    def make_empty(
        self,
        *size: Size,
        num_solutions: Optional[int] = None,
        out: Optional[Iterable] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
    ) -> Iterable:
        """
        Make an empty tensor.

        When not explicitly specified via arguments, the dtype and the device
        of the resulting tensor is determined by this method's parent object.

        Args:
            size: Shape of the empty tensor to be created.
                expected as multiple positional arguments of integers,
                or as a single positional argument containing a tuple of
                integers.
                Note that when the user wishes to create an `ObjectArray`
                (i.e. when `dtype` is given as `object`), then the size
                is expected as a single integer, or as a single-element
                tuple containing an integer (because `ObjectArray` can only
                be one-dimensional).
            num_solutions: This can be used instead of the `size` arguments
                for specifying the shape of the target tensor.
                Expected as an integer, when `num_solutions` is specified
                as `n`, the shape of the resulting tensor will be
                `(n, m)` where `m` is the solution length reported by this
                method's parent object's `solution_length` attribute.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32) or, for creating an `ObjectArray`,
                "object" (as string) or `object` or `Any`.
                If `dtype` is not specified (and also `out` is None),
                it will be assumed that the user wishes to create a tensor
                using the dtype of this method's parent object.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
        Returns:
            The new empty tensor, which can be a PyTorch tensor or an
            `ObjectArray`.
        """
        args, kwargs = self.__get_all_args_for_maker(
            *size,
            num_solutions=num_solutions,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
        )
        return misc.make_empty(*args, **kwargs)

    def make_zeros(
        self,
        *size: Size,
        num_solutions: Optional[int] = None,
        out: Optional[torch.Tensor] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
    ) -> torch.Tensor:
        """
        Make a new tensor filled with 0, or fill an existing tensor with 0.

        When not explicitly specified via arguments, the dtype and the device
        of the resulting tensor is determined by this method's parent object.

        Args:
            size: Size of the new tensor to be filled with 0.
                This can be given as multiple positional arguments, each such
                positional argument being an integer, or as a single positional
                argument of a tuple, the tuple containing multiple integers.
                Note that, if the user wishes to fill an existing tensor with
                0 values, then no positional argument is expected.
            num_solutions: This can be used instead of the `size` arguments
                for specifying the shape of the target tensor.
                Expected as an integer, when `num_solutions` is specified
                as `n`, the shape of the resulting tensor will be
                `(n, m)` where `m` is the solution length reported by this
                method's parent object's `solution_length` attribute.
            out: Optionally, the tensor to be filled by 0 values.
                If an `out` tensor is given, then no `size` argument is expected.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32).
                If `dtype` is not specified (and also `out` is None),
                it will be assumed that the user wishes to create a tensor
                using the dtype of this method's parent object.
                If an `out` tensor is specified, then `dtype` is expected
                as None.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
                If an `out` tensor is specified, then `device` is expected
                as None.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
        Returns:
            The created or modified tensor after placing 0 values.
        """

        args, kwargs = self.__get_all_args_for_maker(
            *size,
            num_solutions=num_solutions,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
        )
        return misc.make_zeros(*args, **kwargs)

    def make_ones(
        self,
        *size: Size,
        num_solutions: Optional[int] = None,
        out: Optional[torch.Tensor] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
    ) -> torch.Tensor:
        """
        Make a new tensor filled with 1, or fill an existing tensor with 1.

        When not explicitly specified via arguments, the dtype and the device
        of the resulting tensor is determined by this method's parent object.

        Args:
            size: Size of the new tensor to be filled with 1.
                This can be given as multiple positional arguments, each such
                positional argument being an integer, or as a single positional
                argument of a tuple, the tuple containing multiple integers.
                Note that, if the user wishes to fill an existing tensor with
                1 values, then no positional argument is expected.
            num_solutions: This can be used instead of the `size` arguments
                for specifying the shape of the target tensor.
                Expected as an integer, when `num_solutions` is specified
                as `n`, the shape of the resulting tensor will be
                `(n, m)` where `m` is the solution length reported by this
                method's parent object's `solution_length` attribute.
            out: Optionally, the tensor to be filled by 1 values.
                If an `out` tensor is given, then no `size` argument is expected.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32).
                If `dtype` is not specified (and also `out` is None),
                it will be assumed that the user wishes to create a tensor
                using the dtype of this method's parent object.
                If an `out` tensor is specified, then `dtype` is expected
                as None.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
                If an `out` tensor is specified, then `device` is expected
                as None.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
        Returns:
            The created or modified tensor after placing 1 values.
        """
        args, kwargs = self.__get_all_args_for_maker(
            *size,
            num_solutions=num_solutions,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
        )
        return misc.make_ones(*args, **kwargs)

    def make_nan(
        self,
        *size: Size,
        num_solutions: Optional[int] = None,
        out: Optional[torch.Tensor] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
    ) -> torch.Tensor:
        """
        Make a new tensor filled with NaN values, or fill an existing tensor
        with NaN values.

        When not explicitly specified via arguments, the dtype and the device
        of the resulting tensor is determined by this method's parent object.

        Args:
            size: Size of the new tensor to be filled with NaN.
                This can be given as multiple positional arguments, each such
                positional argument being an integer, or as a single positional
                argument of a tuple, the tuple containing multiple integers.
                Note that, if the user wishes to fill an existing tensor with
                NaN values, then no positional argument is expected.
            num_solutions: This can be used instead of the `size` arguments
                for specifying the shape of the target tensor.
                Expected as an integer, when `num_solutions` is specified
                as `n`, the shape of the resulting tensor will be
                `(n, m)` where `m` is the solution length reported by this
                method's parent object's `solution_length` attribute.
            out: Optionally, the tensor to be filled by NaN values.
                If an `out` tensor is given, then no `size` argument is expected.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32).
                If `dtype` is not specified (and also `out` is None),
                it will be assumed that the user wishes to create a tensor
                using the dtype of this method's parent object.
                If an `out` tensor is specified, then `dtype` is expected
                as None.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
                If an `out` tensor is specified, then `device` is expected
                as None.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
        Returns:
            The created or modified tensor after placing NaN values.
        """
        args, kwargs = self.__get_all_args_for_maker(
            *size,
            num_solutions=num_solutions,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
        )
        return misc.make_nan(*args, **kwargs)

    def make_I(
        self,
        size: Optional[Union[int, tuple]] = None,
        *,
        out: Optional[torch.Tensor] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
    ) -> torch.Tensor:
        """
        Make a new identity matrix (I), or change an existing tensor so that
        it expresses the identity matrix.

        When not explicitly specified via arguments, the dtype and the device
        of the resulting tensor is determined by this method's parent object.

        Args:
            size: A single integer or a tuple containing a single integer,
                where the integer specifies the length of the target square
                matrix. In this context, "length" means both rowwise length
                and columnwise length, since the target is a square matrix.
                Note that, if the user wishes to fill an existing tensor with
                identity values, then `size` is expected to be left as None.
            out: Optionally, the existing tensor whose values will be changed
                so that they represent an identity matrix.
                If an `out` tensor is given, then `size` is expected as None.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32).
                If `dtype` is not specified (and also `out` is None),
                it will be assumed that the user wishes to create a tensor
                using the dtype of this method's parent object.
                If an `out` tensor is specified, then `dtype` is expected
                as None.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
                If an `out` tensor is specified, then `device` is expected
                as None.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
        Returns:
            The created or modified tensor after placing the I matrix values
        """

        if size is None:
            if out is None:
                if hasattr(self, "solution_length"):
                    size_args = (self.solution_length,)
                else:
                    raise AttributeError(
                        "The method `.make_I(...)` was used without any `size`"
                        " arguments."
                        " When the `size` argument is missing, the default"
                        " behavior of this method is to create an identity matrix"
                        " of size (n, n), n being the length of a solution."
                        " However, the parent object of this method does not have"
                        " an attribute name `solution_length`."
                    )
            else:
                size_args = tuple()
        elif isinstance(size, tuple):
            if len(size) != 1:
                raise ValueError(
                    f"When the size argument is given as a tuple, the method `make_I(...)` expects the tuple to have"
                    f" only one element. The given tuple is {size}."
                )
            size_args = size
        else:
            size_args = (int(size),)

        args, kwargs = self.__get_all_args_for_maker(
            *size_args,
            num_solutions=None,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
        )
        return misc.make_I(*args, **kwargs)

    def make_uniform(
        self,
        *size: Size,
        num_solutions: Optional[int] = None,
        lb: Optional[RealOrVector] = None,
        ub: Optional[RealOrVector] = None,
        out: Optional[torch.Tensor] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
        generator: Any = None,
    ) -> torch.Tensor:
        """
        Make a new or existing tensor filled by uniformly distributed values.
        Both lower and upper bounds are inclusive.
        This function can work with both float and int dtypes.

        When not explicitly specified via arguments, the dtype and the device
        of the resulting tensor is determined by this method's parent object.

        Args:
            size: Size of the new tensor to be filled with uniformly distributed
                values. This can be given as multiple positional arguments, each
                such positional argument being an integer, or as a single
                positional argument of a tuple, the tuple containing multiple
                integers. Note that, if the user wishes to fill an existing
                tensor instead, then no positional argument is expected.
            num_solutions: This can be used instead of the `size` arguments
                for specifying the shape of the target tensor.
                Expected as an integer, when `num_solutions` is specified
                as `n`, the shape of the resulting tensor will be
                `(n, m)` where `m` is the solution length reported by this
                method's parent object's `solution_length` attribute.
            lb: Lower bound for the uniformly distributed values.
                Can be a scalar, or a tensor.
                If not specified, the lower bound will be taken as 0.
                Note that, if one specifies `lb`, then `ub` is also expected to
                be explicitly specified.
            ub: Upper bound for the uniformly distributed values.
                Can be a scalar, or a tensor.
                If not specified, the upper bound will be taken as 1.
                Note that, if one specifies `ub`, then `lb` is also expected to
                be explicitly specified.
            out: Optionally, the tensor to be filled by uniformly distributed
                values. If an `out` tensor is given, then no `size` argument is
                expected.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32).
                If `dtype` is not specified (and also `out` is None),
                it will be assumed that the user wishes to create a tensor
                using the dtype of this method's parent object.
                If an `out` tensor is specified, then `dtype` is expected
                as None.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
                If an `out` tensor is specified, then `device` is expected
                as None.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
            generator: Pseudo-random generator to be used when sampling
                the values. Can be a `torch.Generator` or any object with
                a `generator` attribute (e.g. a Problem object).
                If not given, then this method's parent object will be
                analyzed whether or not it has its own generator.
                If it does, that generator will be used.
                If not, the global generator of PyTorch will be used.
        Returns:
            The created or modified tensor after placing the uniformly
            distributed values.
        """
        args, kwargs = self.__get_all_args_for_random_maker(
            *size,
            num_solutions=num_solutions,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
            generator=generator,
        )
        return misc.make_uniform(*args, lb=lb, ub=ub, **kwargs)

    def make_gaussian(
        self,
        *size: Size,
        num_solutions: Optional[int] = None,
        center: Optional[RealOrVector] = None,
        stdev: Optional[RealOrVector] = None,
        symmetric: bool = False,
        out: Optional[torch.Tensor] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
        generator: Any = None,
    ) -> torch.Tensor:
        """
        Make a new or existing tensor filled by Gaussian distributed values.
        This function can work only with float dtypes.

        Args:
            size: Size of the new tensor to be filled with Gaussian distributed
                values. This can be given as multiple positional arguments, each
                such positional argument being an integer, or as a single
                positional argument of a tuple, the tuple containing multiple
                integers. Note that, if the user wishes to fill an existing
                tensor instead, then no positional argument is expected.
            num_solutions: This can be used instead of the `size` arguments
                for specifying the shape of the target tensor.
                Expected as an integer, when `num_solutions` is specified
                as `n`, the shape of the resulting tensor will be
                `(n, m)` where `m` is the solution length reported by this
                method's parent object's `solution_length` attribute.
            center: Center point (i.e. mean) of the Gaussian distribution.
                Can be a scalar, or a tensor.
                If not specified, the center point will be taken as 0.
                Note that, if one specifies `center`, then `stdev` is also
                expected to be explicitly specified.
            stdev: Standard deviation for the Gaussian distributed values.
                Can be a scalar, or a tensor.
                If not specified, the standard deviation will be taken as 1.
                Note that, if one specifies `stdev`, then `center` is also
                expected to be explicitly specified.
            symmetric: Whether or not the values should be sampled in a
                symmetric (i.e. antithetic) manner.
                The default is False.
            out: Optionally, the tensor to be filled by Gaussian distributed
                values. If an `out` tensor is given, then no `size` argument is
                expected.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32).
                If `dtype` is not specified (and also `out` is None),
                it will be assumed that the user wishes to create a tensor
                using the dtype of this method's parent object.
                If an `out` tensor is specified, then `dtype` is expected
                as None.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
                If an `out` tensor is specified, then `device` is expected
                as None.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
            generator: Pseudo-random generator to be used when sampling
                the values. Can be a `torch.Generator` or any object with
                a `generator` attribute (e.g. a Problem object).
                If not given, then this method's parent object will be
                analyzed whether or not it has its own generator.
                If it does, that generator will be used.
                If not, the global generator of PyTorch will be used.
        Returns:
            The created or modified tensor after placing the Gaussian
            distributed values.
        """

        args, kwargs = self.__get_all_args_for_random_maker(
            *size,
            num_solutions=num_solutions,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
            generator=generator,
        )
        return misc.make_gaussian(*args, center=center, stdev=stdev, symmetric=symmetric, **kwargs)

    def make_randint(
        self,
        *size: Size,
        n: Union[int, float, torch.Tensor],
        num_solutions: Optional[int] = None,
        out: Optional[torch.Tensor] = None,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
        generator: Any = None,
    ) -> torch.Tensor:
        """
        Make a new or existing tensor filled by random integers.
        The integers are uniformly distributed within `[0 ... n-1]`.
        This function can be used with integer or float dtypes.

        Args:
            size: Size of the new tensor to be filled with uniformly distributed
                values. This can be given as multiple positional arguments, each
                such positional argument being an integer, or as a single
                positional argument of a tuple, the tuple containing multiple
                integers. Note that, if the user wishes to fill an existing
                tensor instead, then no positional argument is expected.
            n: Number of choice(s) for integer sampling.
                The lowest possible value will be 0, and the highest possible
                value will be n - 1.
                `n` can be a scalar, or a tensor.
            out: Optionally, the tensor to be filled by the random integers.
                If an `out` tensor is given, then no `size` argument is
                expected.
            dtype: Optionally a string (e.g. "int64") or a PyTorch dtype
                (e.g. torch.int64).
                If `dtype` is not specified (and also `out` is None),
                `torch.int64` will be used.
                If an `out` tensor is specified, then `dtype` is expected
                as None.
            device: The device in which the new empty tensor will be stored.
                If not specified (and also `out` is None), it will be
                assumed that the user wishes to create a tensor on the
                same device with this method's parent object.
                If an `out` tensor is specified, then `device` is expected
                as None.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
            generator: Pseudo-random generator to be used when sampling
                the values. Can be a `torch.Generator` or any object with
                a `generator` attribute (e.g. a Problem object).
                If not given, then this method's parent object will be
                analyzed whether or not it has its own generator.
                If it does, that generator will be used.
                If not, the global generator of PyTorch will be used.
        Returns:
            The created or modified tensor after placing the uniformly
            distributed values.
        """

        if (dtype is None) and (out is None):
            dtype = torch.int64

        args, kwargs = self.__get_all_args_for_random_maker(
            *size,
            num_solutions=num_solutions,
            out=out,
            dtype=dtype,
            device=device,
            use_eval_dtype=use_eval_dtype,
            generator=generator,
        )
        return misc.make_randint(*args, n=n, **kwargs)

    def as_tensor(
        self,
        x: Any,
        dtype: Optional[DType] = None,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
    ) -> torch.Tensor:
        """
        Get the tensor counterpart of the given object `x`.

        Args:
            x: Any object to be converted to a tensor.
            dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
                (e.g. torch.float32) or, for creating an `ObjectArray`,
                "object" (as string) or `object` or `Any`.
                If `dtype` is not specified, the dtype of this method's
                parent object will be used.
            device: The device in which the resulting tensor will be stored.
                If `device` is not specified, the device of this method's
                parent object will be used.
            use_eval_dtype: If this is given as True and a `dtype` is not
                specified, then the `dtype` of the result will be taken
                from the `eval_dtype` attribute of this method's parent
                object.
        Returns:
            The tensor counterpart of the given object `x`.
        """
        kwargs = self.__get_dtype_and_device_kwargs(dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=None)
        return misc.as_tensor(x, **kwargs)

    def ensure_tensor_length_and_dtype(
        self,
        t: Any,
        length: Optional[int] = None,
        dtype: Optional[DType] = None,
        about: Optional[str] = None,
        *,
        allow_scalar: bool = False,
        device: Optional[Device] = None,
        use_eval_dtype: bool = False,
    ) -> Iterable:
        """
        Return the given sequence as a tensor while also confirming its
        length, dtype, and device.

        Default length, dtype, device are taken from this method's
        parent object.
        In more details, these attributes belonging to this method's parent
        object will be used for determining the the defaults:
        `solution_length`, `dtype`, and `device`.

        Args:
            t: The tensor, or a sequence which is convertible to a tensor.
            length: The length to which the tensor is expected to conform.
                If missing, the `solution_length` attribute of this method's
                parent object will be used as the default value.
            dtype: The dtype to which the tensor is expected to conform.
                If `dtype` argument is missing and `use_eval_dtype` is False,
                then the default dtype will be determined by the `dtype`
                attribute of this method's parent object.
                If `dtype` argument is missing and `use_eval_dtype` is True,
                then the default dtype will be determined by the `eval_dtype`
                attribute of this method's parent object.
            about: The prefix for the error message. Can be left as None.
            allow_scalar: Whether or not to accept scalars in addition
                to vector of the desired length.
                If `allow_scalar` is False, then scalars will be converted
                to sequences of the desired length. The sequence will contain
                the same scalar, repeated.
                If `allow_scalar` is True, then the scalar itself will be
                converted to a PyTorch scalar, and then will be returned.
            device: The device in which the sequence is to be stored.
                If the given sequence is on a different device than the
                desired device, a copy on the correct device will be made.
                If device is None, the default behavior of `torch.tensor(...)`
                will be used, that is: if `t` is already a tensor, the result
                will be on the same device, otherwise, the result will be on
                the cpu.
            use_eval_dtype: Whether or not to use the evaluation dtype
                (instead of the dtype of decision values).
                If this is given as True, the `dtype` argument is expected
                as None.
                If `dtype` argument is missing and `use_eval_dtype` is False,
                then the default dtype will be determined by the `dtype`
                attribute of this method's parent object.
                If `dtype` argument is missing and `use_eval_dtype` is True,
                then the default dtype will be determined by the `eval_dtype`
                attribute of this method's parent object.
        Returns:
            The sequence whose correctness in terms of length, dtype, and
            device is ensured.
        Raises:
            ValueError: if there is a length mismatch.
        """

        if length is None:
            if hasattr(self, "solution_length"):
                length = self.solution_length
            else:
                raise AttributeError(
                    f"{about}: The argument `length` was found to be None."
                    f" When the `length` argument is None, the default behavior is to use the `solution_length`"
                    f" attribute of this method's parent object."
                    f" However, this method's parent object does NOT have a `solution_length` attribute."
                )
        dtype_and_device = self.__get_dtype_and_device_kwargs(
            dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=None
        )

        return misc.ensure_tensor_length_and_dtype(
            t, length=length, about=about, allow_scalar=allow_scalar, **dtype_and_device
        )

    def make_uniform_shaped_like(
        self,
        t: torch.Tensor,
        *,
        lb: Optional[RealOrVector] = None,
        ub: Optional[RealOrVector] = None,
    ) -> torch.Tensor:
        """
        Make a new uniformly-filled tensor, shaped like the given tensor.
        The `dtype` and `device` will be determined by the parent of this
        method (not by the given tensor).
        If the parent of this method has its own random generator, then that
        generator will be used.

        Args:
            t: The tensor according to which the result will be shaped.
            lb: The inclusive lower bounds for the uniform distribution.
                Can be a scalar or a tensor.
                If left as None, 0.0 will be used as the upper bound.
            ub: The inclusive upper bounds for the uniform distribution.
                Can be a scalar or a tensor.
                If left as None, 1.0 will be used as the upper bound.
        Returns:
            A new tensor whose shape is the same with the given tensor.
        """
        return self.make_uniform(t.shape, lb=lb, ub=ub)

    def make_gaussian_shaped_like(
        self,
        t: torch.Tensor,
        *,
        center: Optional[RealOrVector] = None,
        stdev: Optional[RealOrVector] = None,
    ) -> torch.Tensor:
        """
        Make a new tensor, shaped like the given tensor, with its values
        filled by the Gaussian distribution.

        The `dtype` and `device` will be determined by the parent of this
        method (not by the given tensor).
        If the parent of this method has its own random generator, then that
        generator will be used.

        Args:
            t: The tensor according to which the result will be shaped.
            center: Center point for the Gaussian distribution.
                Can be a scalar or a tensor.
                If left as None, 0.0 will be used as the center point.
            stdev: The standard deviation for the Gaussian distribution.
                Can be a scalar or a tensor.
                If left as None, 1.0 will be used as the standard deviation.
        Returns:
            A new tensor whose shape is the same with the given tensor.
        """
        return self.make_gaussian(t.shape, center=center, stdev=stdev)

as_tensor(x, dtype=None, device=None, use_eval_dtype=False)

Get the tensor counterpart of the given object x.

Parameters:

Name Type Description Default
x Any

Any object to be converted to a tensor.

required
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32) or, for creating an ObjectArray, "object" (as string) or object or Any. If dtype is not specified, the dtype of this method's parent object will be used.

None
device Optional[Device]

The device in which the resulting tensor will be stored. If device is not specified, the device of this method's parent object will be used.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
Source code in evotorch/tools/tensormaker.py
def as_tensor(
    self,
    x: Any,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
) -> torch.Tensor:
    """
    Get the tensor counterpart of the given object `x`.

    Args:
        x: Any object to be converted to a tensor.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32) or, for creating an `ObjectArray`,
            "object" (as string) or `object` or `Any`.
            If `dtype` is not specified, the dtype of this method's
            parent object will be used.
        device: The device in which the resulting tensor will be stored.
            If `device` is not specified, the device of this method's
            parent object will be used.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
    Returns:
        The tensor counterpart of the given object `x`.
    """
    kwargs = self.__get_dtype_and_device_kwargs(dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=None)
    return misc.as_tensor(x, **kwargs)

ensure_tensor_length_and_dtype(t, length=None, dtype=None, about=None, *, allow_scalar=False, device=None, use_eval_dtype=False)

Return the given sequence as a tensor while also confirming its length, dtype, and device.

Default length, dtype, device are taken from this method's parent object. In more details, these attributes belonging to this method's parent object will be used for determining the the defaults: solution_length, dtype, and device.

Parameters:

Name Type Description Default
t Any

The tensor, or a sequence which is convertible to a tensor.

required
length Optional[int]

The length to which the tensor is expected to conform. If missing, the solution_length attribute of this method's parent object will be used as the default value.

None
dtype Optional[DType]

The dtype to which the tensor is expected to conform. If dtype argument is missing and use_eval_dtype is False, then the default dtype will be determined by the dtype attribute of this method's parent object. If dtype argument is missing and use_eval_dtype is True, then the default dtype will be determined by the eval_dtype attribute of this method's parent object.

None
about Optional[str]

The prefix for the error message. Can be left as None.

None
allow_scalar bool

Whether or not to accept scalars in addition to vector of the desired length. If allow_scalar is False, then scalars will be converted to sequences of the desired length. The sequence will contain the same scalar, repeated. If allow_scalar is True, then the scalar itself will be converted to a PyTorch scalar, and then will be returned.

False
device Optional[Device]

The device in which the sequence is to be stored. If the given sequence is on a different device than the desired device, a copy on the correct device will be made. If device is None, the default behavior of torch.tensor(...) will be used, that is: if t is already a tensor, the result will be on the same device, otherwise, the result will be on the cpu.

None
use_eval_dtype bool

Whether or not to use the evaluation dtype (instead of the dtype of decision values). If this is given as True, the dtype argument is expected as None. If dtype argument is missing and use_eval_dtype is False, then the default dtype will be determined by the dtype attribute of this method's parent object. If dtype argument is missing and use_eval_dtype is True, then the default dtype will be determined by the eval_dtype attribute of this method's parent object.

False
Source code in evotorch/tools/tensormaker.py
def ensure_tensor_length_and_dtype(
    self,
    t: Any,
    length: Optional[int] = None,
    dtype: Optional[DType] = None,
    about: Optional[str] = None,
    *,
    allow_scalar: bool = False,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
) -> Iterable:
    """
    Return the given sequence as a tensor while also confirming its
    length, dtype, and device.

    Default length, dtype, device are taken from this method's
    parent object.
    In more details, these attributes belonging to this method's parent
    object will be used for determining the the defaults:
    `solution_length`, `dtype`, and `device`.

    Args:
        t: The tensor, or a sequence which is convertible to a tensor.
        length: The length to which the tensor is expected to conform.
            If missing, the `solution_length` attribute of this method's
            parent object will be used as the default value.
        dtype: The dtype to which the tensor is expected to conform.
            If `dtype` argument is missing and `use_eval_dtype` is False,
            then the default dtype will be determined by the `dtype`
            attribute of this method's parent object.
            If `dtype` argument is missing and `use_eval_dtype` is True,
            then the default dtype will be determined by the `eval_dtype`
            attribute of this method's parent object.
        about: The prefix for the error message. Can be left as None.
        allow_scalar: Whether or not to accept scalars in addition
            to vector of the desired length.
            If `allow_scalar` is False, then scalars will be converted
            to sequences of the desired length. The sequence will contain
            the same scalar, repeated.
            If `allow_scalar` is True, then the scalar itself will be
            converted to a PyTorch scalar, and then will be returned.
        device: The device in which the sequence is to be stored.
            If the given sequence is on a different device than the
            desired device, a copy on the correct device will be made.
            If device is None, the default behavior of `torch.tensor(...)`
            will be used, that is: if `t` is already a tensor, the result
            will be on the same device, otherwise, the result will be on
            the cpu.
        use_eval_dtype: Whether or not to use the evaluation dtype
            (instead of the dtype of decision values).
            If this is given as True, the `dtype` argument is expected
            as None.
            If `dtype` argument is missing and `use_eval_dtype` is False,
            then the default dtype will be determined by the `dtype`
            attribute of this method's parent object.
            If `dtype` argument is missing and `use_eval_dtype` is True,
            then the default dtype will be determined by the `eval_dtype`
            attribute of this method's parent object.
    Returns:
        The sequence whose correctness in terms of length, dtype, and
        device is ensured.
    Raises:
        ValueError: if there is a length mismatch.
    """

    if length is None:
        if hasattr(self, "solution_length"):
            length = self.solution_length
        else:
            raise AttributeError(
                f"{about}: The argument `length` was found to be None."
                f" When the `length` argument is None, the default behavior is to use the `solution_length`"
                f" attribute of this method's parent object."
                f" However, this method's parent object does NOT have a `solution_length` attribute."
            )
    dtype_and_device = self.__get_dtype_and_device_kwargs(
        dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=None
    )

    return misc.ensure_tensor_length_and_dtype(
        t, length=length, about=about, allow_scalar=allow_scalar, **dtype_and_device
    )

make_I(size=None, *, out=None, dtype=None, device=None, use_eval_dtype=False)

Make a new identity matrix (I), or change an existing tensor so that it expresses the identity matrix.

When not explicitly specified via arguments, the dtype and the device of the resulting tensor is determined by this method's parent object.

Parameters:

Name Type Description Default
size Optional[Union[int, tuple]]

A single integer or a tuple containing a single integer, where the integer specifies the length of the target square matrix. In this context, "length" means both rowwise length and columnwise length, since the target is a square matrix. Note that, if the user wishes to fill an existing tensor with identity values, then size is expected to be left as None.

None
out Optional[Tensor]

Optionally, the existing tensor whose values will be changed so that they represent an identity matrix. If an out tensor is given, then size is expected as None.

None
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32). If dtype is not specified (and also out is None), it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object. If an out tensor is specified, then dtype is expected as None.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object. If an out tensor is specified, then device is expected as None.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
Source code in evotorch/tools/tensormaker.py
def make_I(
    self,
    size: Optional[Union[int, tuple]] = None,
    *,
    out: Optional[torch.Tensor] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
) -> torch.Tensor:
    """
    Make a new identity matrix (I), or change an existing tensor so that
    it expresses the identity matrix.

    When not explicitly specified via arguments, the dtype and the device
    of the resulting tensor is determined by this method's parent object.

    Args:
        size: A single integer or a tuple containing a single integer,
            where the integer specifies the length of the target square
            matrix. In this context, "length" means both rowwise length
            and columnwise length, since the target is a square matrix.
            Note that, if the user wishes to fill an existing tensor with
            identity values, then `size` is expected to be left as None.
        out: Optionally, the existing tensor whose values will be changed
            so that they represent an identity matrix.
            If an `out` tensor is given, then `size` is expected as None.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32).
            If `dtype` is not specified (and also `out` is None),
            it will be assumed that the user wishes to create a tensor
            using the dtype of this method's parent object.
            If an `out` tensor is specified, then `dtype` is expected
            as None.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
            If an `out` tensor is specified, then `device` is expected
            as None.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
    Returns:
        The created or modified tensor after placing the I matrix values
    """

    if size is None:
        if out is None:
            if hasattr(self, "solution_length"):
                size_args = (self.solution_length,)
            else:
                raise AttributeError(
                    "The method `.make_I(...)` was used without any `size`"
                    " arguments."
                    " When the `size` argument is missing, the default"
                    " behavior of this method is to create an identity matrix"
                    " of size (n, n), n being the length of a solution."
                    " However, the parent object of this method does not have"
                    " an attribute name `solution_length`."
                )
        else:
            size_args = tuple()
    elif isinstance(size, tuple):
        if len(size) != 1:
            raise ValueError(
                f"When the size argument is given as a tuple, the method `make_I(...)` expects the tuple to have"
                f" only one element. The given tuple is {size}."
            )
        size_args = size
    else:
        size_args = (int(size),)

    args, kwargs = self.__get_all_args_for_maker(
        *size_args,
        num_solutions=None,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
    )
    return misc.make_I(*args, **kwargs)

make_empty(*size, num_solutions=None, out=None, dtype=None, device=None, use_eval_dtype=False)

Make an empty tensor.

When not explicitly specified via arguments, the dtype and the device of the resulting tensor is determined by this method's parent object.

Parameters:

Name Type Description Default
size Size

Shape of the empty tensor to be created. expected as multiple positional arguments of integers, or as a single positional argument containing a tuple of integers. Note that when the user wishes to create an ObjectArray (i.e. when dtype is given as object), then the size is expected as a single integer, or as a single-element tuple containing an integer (because ObjectArray can only be one-dimensional).

()
num_solutions Optional[int]

This can be used instead of the size arguments for specifying the shape of the target tensor. Expected as an integer, when num_solutions is specified as n, the shape of the resulting tensor will be (n, m) where m is the solution length reported by this method's parent object's solution_length attribute.

None
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32) or, for creating an ObjectArray, "object" (as string) or object or Any. If dtype is not specified (and also out is None), it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
Source code in evotorch/tools/tensormaker.py
def make_empty(
    self,
    *size: Size,
    num_solutions: Optional[int] = None,
    out: Optional[Iterable] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
) -> Iterable:
    """
    Make an empty tensor.

    When not explicitly specified via arguments, the dtype and the device
    of the resulting tensor is determined by this method's parent object.

    Args:
        size: Shape of the empty tensor to be created.
            expected as multiple positional arguments of integers,
            or as a single positional argument containing a tuple of
            integers.
            Note that when the user wishes to create an `ObjectArray`
            (i.e. when `dtype` is given as `object`), then the size
            is expected as a single integer, or as a single-element
            tuple containing an integer (because `ObjectArray` can only
            be one-dimensional).
        num_solutions: This can be used instead of the `size` arguments
            for specifying the shape of the target tensor.
            Expected as an integer, when `num_solutions` is specified
            as `n`, the shape of the resulting tensor will be
            `(n, m)` where `m` is the solution length reported by this
            method's parent object's `solution_length` attribute.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32) or, for creating an `ObjectArray`,
            "object" (as string) or `object` or `Any`.
            If `dtype` is not specified (and also `out` is None),
            it will be assumed that the user wishes to create a tensor
            using the dtype of this method's parent object.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
    Returns:
        The new empty tensor, which can be a PyTorch tensor or an
        `ObjectArray`.
    """
    args, kwargs = self.__get_all_args_for_maker(
        *size,
        num_solutions=num_solutions,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
    )
    return misc.make_empty(*args, **kwargs)

make_gaussian(*size, num_solutions=None, center=None, stdev=None, symmetric=False, out=None, dtype=None, device=None, use_eval_dtype=False, generator=None)

Make a new or existing tensor filled by Gaussian distributed values. This function can work only with float dtypes.

Parameters:

Name Type Description Default
size Size

Size of the new tensor to be filled with Gaussian distributed values. This can be given as multiple positional arguments, each such positional argument being an integer, or as a single positional argument of a tuple, the tuple containing multiple integers. Note that, if the user wishes to fill an existing tensor instead, then no positional argument is expected.

()
num_solutions Optional[int]

This can be used instead of the size arguments for specifying the shape of the target tensor. Expected as an integer, when num_solutions is specified as n, the shape of the resulting tensor will be (n, m) where m is the solution length reported by this method's parent object's solution_length attribute.

None
center Optional[RealOrVector]

Center point (i.e. mean) of the Gaussian distribution. Can be a scalar, or a tensor. If not specified, the center point will be taken as 0. Note that, if one specifies center, then stdev is also expected to be explicitly specified.

None
stdev Optional[RealOrVector]

Standard deviation for the Gaussian distributed values. Can be a scalar, or a tensor. If not specified, the standard deviation will be taken as 1. Note that, if one specifies stdev, then center is also expected to be explicitly specified.

None
symmetric bool

Whether or not the values should be sampled in a symmetric (i.e. antithetic) manner. The default is False.

False
out Optional[Tensor]

Optionally, the tensor to be filled by Gaussian distributed values. If an out tensor is given, then no size argument is expected.

None
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32). If dtype is not specified (and also out is None), it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object. If an out tensor is specified, then dtype is expected as None.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object. If an out tensor is specified, then device is expected as None.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
generator Any

Pseudo-random generator to be used when sampling the values. Can be a torch.Generator or any object with a generator attribute (e.g. a Problem object). If not given, then this method's parent object will be analyzed whether or not it has its own generator. If it does, that generator will be used. If not, the global generator of PyTorch will be used.

None
Source code in evotorch/tools/tensormaker.py
def make_gaussian(
    self,
    *size: Size,
    num_solutions: Optional[int] = None,
    center: Optional[RealOrVector] = None,
    stdev: Optional[RealOrVector] = None,
    symmetric: bool = False,
    out: Optional[torch.Tensor] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
    generator: Any = None,
) -> torch.Tensor:
    """
    Make a new or existing tensor filled by Gaussian distributed values.
    This function can work only with float dtypes.

    Args:
        size: Size of the new tensor to be filled with Gaussian distributed
            values. This can be given as multiple positional arguments, each
            such positional argument being an integer, or as a single
            positional argument of a tuple, the tuple containing multiple
            integers. Note that, if the user wishes to fill an existing
            tensor instead, then no positional argument is expected.
        num_solutions: This can be used instead of the `size` arguments
            for specifying the shape of the target tensor.
            Expected as an integer, when `num_solutions` is specified
            as `n`, the shape of the resulting tensor will be
            `(n, m)` where `m` is the solution length reported by this
            method's parent object's `solution_length` attribute.
        center: Center point (i.e. mean) of the Gaussian distribution.
            Can be a scalar, or a tensor.
            If not specified, the center point will be taken as 0.
            Note that, if one specifies `center`, then `stdev` is also
            expected to be explicitly specified.
        stdev: Standard deviation for the Gaussian distributed values.
            Can be a scalar, or a tensor.
            If not specified, the standard deviation will be taken as 1.
            Note that, if one specifies `stdev`, then `center` is also
            expected to be explicitly specified.
        symmetric: Whether or not the values should be sampled in a
            symmetric (i.e. antithetic) manner.
            The default is False.
        out: Optionally, the tensor to be filled by Gaussian distributed
            values. If an `out` tensor is given, then no `size` argument is
            expected.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32).
            If `dtype` is not specified (and also `out` is None),
            it will be assumed that the user wishes to create a tensor
            using the dtype of this method's parent object.
            If an `out` tensor is specified, then `dtype` is expected
            as None.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
            If an `out` tensor is specified, then `device` is expected
            as None.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
        generator: Pseudo-random generator to be used when sampling
            the values. Can be a `torch.Generator` or any object with
            a `generator` attribute (e.g. a Problem object).
            If not given, then this method's parent object will be
            analyzed whether or not it has its own generator.
            If it does, that generator will be used.
            If not, the global generator of PyTorch will be used.
    Returns:
        The created or modified tensor after placing the Gaussian
        distributed values.
    """

    args, kwargs = self.__get_all_args_for_random_maker(
        *size,
        num_solutions=num_solutions,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
        generator=generator,
    )
    return misc.make_gaussian(*args, center=center, stdev=stdev, symmetric=symmetric, **kwargs)

make_gaussian_shaped_like(t, *, center=None, stdev=None)

Make a new tensor, shaped like the given tensor, with its values filled by the Gaussian distribution.

The dtype and device will be determined by the parent of this method (not by the given tensor). If the parent of this method has its own random generator, then that generator will be used.

Parameters:

Name Type Description Default
t Tensor

The tensor according to which the result will be shaped.

required
center Optional[RealOrVector]

Center point for the Gaussian distribution. Can be a scalar or a tensor. If left as None, 0.0 will be used as the center point.

None
stdev Optional[RealOrVector]

The standard deviation for the Gaussian distribution. Can be a scalar or a tensor. If left as None, 1.0 will be used as the standard deviation.

None
Source code in evotorch/tools/tensormaker.py
def make_gaussian_shaped_like(
    self,
    t: torch.Tensor,
    *,
    center: Optional[RealOrVector] = None,
    stdev: Optional[RealOrVector] = None,
) -> torch.Tensor:
    """
    Make a new tensor, shaped like the given tensor, with its values
    filled by the Gaussian distribution.

    The `dtype` and `device` will be determined by the parent of this
    method (not by the given tensor).
    If the parent of this method has its own random generator, then that
    generator will be used.

    Args:
        t: The tensor according to which the result will be shaped.
        center: Center point for the Gaussian distribution.
            Can be a scalar or a tensor.
            If left as None, 0.0 will be used as the center point.
        stdev: The standard deviation for the Gaussian distribution.
            Can be a scalar or a tensor.
            If left as None, 1.0 will be used as the standard deviation.
    Returns:
        A new tensor whose shape is the same with the given tensor.
    """
    return self.make_gaussian(t.shape, center=center, stdev=stdev)

make_nan(*size, num_solutions=None, out=None, dtype=None, device=None, use_eval_dtype=False)

Make a new tensor filled with NaN values, or fill an existing tensor with NaN values.

When not explicitly specified via arguments, the dtype and the device of the resulting tensor is determined by this method's parent object.

Parameters:

Name Type Description Default
size Size

Size of the new tensor to be filled with NaN. This can be given as multiple positional arguments, each such positional argument being an integer, or as a single positional argument of a tuple, the tuple containing multiple integers. Note that, if the user wishes to fill an existing tensor with NaN values, then no positional argument is expected.

()
num_solutions Optional[int]

This can be used instead of the size arguments for specifying the shape of the target tensor. Expected as an integer, when num_solutions is specified as n, the shape of the resulting tensor will be (n, m) where m is the solution length reported by this method's parent object's solution_length attribute.

None
out Optional[Tensor]

Optionally, the tensor to be filled by NaN values. If an out tensor is given, then no size argument is expected.

None
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32). If dtype is not specified (and also out is None), it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object. If an out tensor is specified, then dtype is expected as None.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object. If an out tensor is specified, then device is expected as None.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
Source code in evotorch/tools/tensormaker.py
def make_nan(
    self,
    *size: Size,
    num_solutions: Optional[int] = None,
    out: Optional[torch.Tensor] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
) -> torch.Tensor:
    """
    Make a new tensor filled with NaN values, or fill an existing tensor
    with NaN values.

    When not explicitly specified via arguments, the dtype and the device
    of the resulting tensor is determined by this method's parent object.

    Args:
        size: Size of the new tensor to be filled with NaN.
            This can be given as multiple positional arguments, each such
            positional argument being an integer, or as a single positional
            argument of a tuple, the tuple containing multiple integers.
            Note that, if the user wishes to fill an existing tensor with
            NaN values, then no positional argument is expected.
        num_solutions: This can be used instead of the `size` arguments
            for specifying the shape of the target tensor.
            Expected as an integer, when `num_solutions` is specified
            as `n`, the shape of the resulting tensor will be
            `(n, m)` where `m` is the solution length reported by this
            method's parent object's `solution_length` attribute.
        out: Optionally, the tensor to be filled by NaN values.
            If an `out` tensor is given, then no `size` argument is expected.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32).
            If `dtype` is not specified (and also `out` is None),
            it will be assumed that the user wishes to create a tensor
            using the dtype of this method's parent object.
            If an `out` tensor is specified, then `dtype` is expected
            as None.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
            If an `out` tensor is specified, then `device` is expected
            as None.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
    Returns:
        The created or modified tensor after placing NaN values.
    """
    args, kwargs = self.__get_all_args_for_maker(
        *size,
        num_solutions=num_solutions,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
    )
    return misc.make_nan(*args, **kwargs)

make_ones(*size, num_solutions=None, out=None, dtype=None, device=None, use_eval_dtype=False)

Make a new tensor filled with 1, or fill an existing tensor with 1.

When not explicitly specified via arguments, the dtype and the device of the resulting tensor is determined by this method's parent object.

Parameters:

Name Type Description Default
size Size

Size of the new tensor to be filled with 1. This can be given as multiple positional arguments, each such positional argument being an integer, or as a single positional argument of a tuple, the tuple containing multiple integers. Note that, if the user wishes to fill an existing tensor with 1 values, then no positional argument is expected.

()
num_solutions Optional[int]

This can be used instead of the size arguments for specifying the shape of the target tensor. Expected as an integer, when num_solutions is specified as n, the shape of the resulting tensor will be (n, m) where m is the solution length reported by this method's parent object's solution_length attribute.

None
out Optional[Tensor]

Optionally, the tensor to be filled by 1 values. If an out tensor is given, then no size argument is expected.

None
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32). If dtype is not specified (and also out is None), it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object. If an out tensor is specified, then dtype is expected as None.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object. If an out tensor is specified, then device is expected as None.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
Source code in evotorch/tools/tensormaker.py
def make_ones(
    self,
    *size: Size,
    num_solutions: Optional[int] = None,
    out: Optional[torch.Tensor] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
) -> torch.Tensor:
    """
    Make a new tensor filled with 1, or fill an existing tensor with 1.

    When not explicitly specified via arguments, the dtype and the device
    of the resulting tensor is determined by this method's parent object.

    Args:
        size: Size of the new tensor to be filled with 1.
            This can be given as multiple positional arguments, each such
            positional argument being an integer, or as a single positional
            argument of a tuple, the tuple containing multiple integers.
            Note that, if the user wishes to fill an existing tensor with
            1 values, then no positional argument is expected.
        num_solutions: This can be used instead of the `size` arguments
            for specifying the shape of the target tensor.
            Expected as an integer, when `num_solutions` is specified
            as `n`, the shape of the resulting tensor will be
            `(n, m)` where `m` is the solution length reported by this
            method's parent object's `solution_length` attribute.
        out: Optionally, the tensor to be filled by 1 values.
            If an `out` tensor is given, then no `size` argument is expected.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32).
            If `dtype` is not specified (and also `out` is None),
            it will be assumed that the user wishes to create a tensor
            using the dtype of this method's parent object.
            If an `out` tensor is specified, then `dtype` is expected
            as None.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
            If an `out` tensor is specified, then `device` is expected
            as None.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
    Returns:
        The created or modified tensor after placing 1 values.
    """
    args, kwargs = self.__get_all_args_for_maker(
        *size,
        num_solutions=num_solutions,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
    )
    return misc.make_ones(*args, **kwargs)

make_randint(*size, n, num_solutions=None, out=None, dtype=None, device=None, use_eval_dtype=False, generator=None)

Make a new or existing tensor filled by random integers. The integers are uniformly distributed within [0 ... n-1]. This function can be used with integer or float dtypes.

Parameters:

Name Type Description Default
size Size

Size of the new tensor to be filled with uniformly distributed values. This can be given as multiple positional arguments, each such positional argument being an integer, or as a single positional argument of a tuple, the tuple containing multiple integers. Note that, if the user wishes to fill an existing tensor instead, then no positional argument is expected.

()
n Union[int, float, Tensor]

Number of choice(s) for integer sampling. The lowest possible value will be 0, and the highest possible value will be n - 1. n can be a scalar, or a tensor.

required
out Optional[Tensor]

Optionally, the tensor to be filled by the random integers. If an out tensor is given, then no size argument is expected.

None
dtype Optional[DType]

Optionally a string (e.g. "int64") or a PyTorch dtype (e.g. torch.int64). If dtype is not specified (and also out is None), torch.int64 will be used. If an out tensor is specified, then dtype is expected as None.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object. If an out tensor is specified, then device is expected as None.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
generator Any

Pseudo-random generator to be used when sampling the values. Can be a torch.Generator or any object with a generator attribute (e.g. a Problem object). If not given, then this method's parent object will be analyzed whether or not it has its own generator. If it does, that generator will be used. If not, the global generator of PyTorch will be used.

None
Source code in evotorch/tools/tensormaker.py
def make_randint(
    self,
    *size: Size,
    n: Union[int, float, torch.Tensor],
    num_solutions: Optional[int] = None,
    out: Optional[torch.Tensor] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
    generator: Any = None,
) -> torch.Tensor:
    """
    Make a new or existing tensor filled by random integers.
    The integers are uniformly distributed within `[0 ... n-1]`.
    This function can be used with integer or float dtypes.

    Args:
        size: Size of the new tensor to be filled with uniformly distributed
            values. This can be given as multiple positional arguments, each
            such positional argument being an integer, or as a single
            positional argument of a tuple, the tuple containing multiple
            integers. Note that, if the user wishes to fill an existing
            tensor instead, then no positional argument is expected.
        n: Number of choice(s) for integer sampling.
            The lowest possible value will be 0, and the highest possible
            value will be n - 1.
            `n` can be a scalar, or a tensor.
        out: Optionally, the tensor to be filled by the random integers.
            If an `out` tensor is given, then no `size` argument is
            expected.
        dtype: Optionally a string (e.g. "int64") or a PyTorch dtype
            (e.g. torch.int64).
            If `dtype` is not specified (and also `out` is None),
            `torch.int64` will be used.
            If an `out` tensor is specified, then `dtype` is expected
            as None.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
            If an `out` tensor is specified, then `device` is expected
            as None.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
        generator: Pseudo-random generator to be used when sampling
            the values. Can be a `torch.Generator` or any object with
            a `generator` attribute (e.g. a Problem object).
            If not given, then this method's parent object will be
            analyzed whether or not it has its own generator.
            If it does, that generator will be used.
            If not, the global generator of PyTorch will be used.
    Returns:
        The created or modified tensor after placing the uniformly
        distributed values.
    """

    if (dtype is None) and (out is None):
        dtype = torch.int64

    args, kwargs = self.__get_all_args_for_random_maker(
        *size,
        num_solutions=num_solutions,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
        generator=generator,
    )
    return misc.make_randint(*args, n=n, **kwargs)

make_tensor(data, *, dtype=None, device=None, use_eval_dtype=False, read_only=False)

Make a new tensor.

When not explicitly specified via arguments, the dtype and the device of the resulting tensor is determined by this method's parent object.

Parameters:

Name Type Description Default
data Any

The data to be converted to a tensor. If one wishes to create a PyTorch tensor, this can be anything that can be stored by a PyTorch tensor. If one wishes to create an ObjectArray and therefore passes dtype=object, then the provided data is expected as an Iterable.

required
dtype Optional[DType]

Optionally a string (e.g. "float32"), or a PyTorch dtype (e.g. torch.float32), or object or "object" (as a string) or Any if one wishes to create an ObjectArray. If dtype is not specified it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object.

None
device Optional[Device]

The device in which the tensor will be stored. If device is not specified, it will be assumed that the user wishes to create a tensor on the device of this method's parent object.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
read_only bool

Whether or not the created tensor will be read-only. By default, this is False.

False
Source code in evotorch/tools/tensormaker.py
def make_tensor(
    self,
    data: Any,
    *,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
    read_only: bool = False,
) -> Iterable:
    """
    Make a new tensor.

    When not explicitly specified via arguments, the dtype and the device
    of the resulting tensor is determined by this method's parent object.

    Args:
        data: The data to be converted to a tensor.
            If one wishes to create a PyTorch tensor, this can be anything
            that can be stored by a PyTorch tensor.
            If one wishes to create an `ObjectArray` and therefore passes
            `dtype=object`, then the provided `data` is expected as an
            `Iterable`.
        dtype: Optionally a string (e.g. "float32"), or a PyTorch dtype
            (e.g. torch.float32), or `object` or "object" (as a string)
            or `Any` if one wishes to create an `ObjectArray`.
            If `dtype` is not specified it will be assumed that the user
            wishes to create a tensor using the dtype of this method's
            parent object.
        device: The device in which the tensor will be stored.
            If `device` is not specified, it will be assumed that the user
            wishes to create a tensor on the device of this method's
            parent object.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
        read_only: Whether or not the created tensor will be read-only.
            By default, this is False.
    Returns:
        A PyTorch tensor or an ObjectArray.
    """
    kwargs = self.__get_dtype_and_device_kwargs(dtype=dtype, device=device, use_eval_dtype=use_eval_dtype, out=None)
    return misc.make_tensor(data, read_only=read_only, **kwargs)

make_uniform(*size, num_solutions=None, lb=None, ub=None, out=None, dtype=None, device=None, use_eval_dtype=False, generator=None)

Make a new or existing tensor filled by uniformly distributed values. Both lower and upper bounds are inclusive. This function can work with both float and int dtypes.

When not explicitly specified via arguments, the dtype and the device of the resulting tensor is determined by this method's parent object.

Parameters:

Name Type Description Default
size Size

Size of the new tensor to be filled with uniformly distributed values. This can be given as multiple positional arguments, each such positional argument being an integer, or as a single positional argument of a tuple, the tuple containing multiple integers. Note that, if the user wishes to fill an existing tensor instead, then no positional argument is expected.

()
num_solutions Optional[int]

This can be used instead of the size arguments for specifying the shape of the target tensor. Expected as an integer, when num_solutions is specified as n, the shape of the resulting tensor will be (n, m) where m is the solution length reported by this method's parent object's solution_length attribute.

None
lb Optional[RealOrVector]

Lower bound for the uniformly distributed values. Can be a scalar, or a tensor. If not specified, the lower bound will be taken as 0. Note that, if one specifies lb, then ub is also expected to be explicitly specified.

None
ub Optional[RealOrVector]

Upper bound for the uniformly distributed values. Can be a scalar, or a tensor. If not specified, the upper bound will be taken as 1. Note that, if one specifies ub, then lb is also expected to be explicitly specified.

None
out Optional[Tensor]

Optionally, the tensor to be filled by uniformly distributed values. If an out tensor is given, then no size argument is expected.

None
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32). If dtype is not specified (and also out is None), it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object. If an out tensor is specified, then dtype is expected as None.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object. If an out tensor is specified, then device is expected as None.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
generator Any

Pseudo-random generator to be used when sampling the values. Can be a torch.Generator or any object with a generator attribute (e.g. a Problem object). If not given, then this method's parent object will be analyzed whether or not it has its own generator. If it does, that generator will be used. If not, the global generator of PyTorch will be used.

None
Source code in evotorch/tools/tensormaker.py
def make_uniform(
    self,
    *size: Size,
    num_solutions: Optional[int] = None,
    lb: Optional[RealOrVector] = None,
    ub: Optional[RealOrVector] = None,
    out: Optional[torch.Tensor] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
    generator: Any = None,
) -> torch.Tensor:
    """
    Make a new or existing tensor filled by uniformly distributed values.
    Both lower and upper bounds are inclusive.
    This function can work with both float and int dtypes.

    When not explicitly specified via arguments, the dtype and the device
    of the resulting tensor is determined by this method's parent object.

    Args:
        size: Size of the new tensor to be filled with uniformly distributed
            values. This can be given as multiple positional arguments, each
            such positional argument being an integer, or as a single
            positional argument of a tuple, the tuple containing multiple
            integers. Note that, if the user wishes to fill an existing
            tensor instead, then no positional argument is expected.
        num_solutions: This can be used instead of the `size` arguments
            for specifying the shape of the target tensor.
            Expected as an integer, when `num_solutions` is specified
            as `n`, the shape of the resulting tensor will be
            `(n, m)` where `m` is the solution length reported by this
            method's parent object's `solution_length` attribute.
        lb: Lower bound for the uniformly distributed values.
            Can be a scalar, or a tensor.
            If not specified, the lower bound will be taken as 0.
            Note that, if one specifies `lb`, then `ub` is also expected to
            be explicitly specified.
        ub: Upper bound for the uniformly distributed values.
            Can be a scalar, or a tensor.
            If not specified, the upper bound will be taken as 1.
            Note that, if one specifies `ub`, then `lb` is also expected to
            be explicitly specified.
        out: Optionally, the tensor to be filled by uniformly distributed
            values. If an `out` tensor is given, then no `size` argument is
            expected.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32).
            If `dtype` is not specified (and also `out` is None),
            it will be assumed that the user wishes to create a tensor
            using the dtype of this method's parent object.
            If an `out` tensor is specified, then `dtype` is expected
            as None.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
            If an `out` tensor is specified, then `device` is expected
            as None.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
        generator: Pseudo-random generator to be used when sampling
            the values. Can be a `torch.Generator` or any object with
            a `generator` attribute (e.g. a Problem object).
            If not given, then this method's parent object will be
            analyzed whether or not it has its own generator.
            If it does, that generator will be used.
            If not, the global generator of PyTorch will be used.
    Returns:
        The created or modified tensor after placing the uniformly
        distributed values.
    """
    args, kwargs = self.__get_all_args_for_random_maker(
        *size,
        num_solutions=num_solutions,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
        generator=generator,
    )
    return misc.make_uniform(*args, lb=lb, ub=ub, **kwargs)

make_uniform_shaped_like(t, *, lb=None, ub=None)

Make a new uniformly-filled tensor, shaped like the given tensor. The dtype and device will be determined by the parent of this method (not by the given tensor). If the parent of this method has its own random generator, then that generator will be used.

Parameters:

Name Type Description Default
t Tensor

The tensor according to which the result will be shaped.

required
lb Optional[RealOrVector]

The inclusive lower bounds for the uniform distribution. Can be a scalar or a tensor. If left as None, 0.0 will be used as the upper bound.

None
ub Optional[RealOrVector]

The inclusive upper bounds for the uniform distribution. Can be a scalar or a tensor. If left as None, 1.0 will be used as the upper bound.

None
Source code in evotorch/tools/tensormaker.py
def make_uniform_shaped_like(
    self,
    t: torch.Tensor,
    *,
    lb: Optional[RealOrVector] = None,
    ub: Optional[RealOrVector] = None,
) -> torch.Tensor:
    """
    Make a new uniformly-filled tensor, shaped like the given tensor.
    The `dtype` and `device` will be determined by the parent of this
    method (not by the given tensor).
    If the parent of this method has its own random generator, then that
    generator will be used.

    Args:
        t: The tensor according to which the result will be shaped.
        lb: The inclusive lower bounds for the uniform distribution.
            Can be a scalar or a tensor.
            If left as None, 0.0 will be used as the upper bound.
        ub: The inclusive upper bounds for the uniform distribution.
            Can be a scalar or a tensor.
            If left as None, 1.0 will be used as the upper bound.
    Returns:
        A new tensor whose shape is the same with the given tensor.
    """
    return self.make_uniform(t.shape, lb=lb, ub=ub)

make_zeros(*size, num_solutions=None, out=None, dtype=None, device=None, use_eval_dtype=False)

Make a new tensor filled with 0, or fill an existing tensor with 0.

When not explicitly specified via arguments, the dtype and the device of the resulting tensor is determined by this method's parent object.

Parameters:

Name Type Description Default
size Size

Size of the new tensor to be filled with 0. This can be given as multiple positional arguments, each such positional argument being an integer, or as a single positional argument of a tuple, the tuple containing multiple integers. Note that, if the user wishes to fill an existing tensor with 0 values, then no positional argument is expected.

()
num_solutions Optional[int]

This can be used instead of the size arguments for specifying the shape of the target tensor. Expected as an integer, when num_solutions is specified as n, the shape of the resulting tensor will be (n, m) where m is the solution length reported by this method's parent object's solution_length attribute.

None
out Optional[Tensor]

Optionally, the tensor to be filled by 0 values. If an out tensor is given, then no size argument is expected.

None
dtype Optional[DType]

Optionally a string (e.g. "float32") or a PyTorch dtype (e.g. torch.float32). If dtype is not specified (and also out is None), it will be assumed that the user wishes to create a tensor using the dtype of this method's parent object. If an out tensor is specified, then dtype is expected as None.

None
device Optional[Device]

The device in which the new empty tensor will be stored. If not specified (and also out is None), it will be assumed that the user wishes to create a tensor on the same device with this method's parent object. If an out tensor is specified, then device is expected as None.

None
use_eval_dtype bool

If this is given as True and a dtype is not specified, then the dtype of the result will be taken from the eval_dtype attribute of this method's parent object.

False
Source code in evotorch/tools/tensormaker.py
def make_zeros(
    self,
    *size: Size,
    num_solutions: Optional[int] = None,
    out: Optional[torch.Tensor] = None,
    dtype: Optional[DType] = None,
    device: Optional[Device] = None,
    use_eval_dtype: bool = False,
) -> torch.Tensor:
    """
    Make a new tensor filled with 0, or fill an existing tensor with 0.

    When not explicitly specified via arguments, the dtype and the device
    of the resulting tensor is determined by this method's parent object.

    Args:
        size: Size of the new tensor to be filled with 0.
            This can be given as multiple positional arguments, each such
            positional argument being an integer, or as a single positional
            argument of a tuple, the tuple containing multiple integers.
            Note that, if the user wishes to fill an existing tensor with
            0 values, then no positional argument is expected.
        num_solutions: This can be used instead of the `size` arguments
            for specifying the shape of the target tensor.
            Expected as an integer, when `num_solutions` is specified
            as `n`, the shape of the resulting tensor will be
            `(n, m)` where `m` is the solution length reported by this
            method's parent object's `solution_length` attribute.
        out: Optionally, the tensor to be filled by 0 values.
            If an `out` tensor is given, then no `size` argument is expected.
        dtype: Optionally a string (e.g. "float32") or a PyTorch dtype
            (e.g. torch.float32).
            If `dtype` is not specified (and also `out` is None),
            it will be assumed that the user wishes to create a tensor
            using the dtype of this method's parent object.
            If an `out` tensor is specified, then `dtype` is expected
            as None.
        device: The device in which the new empty tensor will be stored.
            If not specified (and also `out` is None), it will be
            assumed that the user wishes to create a tensor on the
            same device with this method's parent object.
            If an `out` tensor is specified, then `device` is expected
            as None.
        use_eval_dtype: If this is given as True and a `dtype` is not
            specified, then the `dtype` of the result will be taken
            from the `eval_dtype` attribute of this method's parent
            object.
    Returns:
        The created or modified tensor after placing 0 values.
    """

    args, kwargs = self.__get_all_args_for_maker(
        *size,
        num_solutions=num_solutions,
        out=out,
        dtype=dtype,
        device=device,
        use_eval_dtype=use_eval_dtype,
    )
    return misc.make_zeros(*args, **kwargs)