0

I am trying to implement a C# interface in IronPython but am having some trouble. I've done this before with another C# interface but have come across another one which I've not been able to resolve how to subclass in IronPython to use it successfully, here it is:

The C# interface that I want to implement in IronPython:

using System;
namespace Accord.Math.Random

{
    public interface IRandomNumberGenerator
    {
        float Mean
        {
            get;
        }
        float Variance
        {
            get;
        }
        float Next();
        void SetSeed(int seed);
    }
}

Here is what I have done before successfully in the past:

C# interface to implement

using System;
namespace Accord.Genetic
{
    public interface IFitnessFunction
    {
        double Evaluate(IChromosome chromosome);
    }
}

IronPython implementation:

class FitnessFunction(AG.IFitnessFunction):
    def Evaluate(self, chromosome):
        #some fitness calculation using chromosome
        return Fitness

Any help would be so much appreciated!

1 Answer 1

1

I sorted it out! I figured that the interface in this instance was actually calling some other methods in the Accord.Math.Random namespace, so I went looking for which methods these were.

I also ended up using a more updated version of the IRandomNumberGenerator interface, which was IRandomNumberGenerator[T]

Heres the C# version

using System;
namespace Accord.Math.Random
{
    public interface IRandomNumberGenerator<T>
    {
        T[] Generate(int samples);
        T[] Generate(int samples, T[] result);
        T Generate();
    }
}

Heres the working IronPython version

class RandomNumberGenerator(AM.Random.IRandomNumberGenerator[System.Double]):
    def __init__(self):
        self.actual = AM.Random.ZigguratUniformOneGenerator()
    def Generate(self):
        return self.actual.Generate()
    def Generate(self, samples):
        return self.actual.Generate(samples)
    def Generate(self, samples, result):
        return self.actual.Generate(samples, result)

I've so far been able to use it for my purposes. If anyone notices any issues in the code or knows of a more proper implementation, please post!

Cheers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.