-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev/3.0' into feature/refactor_graph_partition
- Loading branch information
Showing
24 changed files
with
976 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/Nncase.Schedule/Schedule/MonteCarloTreeSearch/IEnvironmentState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Canaan Inc. All rights reserved. | ||
// Licensed under the Apache license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Nncase.Schedule.MonteCarloTreeSearch; | ||
|
||
public interface IEnvironmentState<TAction> | ||
where TAction : class | ||
{ | ||
int LegalActions(); | ||
|
||
TAction GetNextAction(int index); | ||
|
||
IEnvironmentState<TAction>? PerformAction(TAction action); | ||
|
||
double RollOut(); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Nncase.Schedule/Schedule/MonteCarloTreeSearch/SearchNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Canaan Inc. All rights reserved. | ||
// Licensed under the Apache license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Nncase.Schedule.MonteCarloTreeSearch; | ||
|
||
public abstract class SearchNode<T> | ||
where T : class | ||
{ | ||
public SearchNode(IEnvironmentState<T> state) | ||
{ | ||
Parent = null; | ||
Children = new List<SearchNode<T>>(); | ||
VisitTimes = 0; | ||
QualityValue = 0.0; | ||
State = state; | ||
} | ||
|
||
public SearchNode(SearchNode<T> parent, IEnvironmentState<T> state) | ||
{ | ||
Parent = parent; | ||
Children = new List<SearchNode<T>>(); | ||
VisitTimes = 0; | ||
QualityValue = 0.0; | ||
State = state; | ||
Parent.Children.Add(this); | ||
} | ||
|
||
public SearchNode<T>? Parent { get; } | ||
|
||
public List<SearchNode<T>> Children { get; } | ||
|
||
public int VisitTimes { get; set; } | ||
|
||
public double QualityValue { get; set; } | ||
|
||
public IEnvironmentState<T> State { get; } | ||
|
||
public bool IsRootNode => Parent is null; | ||
|
||
public abstract void Update(double reward); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Nncase.Schedule/Schedule/MonteCarloTreeSearch/Searcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Canaan Inc. All rights reserved. | ||
// Licensed under the Apache license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Nncase.Schedule.MonteCarloTreeSearch; | ||
|
||
public abstract class Searcher<T> | ||
where T : class | ||
{ | ||
public Searcher(int searchTimes = 20) | ||
{ | ||
SearchTimes = searchTimes; | ||
} | ||
|
||
public int SearchTimes { get; } | ||
|
||
public void Search(SearchNode<T> rootNode) | ||
{ | ||
for (int i = 0; i < SearchTimes; i++) | ||
{ | ||
if (!Selection(rootNode, out var node)) | ||
{ | ||
return; | ||
} | ||
|
||
var expanded = Expand(node); | ||
if (expanded is not null) | ||
{ | ||
BackPropagate(expanded, Simulation(expanded)); | ||
} | ||
else | ||
{ | ||
BackPropagate(node, double.PositiveInfinity); | ||
} | ||
} | ||
} | ||
|
||
public abstract bool Selection(SearchNode<T> node, out SearchNode<T> selected); | ||
|
||
public abstract SearchNode<T>? Expand(SearchNode<T> node); | ||
|
||
public abstract double Simulation(SearchNode<T> node); | ||
|
||
public abstract void BackPropagate(SearchNode<T> node, double reward); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.