0

I am making a genetic algorithm and I have a sorting problem: I have such a class, it is represented as std::vector

class GA_Class {
public:
float Fitness = 0.0f;

bool Parent = 0;
};

typedef std::vector<GA_Class> GA_Vector;

Also I have a main class

class GA_Population {
private:
std::vector<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;
GA_Vector PopulationParametrs;
private:
bool FitnessSort(size_t Unit);
public:
void FitnessSorting();
};

My problem is contained in the following: I need somehow to sort the vectors of the variable std::vector<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs; by GA_Vector PopulationParametrs;

I made such an implementation


    bool GA_Population::FitnessSort(size_t Unit)
    {
        return (PopulationParametrs[Unit].Fitness < PopulationParametrs[Unit].Fitness);
    }

    void GA_Population::FitnessSorting()
    {
        std::sort(PopulationSetInstrs.begin(), PopulationSetInstrs.end(), FitnessSort);

        //for (size_t Unit = 0; Unit < PopulationSetInstrs.size(); ++Unit)
        //{
        //  std::sort(PopulationSetInstrs[Unit].begin(), PopulationSetInstrs[Unit].end(), FitnessSort);
        //}
    }

But I need to iterate somehow along the PopulationParametrs vector with the same iterator that PopulationSetInstrs has

Or do I need to redo the GA_Population class in some other way? I just need to contain instructions for the ZydisDisassembledInstruction class in the vector and add this from the GA_Class

float Fitness = 0.0f;

bool Parent = 0;

p.s. Please don't beat me with piss rags. I'm not good at programming in C++

  • [How can I sort two vectors in the same way, with criteria that uses only one of the vectors?](https://stackoverflow.com/questions/17074324/how-can-i-sort-two-vectors-in-the-same-way-with-criteria-that-uses-only-one-of) may help get you started. – Retired Ninja Jun 08 '23 at 17:40
  • @RetiredNinja, thanks! – Leo Galante Jun 08 '23 at 23:35

1 Answers1

0

I redid my classes and used range(https://github.com/ericniebler/range-v3)

Class

class GA_Population {
    private:
        std::vector<std::vector<ZydisDisassembledInstruction>> PopulationSetInstrs;
        //GA_Vector PopulationParametrs;
        std::vector<long double> svFitness;
        std::vector<bool> Parentness;
    public:
        inline void FitnessSorting();
    };

Method:

    inline void GA_Population::FitnessSorting()
    {
        ranges::v3::sort(ranges::view::zip(svFitness, PopulationSetInstrs),
            std::less<>{},
            [](const auto& t) -> decltype(auto) { return std::get<0>(t); });
    }