Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleLittleCloud committed Jan 10, 2024
1 parent d0d8569 commit ac45b63
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 182 deletions.
33 changes: 33 additions & 0 deletions src/Microsoft.ML.Core/SearchSpace/BoolearnChoiceAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.ML.SearchSpace;

/// <summary>
/// Boolean choice attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class BooleanChoiceAttribute : Attribute
{
/// <summary>
/// Create a <see cref="BooleanChoiceAttribute"/>.
/// </summary>
public BooleanChoiceAttribute()
{
DefaultValue = true;
}

/// <summary>
/// Create a <see cref="BooleanChoiceAttribute"/> with default value.
/// </summary>
/// <param name="defaultValue">default value for this option.</param>
public BooleanChoiceAttribute(bool defaultValue)
{
DefaultValue = defaultValue;
}

public bool DefaultValue { get; }
}
50 changes: 50 additions & 0 deletions src/Microsoft.ML.Core/SearchSpace/ChoiceAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.Contracts;
using System.Linq;

namespace Microsoft.ML.SearchSpace;

/// <summary>
/// Choice attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class ChoiceAttribute : Attribute
{
/// <summary>
/// Create a <see cref="ChoiceAttribute"/> with <paramref name="candidates"/>.
/// </summary>
public ChoiceAttribute(params object[] candidates)
{
var candidatesType = candidates.Select(o => o.GetType()).Distinct();
Contract.Assert(candidatesType.Count() == 1, "multiple candidates type detected");
this.Candidates = candidates;
this.DefaultValue = null;
}

/// <summary>
/// Create a <see cref="ChoiceAttribute"/> with <paramref name="candidates"/> and <paramref name="defaultValue"/>.
/// </summary>
public ChoiceAttribute(object[] candidates, object defaultValue)
{
var candidatesType = candidates.Select(o => o.GetType()).Distinct();
Contract.Assert(candidatesType.Count() == 1, "multiple candidates type detected");
Contract.Assert(candidatesType.First() == defaultValue.GetType(), "candidates type doesn't match with defaultValue type");

this.Candidates = candidates;
this.DefaultValue = defaultValue;
}

/// <summary>
/// Get the candidates of this option.
/// </summary>
public object[] Candidates { get; }

/// <summary>
/// Get the default value of this option.
/// </summary>
public object DefaultValue { get; }
}
21 changes: 21 additions & 0 deletions src/Microsoft.ML.Core/SearchSpace/NestOptionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.ML.SearchSpace;

/// <summary>
/// attribution class for nest option.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class NestOptionAttribute : Attribute
{
/// <summary>
/// Create an <see cref="NestOptionAttribute"/>.
/// </summary>
public NestOptionAttribute()
{
}
}
88 changes: 88 additions & 0 deletions src/Microsoft.ML.Core/SearchSpace/RangeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.ML.SearchSpace;

/// <summary>
/// Range attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class RangeAttribute : Attribute
{
/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(double min, double max, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = null;
this.LogBase = logBase;
}

/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(double min, double max, double init, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = init;
this.LogBase = logBase;
}

/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(int min, int max, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = null;
this.LogBase = logBase;
}

/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(int min, int max, int init, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = init;
this.LogBase = logBase;
}

/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(float min, float max, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = null;
this.LogBase = logBase;
}

/// <summary>
/// Create a <see cref="RangeAttribute"/>
/// </summary>
public RangeAttribute(float min, float max, float init, bool logBase = false)
{
this.Min = min;
this.Max = max;
this.Init = init;
this.LogBase = logBase;
}

public object Min { get; }

public object Max { get; }

public object Init { get; }

public bool LogBase { get; }
}
37 changes: 0 additions & 37 deletions src/Microsoft.ML.SearchSpace/Attribute/BooleanChoiceAttribute.cs

This file was deleted.

46 changes: 0 additions & 46 deletions src/Microsoft.ML.SearchSpace/Attribute/ChoiceAttribute.cs

This file was deleted.

22 changes: 0 additions & 22 deletions src/Microsoft.ML.SearchSpace/Attribute/NestOptionAttribute.cs

This file was deleted.

66 changes: 0 additions & 66 deletions src/Microsoft.ML.SearchSpace/Attribute/RangeAttribute.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonVersion)" />
</ItemGroup>
</Project>
Loading

0 comments on commit ac45b63

Please sign in to comment.