Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix codestyle, modules structure, convex mutations bugs, add py cfg #69

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gefest/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

194 changes: 0 additions & 194 deletions gefest/core/algs/geom/validation.py

This file was deleted.

Empty file.
Empty file.
4 changes: 2 additions & 2 deletions gefest/core/configs/optimization_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from golem.core.optimisers.genetic.operators.selection import SelectionTypesEnum
from pydantic import BaseModel, ConfigDict, model_validator

from gefest.core.algs.postproc.resolve_errors import Postrocessor
from gefest.core.algs.postproc.rules import PolygonRule, Rules, StructureRule
from gefest.core.configs.tuner_params import TunerParams
from gefest.core.geometry.domain import Domain
from gefest.core.opt.adapters.structure import StructureAdapter
from gefest.core.opt.objective.objective import Objective
from gefest.core.opt.operators.crossovers import CrossoverTypes, panmixis
from gefest.core.opt.operators.mutations import MutationTypes
from gefest.core.opt.operators.selections import SelectionTypes
from gefest.core.opt.postproc.resolve_errors import Postrocessor
from gefest.core.opt.postproc.rules import PolygonRule, Rules, StructureRule
from gefest.core.utils.logger import LogDispatcher
from gefest.tools.samplers.standard.standard import StandardSampler

Expand Down
5 changes: 4 additions & 1 deletion gefest/core/configs/tuner_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from hyperopt import hp
from pydantic import BaseModel, ConfigDict, field_validator

from gefest.core.opt.tuning import utils
from gefest.tools.tuners import utils


class TunerParams(BaseModel):
Expand All @@ -22,6 +22,7 @@ class TunerParams(BaseModel):
timeout_minutes: int = 60

@field_validator('tuner_type')
@classmethod
def tuner_type_validate(cls, value):
if isinstance(value, str):
opt_names = ['iopt', 'optuna', 'sequential', 'simulataneous']
Expand All @@ -33,6 +34,7 @@ def tuner_type_validate(cls, value):
raise ValueError(f'Invalid argument: {value} of type {type(value)}.')

@field_validator('hyperopt_dist')
@classmethod
def hyperopt_fun_validate(cls, value):
if isinstance(value, str):
r_ = inspect.getmembers(hp, inspect.isfunction)
Expand All @@ -48,6 +50,7 @@ def hyperopt_fun_validate(cls, value):
raise ValueError(f'Invalid argument: {value} of type {type(value)}.')

@field_validator('variacne_generator')
@classmethod
def variacne_generator_fun_validate(cls, value):
fun_names = ['average_edge_variance']
if isinstance(value, str):
Expand Down
62 changes: 27 additions & 35 deletions gefest/core/configs/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import importlib.util
import inspect
import sys
from pathlib import Path

import yaml
Expand All @@ -21,49 +19,43 @@ def __str__(self):
return f'ParseMetricsError. {self.message}'


def dynamic_import(name):
import __main__

path = inspect.getfile(__main__)
components = name.split('.')
module = __import__('.'.join(components[:-1]), fromlist=[components[-1]])
import_item = getattr(module, components[-1])
return import_item


def load_config(
cfg_file_path: str,
metrics_file_path: str,
cfg_py_path: str,
cfg_yaml_path: str = None,
*args,
**kwargs,
) -> OptimizationParams:
"""Generates configuretion files from yaml files.

Args:
cfg_file_path (str): Path to config.yaml.
metrics_file_path (str): Path to metrics.py.
cfg_py_path (str): Path to metrics.py.
cfg_yaml_path (str): Path to config.yaml.

Returns:
OptimizationParams: GEFEST unified configuretion file.
"""
config_dict = yaml.safe_load(Path(cfg_file_path).read_text())
domain_cfg = Domain.model_validate(config_dict['domain'])
tuner_cfg = TunerParams.model_validate(config_dict['tuner_params'])

module_ = __import__(Path(metrics_file_path).stem)
user_metrics = []
for name, obj in inspect.getmembers(module_):
if inspect.isclass(obj):
if issubclass(obj, Objective) and obj is not Objective:
user_metrics.append(name)

if not user_metrics:
raise ParseMetricsError('No Objective class has been loaded.')

config_dict['opt_params']['domain'] = domain_cfg
config_dict['opt_params']['tuner_cfg'] = tuner_cfg
config_dict['opt_params']['objectives'] = [
getattr(module_, metric)(domain_cfg) for metric in user_metrics
]
opt_params = OptimizationParams.model_validate(config_dict['opt_params'])
module_ = __import__(Path(cfg_py_path).stem)
if cfg_yaml_path:
user_metrics = []
for name, obj in inspect.getmembers(module_):
if inspect.isclass(obj):
if issubclass(obj, Objective) and obj is not Objective:
user_metrics.append(name)

if not user_metrics:
raise ParseMetricsError('No Objective class has been loaded.')

config_dict = yaml.safe_load(Path(cfg_yaml_path).read_text())
domain_cfg = Domain.model_validate(config_dict['domain'])
tuner_cfg = TunerParams.model_validate(config_dict['tuner_params'])

config_dict['opt_params']['domain'] = domain_cfg
config_dict['opt_params']['tuner_cfg'] = tuner_cfg
config_dict['opt_params']['objectives'] = [
getattr(module_, metric)(domain_cfg) for metric in user_metrics
]
opt_params = OptimizationParams.model_validate(config_dict['opt_params'])
else:
opt_params = module_.opt_params
return opt_params
1 change: 0 additions & 1 deletion gefest/core/geometry/datastructs/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

4 changes: 4 additions & 0 deletions gefest/core/geometry/datastructs/structure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union
from uuid import UUID, uuid4

from loguru import logger
from pydantic import Field
from pydantic.dataclasses import dataclass

Expand All @@ -21,6 +22,9 @@ def __len__(self):
def __setattr__(self, name, value):
if name in ['polygons']:
self.fitness = []
lens = list(map(len, value))
if any(x < 2 for x in lens):
logger.trace('bruh')
super().__setattr__(name, value)

def __setitem__(self, key, value):
Expand Down
6 changes: 6 additions & 0 deletions gefest/core/geometry/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,35 @@ def create_classes_instances(self):
return self

@field_validator('min_poly_num')
@classmethod
def validate_min_poly_num(cls, data: int):
if data < 1:
raise ValueError('Min number of polygons must be positive value.')
return data

@field_validator('max_poly_num')
@classmethod
def validate_max_poly_num(cls, data: int):
if data < 1:
raise ValueError('Max number of polygons must be positive value.')
return data

@field_validator('min_points_num')
@classmethod
def validate_min_points_num(cls, data: int):
if data < 1:
raise ValueError('Max number of polygons must be positive value.')
return data

@field_validator('fixed_points')
@classmethod
def validate_fixed_points(cls, data: Union[Polygon, list[tuple[float, float]]]):
if isinstance(data, Polygon):
return data
return Polygon([Point(*coords) for coords in data])

@field_validator('prohibited_area')
@classmethod
def validate_prohibited_area(cls, data: Optional[Union[Structure, str]]):
if isinstance(data, Structure):
return data
Expand All @@ -88,6 +93,7 @@ def validate_prohibited_area(cls, data: Optional[Union[Structure, str]]):
raise TypeError(f'Invalid argument {data}.')

@field_validator('allowed_area')
@classmethod
def validate_allowed_area(cls, data: Union[Polygon, list[list[float]]]):
if data is None or len(data) <= 2:
raise ValueError('Not enough points for allowed_area.')
Expand Down
Loading