Add a simple Categorical space
This commit is contained in:
parent
95e304495f
commit
d09cb2fe65
34
.github/workflows/test.yml
vendored
Normal file
34
.github/workflows/test.yml
vendored
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
name: Run Python Tests for Spaces
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-16.04, ubuntu-18.04, ubuntu-20.04, macos-latest]
|
||||||
|
python-version: [3.6, 3.7, 3.8, 3.9]
|
||||||
|
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
|
uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
- name: Test Search Space
|
||||||
|
run: |
|
||||||
|
python -m pip install pytest
|
||||||
|
echo $PWD
|
||||||
|
ls
|
||||||
|
python --version
|
||||||
|
python -m pytest AutoDL-Projects/tests --durations=0
|
||||||
|
|
@ -1 +1 @@
|
|||||||
Subproject commit 51187c1e9152ff79b02b11c80bca0b03b402a7e5
|
Subproject commit fbfcba7eb3ad19e862eca32ee0bb3195f1a8a9ef
|
@ -1 +1,7 @@
|
|||||||
#
|
#####################################################
|
||||||
|
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.01 #
|
||||||
|
#####################################################
|
||||||
|
# Define complex searc space for AutoDL #
|
||||||
|
#####################################################
|
||||||
|
|
||||||
|
from .basic_space import Categorical
|
||||||
|
33
lib/spaces/basic_space.py
Normal file
33
lib/spaces/basic_space.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#####################################################
|
||||||
|
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.01 #
|
||||||
|
#####################################################
|
||||||
|
|
||||||
|
import abc
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
class Space(metaclass=abc.ABCMeta):
|
||||||
|
@abc.abstractmethod
|
||||||
|
def random(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def __repr__(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class Categorical(Space):
|
||||||
|
def __init__(self, *data):
|
||||||
|
self._candidates = [*data]
|
||||||
|
|
||||||
|
def __getitem__(self, index):
|
||||||
|
return self._candidates[index]
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self._candidates)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "{name:}(candidates={cs:})".format(name=self.__class__.__name__, cs=self._candidates)
|
||||||
|
|
||||||
|
def random(self):
|
||||||
|
return random.choice(self._candidates)
|
22
tests/test_basic.py
Normal file
22
tests/test_basic.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#####################################################
|
||||||
|
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.03 #
|
||||||
|
#####################################################
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
import pytest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
lib_dir = (Path(__file__).parent / ".." / "lib").resolve()
|
||||||
|
print("library path: {:}".format(lib_dir))
|
||||||
|
if str(lib_dir) not in sys.path:
|
||||||
|
sys.path.insert(0, str(lib_dir))
|
||||||
|
|
||||||
|
from spaces import Categorical
|
||||||
|
|
||||||
|
|
||||||
|
class TestBasicSpace(unittest.TestCase):
|
||||||
|
def test_categorical(self):
|
||||||
|
space = Categorical(1, 2, 3, 4)
|
||||||
|
for i in range(4):
|
||||||
|
self.assertEqual(space[i], i + 1)
|
||||||
|
self.assertEqual("Categorical(candidates=[1, 2, 3, 4])", str(space))
|
Loading…
Reference in New Issue
Block a user