Skip to content

Commit

Permalink
add stepsizes to subproblem context
Browse files Browse the repository at this point in the history
  • Loading branch information
chinook25 committed Jan 25, 2021
1 parent 2e64b6d commit 672cf86
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/ts/Workspace/SubproblemContext/ISubproblemContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export default interface ISubproblemContext {
filteredRelativePerformances: IRelativePerformance[];
filteredWorkspace: IWorkspace;
observedRanges: Record<string, [number, number]>;
getStepSizeForCriterion: (criterion: ICriterion) => number;
}
13 changes: 11 additions & 2 deletions app/ts/Workspace/SubproblemContext/SubproblemContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ICriterion from '@shared/interface/ICriterion';
import {calculateObservedRanges} from 'app/ts/Subproblem/ScaleRanges/ScalesTable/ScalesTableUtil';
import React, {createContext, useContext, useEffect, useState} from 'react';
import {WorkspaceContext} from '../WorkspaceContext';
import {hasScaleValues} from '../WorkspaceContextUtil';
import ISubproblemContext from './ISubproblemContext';
import {applySubproblem} from './SubproblemUtil';
import {applySubproblem, getStepSize} from './SubproblemUtil';

export const SubproblemContext = createContext<ISubproblemContext>(
{} as ISubproblemContext
Expand Down Expand Up @@ -39,6 +40,13 @@ export function SubproblemContextProviderComponent({
setFilteredWorkspace(applySubproblem(workspace, currentSubproblem));
}, [workspace, currentSubproblem]);

function getStepSizeForCriterion(criterion: ICriterion) {
return getStepSize(
currentSubproblem.definition.ranges[criterion.dataSources[0].id],
currentSubproblem.definition.stepSizes[criterion.id]
);
}

return (
<SubproblemContext.Provider
value={{
Expand All @@ -48,7 +56,8 @@ export function SubproblemContextProviderComponent({
filteredDistributions: distributions,
filteredRelativePerformances: relativePerformances,
filteredWorkspace,
observedRanges
observedRanges,
getStepSizeForCriterion
}}
>
{children}
Expand Down
35 changes: 33 additions & 2 deletions app/ts/Workspace/SubproblemContext/SubproblemUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import IOldSubproblem from '@shared/interface/IOldSubproblem';
import IRelativePerformance from '@shared/interface/IRelativePerformance';
import IWorkspace from '@shared/interface/IWorkspace';
import IProblemCriterion from '@shared/interface/Problem/IProblemCriterion';
import {relative} from 'path';
import {applySubproblem} from './SubproblemUtil';
import {applySubproblem, getMagnitude, getStepSize} from './SubproblemUtil';

describe('The Subproblem util', () => {
describe('applySubproblem', () => {
Expand Down Expand Up @@ -325,4 +324,36 @@ describe('The Subproblem util', () => {
expect(result).toEqual(expectedResult);
});
});

describe('getStepSize', () => {
it('should return the calculated step size and magnitude for a configured range', () => {
const configuredRange: [number, number] = [0, 1];
const result = getStepSize(configuredRange, undefined);
const expectedResult = 0.1;
expect(result).toEqual(expectedResult);
});

it('should return the predefined step size and magnitude ', () => {
const configuredRange: [number, number] = [0, 1];
const result = getStepSize(configuredRange, 0.01);
const expectedResult = 0.01;
expect(result).toEqual(expectedResult);
});
});

describe('getMagnitude', () => {
it('should return the calculated step size and magnitude for a configured range', () => {
const configuredRange: [number, number] = [0, 1];
const result = getMagnitude(configuredRange, undefined);
const expectedResult = -1;
expect(result).toEqual(expectedResult);
});

it('should return the predefined step size and magnitude ', () => {
const configuredRange: [number, number] = [0, 1];
const result = getMagnitude(configuredRange, 0.01);
const expectedResult = -2;
expect(result).toEqual(expectedResult);
});
});
});
28 changes: 28 additions & 0 deletions app/ts/Workspace/SubproblemContext/SubproblemUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,31 @@ function cleanUpMu(
): Record<string, number> {
return _.omit(mu, excludedAlternatives);
}

export function getMagnitude(
[lowerConfiguredValue, upperConfiguredValue]: [number, number],
predefinedStepSize: number | undefined
): number {
if (predefinedStepSize) {
return Math.log10(predefinedStepSize);
} else {
const interval = upperConfiguredValue - lowerConfiguredValue;
const magnitude = Math.floor(Math.log10(interval)) - 1;
return magnitude;
}
}

export function getStepSize(
[lowerConfiguredValue, upperConfiguredValue]: [number, number],
predefinedStepSize: number | undefined
) {
if (predefinedStepSize) {
return predefinedStepSize;
} else {
const magnitude = getMagnitude(
[lowerConfiguredValue, upperConfiguredValue],
predefinedStepSize
);
return Math.pow(10, magnitude);
}
}

0 comments on commit 672cf86

Please sign in to comment.