Skip to content

Commit

Permalink
Merge pull request #5 from honglu2875/gergely
Browse files Browse the repository at this point in the history
gergely
  • Loading branch information
honglu2875 authored Jul 18, 2022
2 parents d80e4e9 + fdb4cb8 commit 6670d3c
Show file tree
Hide file tree
Showing 28 changed files with 469 additions and 105 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# Hironaka

A utility package for reinforcement learning study of Hironaka's game of local resolution of singularities and its variation problems.
A utility package for reinforcement learning study of Hironaka's game of local resolution of singularities and its
variation problems.

For ML and RL specialists, the following two sections should give you an overview:

- [Rule of the game](#rule-of-the-game)
- [The structure of the repo](#the-structure-of-the-repo)

For math-oriented viewers or ML experts who are intrigued about the background story, please feel free to continue with:

- [What is a resolution of singularity](#what-is-a-resolution-of-singularity)
- [What is Hironaka's polyhedral game](#what-is-hironakas-polyhedral-game)
- [Variations of Hironaka's game](#variations-of-hironakas-game)
- [Further topics](#further-topics)

# Rule of the game

There are two players. To separate the roles, we name them 'host' and 'agent'.

Game state: a set of integral points .......
Expand All @@ -24,29 +28,49 @@ Agent: ......
......

# The structure of the repo

......

---
Now the big question is, how is this game related to some fundamental questions in pure math, or specifically, algebraic geometry.
Now the big question is, how is this game related to some fundamental questions in pure math, or specifically, algebraic
geometry.

# What is a resolution of singularity

[intro]

## Smoothness

[definition] & [examples]

## Singularities

[examples]

## Blow-up: turning singularities into smooth points

[examples]

## Hironaka's theorem

[statement]

# What is Hironaka's polyhedral game

## Monomial ideals

[definition] & [examples]

## Rephrasing the local resolution of singularity problem

[definitions]

# Variations of Hironaka's game

## Thom game

## Singularity theory and Thom polynomial

# Further topics

...
42 changes: 35 additions & 7 deletions hironaka/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
from typing import List

import numpy as np

from .core import Points
from .policy import Policy

from hironaka.core import Points
from hironaka.policy.Policy import Policy

class Agent(abc.ABC):
"""
An agent can either modify the points in-place, or just return the action (the chosen coordinate)
"""

@abc.abstractmethod
def move(self, points: Points, coords, inplace=True):
def move(self, points: Points, coords, weights, inplace=True):
pass


class RandomAgent(Agent):
def move(self, points: Points, coords, inplace=True):
def move(self, points: Points, coords, weights=None, inplace=True):
assert weights is None, "Only support agents without weights."

actions = [np.random.choice(coord, size=1)[0] if len(coord) > 1 else None for coord in coords]
if not inplace:
return actions
Expand All @@ -28,7 +28,9 @@ def move(self, points: Points, coords, inplace=True):


class ChooseFirstAgent(Agent):
def move(self, points: Points, coords, inplace=True):
def move(self, points: Points, coords, weights=None, inplace=True):
assert weights is None, "Only support agents without weights."

actions = [min(coord) if len(coord) > 1 else None for coord in coords]
if not inplace:
return actions
Expand All @@ -53,3 +55,29 @@ def move(self, points: Points, coords: List[List[int]], inplace=True):
points.get_newton_polytope()

return actions


class AgentMorin(Agent):
def move(self, points: Points, coords, weights, inplace=True):
assert points.batch_size == 1, "Temporarily only support batch size 1." # TODO: generalize!
weights_unravelled = weights[0]
coords_unravelled = coords[0]

if weights_unravelled[coords_unravelled[0]] == weights_unravelled[coords_unravelled[1]]:
action = np.random.choice(coords_unravelled, size=1)[0]
else:
action = coords_unravelled[np.argmin(
[weights_unravelled[coords_unravelled[0]], weights_unravelled[coords_unravelled[1]]]
)]
changing_coordinate = [coord for coord in coords_unravelled if coord != action]
next_weights = [weights_unravelled[i] if i not in changing_coordinate else 0
for i in range(len(weights_unravelled))]

if inplace:
points.shift([coords_unravelled], [action])
points.reposition()
points.get_newton_polytope()
weights[:] = next_weights
return [action]
return [action], [next_weights]

2 changes: 1 addition & 1 deletion hironaka/core/Points.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from hironaka.core.PointsBase import PointsBase
from .PointsBase import PointsBase
from hironaka.src import shift_lst, get_newton_polytope_lst, get_shape, scale_points, reposition_lst, \
get_newton_polytope_approx_lst

Expand Down
4 changes: 2 additions & 2 deletions hironaka/core/PointsNumpy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hironaka.core.PointsBase import PointsBase
from .PointsBase import PointsBase


class PointsNumpy(PointsBase): # TODO:INCOMPLETE
class PointsNumpy(PointsBase): # INCOMPLETE
"""
Storing points using numpy arrays.
"""
Expand Down
2 changes: 1 addition & 1 deletion hironaka/core/PointsTensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import torch

from hironaka.core.PointsBase import PointsBase
from .PointsBase import PointsBase
from hironaka.src import get_batched_padded_array, rescale_torch
from hironaka.src import shift_torch, get_newton_polytope_torch, reposition_torch

Expand Down
1 change: 1 addition & 0 deletions hironaka/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .Points import *
from .PointsTensor import *

105 changes: 104 additions & 1 deletion hironaka/game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import abc
import logging
from typing import Optional
from typing import Optional, Union

from hironaka.core import Points
from hironaka.agent import Agent
from hironaka.host import Host


class Game(abc.ABC):
Expand Down Expand Up @@ -46,8 +50,107 @@ def step(self, verbose: int = 0) -> bool:
"""
pass

def _show(self, coords, action, weights, ended):
self.logger.info(f"Host move: {coords}")
self.logger.info(f"Agent move: {action}")
if weights is not None:
self.logger.info(f"Weights: {weights}")
self.logger.info(f"Game Ended: {ended}")

def print_history(self):
self.logger.info("Coordinate history (host choices):")
self.logger.info(self.coord_history)
self.logger.info("Move history (agent choices):")
self.logger.info(self.move_history)


class GameHironaka(Game):
def __init__(self,
state: Union[Points, None],
host: Host,
agent: Agent,
scale_observation: Optional[bool] = True,
**kwargs):
if self.logger is None:
self.logger = logging.getLogger(__class__.__name__)

super().__init__(state, host, agent, **kwargs)
self.scale_observation = scale_observation
self.stopped = False

def step(self, verbose: int = 0) -> bool:
"""
Make one move forward.
In particular,
1. the host selects coordinates
(use: Host.selectCoord(state));
2. the agent makes one move according to the selected coordinates
(use: Agent.move(state, coords)).
Return: True if successful, False if unsuccessful
"""
if self.stopped:
return False

if verbose:
self.logger.info(self.state)

coords = self.host.select_coord(self.state)
action = self.agent.move(self.state, coords)
if self.scale_observation:
self.state.rescale()

if verbose:
self._show(coords, action, None, self.state.ended)

self.coord_history.append(coords)
self.move_history.append(action)

if self.state.ended:
self.stopped = True
return False
return True


class GameMorin(Game):
"""The agent is Thom (picks the action coordinate with smallest weight), but the game terminates with a
label 'NO CONTRIBUTION' if the distinguished Porteous point is not a vertex of the Newton polytope
after the shift"""

def __init__(self,
state: Union[Points, None],
host: Host,
agent: Agent,
scale_observation: Optional[bool] = True,
**kwargs):
if self.logger is None:
self.logger = logging.getLogger(__class__.__name__)

super().__init__(state, host, agent, **kwargs)
self.weights = [[1] * self.state.dimension for _ in range(self.state.batch_size)]
self.scale_observation = scale_observation
self.stopped = False

def step(self, verbose: int = 0) -> bool:
if self.stopped:
return False

if verbose:
self.logger.info(self.state)

coords = self.host.select_coord(self.state)
action = self.agent.move(self.state, self.weights, coords, inplace=True)

if verbose:
self._show(coords, action, None, self.state.ended)

self.coord_history.append(coords)
self.move_history.append(action)

if self.state.ended or self.state.distinguished_points[0] is None:
self.stopped = True
if self.state.distinguished_points[0] is None:
self.logger.info("No contribution.")
return False
return True
60 changes: 0 additions & 60 deletions hironaka/gameHironaka.py

This file was deleted.

2 changes: 1 addition & 1 deletion hironaka/gym_env/HironakaAgentEnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from gym import spaces

from hironaka.agent import Agent
from hironaka.gym_env.HironakaBase import HironakaBase
from .HironakaBase import HironakaBase
from hironaka.src import decode_action


Expand Down
3 changes: 2 additions & 1 deletion hironaka/gym_env/HironakaBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def __init__(self,
shape=(self.max_number_points, self.dimension),
dtype=np.float32)
else:
self.point_observation_space = spaces.Box(low=-1.0, high=1.0, shape=(self.max_number_points, self.dimension),
self.point_observation_space = spaces.Box(low=-1.0, high=1.0,
shape=(self.max_number_points, self.dimension),
dtype=np.float32)

# Configs to pass down to other functions
Expand Down
2 changes: 1 addition & 1 deletion hironaka/gym_env/HironakaHostEnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from gym import spaces

from hironaka.gym_env.HironakaBase import HironakaBase
from .HironakaBase import HironakaBase
from hironaka.host import Host


Expand Down
Loading

0 comments on commit 6670d3c

Please sign in to comment.